2012-08-15 45 views

回答

2

我已經創建了一個NXT將數據發送回我的A​​ndroid設備的項目。下面是一些代碼,應該工作:

這是所有的Android端代碼:

這是一個類,我寫的,將採取的連接,並通過藍牙通信護理。

public class Connector { 

    public static final String TAG = "Connector"; 

    public static final boolean BT_ON = true; 
    public static final boolean BT_OFF = false; 

    public BluetoothAdapter bluetoothAdapter; 
    public BluetoothSocket bluetoothSocket; 
    public String address; 

    public Connector(String address) { 
     this.address = address; 
     this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    } 

    public void setBluetooth(boolean state) { 
     if(state == Connector.BT_ON) { 
      // Check if bluetooth is off 
      if(this.bluetoothAdapter.isEnabled() == false) 
      { 
       this.bluetoothAdapter.enable(); 
       while(this.bluetoothAdapter.isEnabled() == false) { 

       } 
       Log.d(Connector.TAG, "Bluetooth turned on"); 

      } 

     } 
     // Check if bluetooth is enabled 
     else if(state == Connector.BT_OFF) { 
      // Check if bluetooth is enabled 
      if(this.bluetoothAdapter.isEnabled() == true) 
      { 
       this.bluetoothAdapter.disable(); 
       while(this.bluetoothAdapter.isEnabled() == true) { 

       } 
       Log.d(Connector.TAG, "Bluetooth turned off"); 

      } 

     } 

    } 

    public boolean connect() { 

     boolean connected = false; 
     BluetoothDevice nxt = this.bluetoothAdapter.getRemoteDevice(this.address); 

     try { 
      this.bluetoothSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
      this.bluetoothSocket.connect(); 
      connected = true; 

     } 
     catch (IOException e) { 
      connected = false; 

     } 

     return connected; 

    } 

    public Integer readMessage() { 
     Integer message; 

     if(this.bluetoothSocket!= null) { 
      try { 
       InputStreamReader input = new InputStreamReader(this.bluetoothSocket.getInputStream()); 
       message = input.read(); 
       Log.d(Connector.TAG, "Successfully read message"); 

      } 
      catch (IOException e) { 
       message = null; 
       Log.d(Connector.TAG, "Couldn't read message"); 

      } 
     } 
     else { 
      message = null; 
      Log.d(Connector.TAG, "Couldn't read message"); 

     } 

     return message; 

    } 


} 

在您的活動課程中,您可以創建Connector對象。在onCreate()方法,你必須連接到建立像這樣的NXT的連接:

// Establish a bluetooth connection to the NXT 
this.connector = new Connector("00:16:53:12:B6:78"); 
this.connector.setBluetooth(Connector.BT_ON); 
this.connector.connect(); 

現在讀從NXT(Integer對象),你可以做這樣的一條消息:

this.connector.readMessage(); 

要關閉連接:

this.connector.setBluetooth(Connector.BT_OFF); 

這是所有NXT端代碼:

注意:下載leJOS爲所有代碼工作(leJOS將允許您在Java中編碼您的NXT)。

在主類中定義這兩個對象:

public static DataOutputStream dataOutputStream; 
public static NXTConnection bluetoothConnection; 

連接到您的手機:

bluetoothConnection = Bluetooth.waitForConnection(); 
bluetoothConnection.setIOMode(NXTConnection.RAW); 
dataOutputStream = bluetoothConnection.openDataOutputStream(); 

將數據發送到手機中的Integer對象的形式:

dataOutputStream.write(100); 
dataOutputStream.flush(); 

要斷開連接運行:

dataOutputStream.close(); 
bluetoothConnection.close(); 

我希望這會有所幫助。

1

我對藍牙命令有些困惑,但現在我發現你需要下載leJOS!

我通常會盡量避免與NXT上的固件混淆,但是java更容易處理!

對於任何有興趣的人,你可以發送命令到你的機器人的NXT本身的格式,雖然它不如上面列出的漂亮。有一個偉大的教程在這裏: http://www.robotappstore.com/Knowledge-Base/Programming-LEGO-NXT-Mindstorms/92.html

但是,如果你想下載的應用程序免費,這裏是一個: http://www.robotappstore.com/Apps/Lego-NXT-Mindstorms-Driver---Android-app.html?x=693A00AA-7F15-46E7-9616-8101068DB58D

還有一堆多,如果你只是搜索周圍有太多

希望這會有所幫助!

相關問題