2016-06-14 70 views
2

我嘗試使用Android通過藍牙(BT)接收文本。我可以使用Google Play商店中預先存在的藍牙記錄器查看文本,但使用我自己的代碼無法正常工作。通過藍牙的Android ReceiveReceive文本

一般來說,我使用Android文檔和samples的默認BT代碼。我記錄收到的文字。我希望我的文本是這樣的: -0.01, -0.03, 1.04,但我只收到它的一部分。這裏是日誌的一部分:

06-14 17:25:01.925 28217-28412/[...]: I read the following: 0.01, -0. 
06-14 17:25:01.977 28217-28412/[...]: I read the following: 
06-14 17:25:02.039 28217-28412/[...]: I read the following: - 
06-14 17:25:02.051 28217-28412/[...]: I read the following: 0.01, -0. 
06-14 17:25:02.101 28217-28412/[...]: I read the following: 
06-14 17:25:02.174 28217-28412/[...]: I read the following: - 

這是我使用BT閱讀代碼:

public void run() { 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer); 
       String readMessage = new String(buffer, 0, bytes); 
       Log.e(Constants.TAG, "I read the following: "+readMessage); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

不知道,如果是相關的,但我使用連接的HC-06藍牙模塊向Arduino發送帶有9600波特的數據。我也可以給你這個代碼。

我試了兩種不同的Android手機,結果相同。 Wiko Fever和Moto G2。

回答

1

好的,我發現了這個問題。實際的傳輸是好的。這是兩件事的結合。首先我使用Serial.println從Arduino傳輸數據,我將其更改爲Serial.print。此外,我將Logging扔出去並將結果發送給用戶界面。

所以看起來,Android日誌框架對於由Arduino發送的新行字符似乎是合理的,並且(部分)跳過日誌消息,如上所見。