2012-03-03 22 views
2

從我的Android手機中,我嘗試使用外部GPS設備讀取(使用藍牙)字符串。我主要關注了BluetoothChat的例子,並且一切似乎按預期工作到目前爲止。我讀線程執行,我可以看到變量字節的數據包用下面的代碼循環時傳入:我的藍牙連接只讀爲零

Log.d(TAG, "BEGIN mConnectedThread"); 
byte[] buffer = new byte[1024]; 
int bytes; 
// Keep listening to the InputStream while connected 
while (true) 
{ 
    try 
    {     
     bytes = mmInStream.read(buffer); 

     // Test... 
     String strReadBuf = new String(buffer, 0, bytes); 

     // Send the obtained bytes to the UI Activity 
     mHandler.obtainMessage(BluetoothHandler.MessageType.READ, 
      bytes, buffer).sendToTarget(); 
    } catch (IOException e) { 
     Log.e(TAG, "disconnected", e); 
     sendErrorMessage(R.string.bt_connection_lost); 
     break; 
    } 
} 

我應該閱讀的文本字符串(NMEA格式)的字符串,但我只是讀0 -32字節在我的緩衝區數組中。任何想法,爲什麼我得到這個?

+0

GPS是什麼品牌和型號? – Jens 2012-03-03 13:09:09

+0

這是一款u-blox LEA-4T GPS芯片集成。我們已經把它安裝到可以通過藍牙或USB輸出日誌的盒子中。當我將我的盒子連接到我的電腦(通過USB)時,我可以完美地看到NMEA琴絃。此外,我們正在使用PDA上的Windows Mobile程序廣泛使用藍牙連接,因此硬件集成不應該成爲問題。我不知道什麼可以使程序讀取0和-32字節... – Danuc 2012-03-03 13:18:45

回答

0
Log.d(TAG, "BEGIN mConnectedThread"); 
byte[] buffer = new byte[1024]; 
int bytes; 
// Keep listening to the InputStream while connected 
while (true) 
{ 
    try 
    { 
     bytes = mmInStream.read(buffer); 

     // Test... 
     //String strReadBuf = new String(buffer, 0, bytes); 
     //I've changed for 
     String strReadBuf = new String(buffer); 


     // Send the obtained bytes to the UI Activity 
     mHandler.obtainMessage(BluetoothHandler.MessageType.READ, 
      bytes, buffer).sendToTarget(); 
    } catch (IOException e) { 
     Log.e(TAG, "disconnected", e); 
     sendErrorMessage(R.string.bt_connection_lost); 
     break; 
    } 
} 

我用String構造String(byte[]),其中byte[]是緩衝器棄用byte[]大小。我已經使用了很多次,即使緩衝區大小隨着時間的推移而改變,這也適用於我。

+0

小心解釋它爲什麼起作用?它看起來像一個巫術編碼解決方案... – 2012-05-15 08:07:28