2010-04-09 38 views
11

我對這一切都有點新,所以請耐心等待 - 我真的很感謝您的幫助。將Android Nexus One與Arduino + BlueSmirf接口

我在嘗試將Android Nexus One與連接到BlueSmirf的arduino(Duemilanove)鏈接。我有一個程序,只是輸出字符串「你好藍牙」到任何設備BlueSmirf連接到。這裏是Arduino程序:

void setup(){ Serial.begin(115200); int i; }

void loop(){Serial.print(「Hello Bluetooth!」); 延遲(1000); }

一臺我的電腦BT終端我可以看到消息並連接沒有問題。麻煩是我的android代碼。我可以使用android連接到設備,但是當我查看日誌時,它不顯示「Hello Bluetooth」。下面是調試日誌:


04-09 16:27:49.022:ERROR/BTArduino(17288):螢火蟲-2583連接
04-09 16:27:49.022:ERROR/BTArduino(17288):在開始以連接所述插座
04-09 16:27:55.705:ERROR/BTArduino(17288):接收時間:16
04-09 16:27:56.702:ERROR/BTArduino(17288):接收時間:1
04- 09 16:27:56.712:錯誤/ BTArduino(17288):收到:15
04-09 16:27:57.702:錯誤/ BTArduino(17288):收到:1
04-09 16:27:57.702:錯誤/ BTArduino(17288):收到:15
04-09 16:27:58.704:ERROR/BTArduino(17288):接收時間:1
04-09 16:27:58.704:ERROR/BTArduino(17288):接收時間:15

等...

下面是代碼,我想只是把相關代碼,但如果你需要更多信息,請讓我知道:

private class ConnectThread extends Thread { 
    private final BluetoothSocket mySocket; 
    private final BluetoothDevice myDevice; 

    public ConnectThread(BluetoothDevice device) { 
     myDevice = device; 
     BluetoothSocket tmp = null; 
     try { 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK"); 
     } 
     mySocket = tmp; 
    } 
    public void run() { 
     Log.e(TAG, "STARTING TO CONNECT THE SOCKET"); 
     InputStream inStream = null; 
     boolean run = false; 
     //...More Connection code here... 

的多個相對代碼是在這裏:

 byte[] buffer = new byte[1024]; 
     int bytes; 

     // handle Connection 
     try { 
      inStream = mySocket.getInputStream(); 
      while (run) { 
       try { 
        bytes = inStream.read(buffer); 
        Log.e(TAG, "Received: " + bytes); 
       } catch (IOException e3) { 
        Log.e(TAG, "disconnected"); 
       } 
      } 

我正在讀取bytes = inStream.read(buffer)。我知道字節是一個整數,所以我嘗試通過藍牙發送整數,因爲「字節」是一個整數,但它仍然沒有意義。

它幾乎看起來是發送不正確的波特率。這是真的嗎?

任何幫助,將不勝感激。非常感謝你。

+0

改爲使用writeln/readln組合。 – 2010-04-21 06:03:29

回答

1

read()返回它成功地讀入緩衝區的字節數。因此,在這行代碼:

bytes = inStream.read(buffer); 

...您的信息將在第一bytes字節buffer發現(假設一切是正確的)。您可以將那些爲一個字符串,像這樣:

String message = new String(buffer, 0, bytes); 

我掩飾了一些東西在這裏(編碼,串聯多個緩衝區等),但這應該讓你開始。