0
我使用的代碼示例在這裏找到http://goldenhillbooks.com/blog/?p=59#respond。我用下面的代碼修改了Run。我遇到的問題是當我斷開應用程序崩潰連接的藍牙設備之一。如果我評論運行部分,它不會崩潰。藍牙運行接收失敗
有人能告訴我發生了什麼事嗎?
public void run() {
final byte delimiter = 10; //This is the ASCII code for a newline character
byte[] readBuffer;
int readBufferPosition;
readBufferPosition = 0;
readBuffer = new byte[1024];
// Keep listening to the InputStream until an exception occurs
while (threadNumber>0) {
try {
int bytesAvailable = inStream.available();
if(bytesAvailable > 0) {
final byte[] packetBytes = new byte[bytesAvailable];
inStream.read(packetBytes);
//myLabel.setText(packetBytes.toString());
for(int i=0;i<bytesAvailable;i++) {
byte b = packetBytes[i];
if(b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
Log.d(TAG,
"BtConnectionThread run while loop: received from arduino:" + data);
Intent e = new Intent("com.Ryan.myhue.BT_LISTENER");
e.putExtra("notification_event",data);
mycontext.sendBroadcast(e);
}
else {
readBuffer[readBufferPosition++] = b;
}
}
} //bytesAvailable
} catch (IOException e) {
Log.e(TAG,
"BtConnectionThread run while loop: problem reading");
e.printStackTrace();
break;
}
}
}