2013-01-23 75 views
0

數據我按照這個例子:http://english.cxem.net/arduino/arduino5.php接收來自Arduino的藍牙設備

我有一個Arduino的UNO板設置與sparkfun藍牙設備。我可以連接並從android發送數據到arduino,並且我可以在串口監視器中看到這些數據,但我無法從arduino(串口監視器)發送數據並返回到Android。

我正在使用在活動中的onResume方法中啓動的ConnectThread。這是我的線程代碼:

private class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 

     public ConnectedThread(BluetoothSocket socket) { 
      Log.d("THREAD", "constructor"); 
      mmSocket = socket; 
      InputStream tmpIn = null; 
      OutputStream tmpOut = null; 

      // Get the input and output streams, using temp objects because 
      // member streams are final 
      try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 
     } 

     public void run() { 
      Log.d("THREAD", "inside run"); 
      byte[] buffer = new byte[256]; // buffer store for the stream 
      int bytes; // bytes returned from read() 

      // Keep listening to the InputStream until an exception occurs 
      while (true) { 
       Log.d("in loop", "waiting for data"); 
       try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer);  // Get number of bytes and message in "buffer" 
        h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();  // Send to message queue Handler 
        Log.d("recieve", "b " + bytes); 
       } catch (IOException e) { 
        break; 
       } 
      } 
     } 

     /* Call this from the main activity to send data to the remote device */ 
     public void write(String message) { 
      Log.d("TAG", "...Data to send: " + message + "..."); 
      byte[] msgBuffer = message.getBytes(); 
      try { 
       mmOutStream.write(msgBuffer); 
      } catch (IOException e) { 
       Log.d("TAG", "...Error data send: " + e.getMessage() + "...");  
       } 
     } 

     /* Call this from the main activity to shutdown the connection */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { } 
     } 



    } 

我需要使用,因爲串口監視器總是等待數據只發送數據時,我按下發送服務?

編輯:Arduino的代碼:

#include <SoftwareSerial.h> 

int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2 
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3 

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); 

void setup() 
{ 
    Serial.begin(9600); // Begin the serial monitor at 9600bps 

    bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps 
    bluetooth.print("$$$"); // Enter command mode 
    delay(100); // Short delay, wait for the Mate to send back CMD 
    bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity 
    // 115200 can be too fast at times for NewSoftSerial to relay the data reliably 
    bluetooth.begin(9600); // Start bluetooth serial at 9600 
} 

    void loop() 
{ 
    if(bluetooth.available()) // If the bluetooth sent any characters 
    { 
    // Send any characters the bluetooth prints to the serial monitor 
    Serial.print((char)bluetooth.read()); 
    } 
    if(Serial.available()) // If stuff was typed in the serial monitor 
    { 
    char c = (char) Serial.read(); 
    // Send any characters the Serial monitor prints to the bluetooth 
    bluetooth.print(c); 
    } 
    // and loop forever and ever! 
} 
+0

嗨oivindth,我也遇到了類似的問題,因爲我也碰巧提到你有相同的來源。你提到你在線程中使用Looper.prepare解決了這個問題,可以請你多解釋一下,或者如果你可以在上面的代碼中更新你的解決方案,那將會很棒。謝謝一堆。 –

+1

嗨@NicholasTJ。我只是添加了Looper.prepare();在我的Run()方法中。如果這沒有幫助,我可以用代碼更新帖子。 – oivindth

+0

我知道它運行了,但是沒有與Looper.prepare(),哦,我想我應該看看Looper.prepare()並看看它做了什麼。但是如果你對懶惰很滿意,那麼在你的帖子中更新你的代碼會很棒。 –

回答

1

不,你並不需要一個服務,一個線程應該很好地工作。這段代碼實際上與通過Android SDK提供的BluetoothChat示例中提供的代碼完全相同,因此它應該可以工作。

通過使用串行監視器,您正在將數據發送到Arduino,所以我假設您使用Serial.println(「數據字符串」)或類似的東西回顯該數據。但是,由於您遵循了您引用的文章,因此您已將藍牙芯片連接到Arduino上的RX/TX引腳(0和1),這與串行監視器使用的引腳相同。我發現,當藍牙芯片連接到這些時,串行監視器仍然接收數據,但不能再發送它。所以你的問題在Arduino方面。

此外,此StackOverflow answer提到您不能同時在引腳0和1上同時使用串行監視器和藍牙。

如果您仍想使用串行監視器,請將藍牙芯片連接到不同的數字引腳,然後按here所述使用SoftwareSerial Arduino庫。

我已經對this post中描述的藍牙聊天樣本進行了簡單的修改,我的Android從Arduino接收沒有問題。

+0

我實際上已經在Matt Bells博客中描述過使用SoftwareSerial庫,並且已經按照該文章的說法連接了arduino。 (第2和第3針)我也試着定期發送數據,但沒有收到任何消息。如果有幫助,我可以發佈我的arduino代碼。 – oivindth