Android InputStream丢弃前两个字节(已修改的BluetoothChat)

我已经使用BluetoothChat示例中的代码来发送和接收来自Bluetooth Scale的字节数据。 秤从设备接收命令,然后发回一个字节数组。 {2,198,48,48,48,48,199,3}在我们的通信协议中,2 = STX,198 =分组开始,199 =分组结束,3 = ETX。

所有的工作都很好,除了下面的代码在BluetoothChatService.java奇怪的反应,它会丢弃前两个字节。

/** * This thread runs during a connection with a remote device. * It handles all incoming and outgoing transmissions. */ private class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket, String socketType) { Log.d(TAG, "create ConnectedThread: " + socketType); mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; // Get the BluetoothSocket input and output streams try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "temp sockets not created", e); } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { Log.i(TAG, "BEGIN mConnectedThread"); final byte[] buffer = new byte[1024]; int bytes; // Keep listening to the InputStream while connected while (true) { try { // Read from the InputStream bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI Activity mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } catch (IOException e) { Log.e(TAG, "disconnected", e); connectionLost(); // Start the service over to restart listening mode BluetoothChatService.this.start(); break; } } } 

我有一个特别的问题,下面的代码段:

  bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI Activity mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); 

在执行debugging之前,在mmInStream.read(buffer)中查看缓冲区的内容时,缓冲区将包含由缩放设备发回的正确数据,即:

 {2,198,48,48,48,48,48,199,3} 

但是一旦代码被分步执行,缓冲区的前两个字节就被剥离了,现在它错误地包含了:

 {48,48,48,48,48,199,3} 

这就是消息处理程序最终传递给活动。

为了更清楚起见,我必须补充说明,按比例发送的字节stream是在00到FF范围内的hex字符。 出于一些奇怪的原因,string在debugging器中看起来像这样:

 {2,-58,48,48,48,48,48,-57,3} 

然后2,-58被丢弃。

我注意到,当我通过套接字发送一个字节数组时,我需要执行以下操作:

 byte[] sendBytes = {2,(byte)198,48,48,48,48,48,(byte)199,3} 

当这个数组的内容被debugging时,会给出{2,-58,48,48,48,48,48,-57,3}

请理解我是Android新手 – java,有很多东西要学。 所有的帮助将不胜感激。 谢谢阿德里安

我已经添加了log.i条目,以便根据Radu的build议更好地了解正在发生的事情。 看来,在我通过蓝牙向我的设备写入数据之后,它会作出响应,并且由于某些原因我们只读取前两个字节,然后将这些数据发送到消息处理程序,然后读取设备发送的其余数据包,然后发送这closures到消息处理程序,但在处理程序甚至第一次响应之前,缓冲区已被覆盖,因此,当处理程序尝试读取前两个字节时,它正在读取响应的第3个和第4个字节数据包,然后立即再次响应,从3-17位读取整个数据包。 所以,如果我可以把它简单地..消息处理程序只会响应发送的缓冲区被覆盖之后。 看下面的日志:

 09-05 13:16:52.093: V/BluetoothSocket.cpp(11279): writeNative 09-05 13:16:52.118: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 2 09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 2 09-05 13:16:52.118: I/IN_BUF_AFTER(11279): -58 09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 0 09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 0 ...truncated to save space ... 09-05 13:16:52.163: I/IN_BUF_AFTER(11279): 0 09-05 13:16:52.163: I/IN_BUFFER(11279): We now send to handler. 09-05 13:16:52.168: I/IN_BUFFER(11279): Read Stream into Buffer: 09-05 13:16:52.168: V/BluetoothSocket.cpp(11279): readNative 09-05 13:16:52.168: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 17 09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 44 09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 49 09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 50 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 48 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 44 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 85 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 13 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): -57 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 3 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 6 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 0 ...truncated to save space ... 09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 0 09-05 13:16:52.188: I/IN_BUFFER(11279): We now send to handler. 09-05 13:16:52.193: I/IN_BUFFER(11279): Read Stream into Buffer: 09-05 13:16:52.208: V/BluetoothSocket.cpp(11279): readNative 09-05 13:16:52.208: I/MESSAGE_READ(11279): I am reading 2 bytes 09-05 13:16:52.208: I/Content(11279): The entire array: 09-05 13:16:52.208: I/some hardcoded tag(11279): 0 09-05 13:16:52.208: I/some hardcoded tag(11279): 0 09-05 13:16:52.273: I/MESSAGE_READ(11279): I am reading 17 bytes 09-05 13:16:52.273: I/Content(11279): The entire array: 09-05 13:16:52.273: I/some hardcoded tag(11279): 0 ...truncated to save space ... 09-05 13:16:52.283: I/some hardcoded tag(11279): 0 09-05 13:16:52.283: I/some hardcoded tag(11279): 0 09-05 13:16:54.528: V/BluetoothSocket.cpp(11279): writeNative 09-05 13:16:54.553: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 2 09-05 13:16:54.553: I/IN_BUF_AFTER(11279): 2 09-05 13:16:54.553: I/IN_BUF_AFTER(11279): -58 09-05 13:16:54.558: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.558: I/IN_BUF_AFTER(11279): 0 ...truncated to save space ... 09-05 13:16:54.618: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.618: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.618: I/IN_BUFFER(11279): We now send to handler. 09-05 13:16:54.618: I/IN_BUFFER(11279): Read Stream into Buffer: 09-05 13:16:54.618: V/BluetoothSocket.cpp(11279): readNative 09-05 13:16:54.623: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 17 09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 44 09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.633: I/IN_BUF_AFTER(11279): 49 09-05 13:16:54.633: I/IN_BUF_AFTER(11279): 50 09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 48 09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 44 09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 85 09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 13 09-05 13:16:54.638: I/IN_BUF_AFTER(11279): -57 09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 3 09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 6 09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 0 ...truncated to save space ... 09-05 13:16:54.653: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.653: I/IN_BUF_AFTER(11279): 0 09-05 13:16:54.653: I/IN_BUFFER(11279): We now send to handler. 09-05 13:16:54.653: I/IN_BUFFER(11279): Read Stream into Buffer: 09-05 13:16:54.653: V/BluetoothSocket.cpp(11279): readNative 09-05 13:16:54.658: I/MESSAGE_READ(11279): I am reading 2 bytes 09-05 13:16:54.658: I/Content(11279): The entire array: 09-05 13:16:54.658: I/some hardcoded tag(11279): 0 09-05 13:16:54.663: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/MESSAGE_READ(11279): I am reading 17 bytes 09-05 13:16:54.723: I/Content(11279): The entire array: 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.723: I/some hardcoded tag(11279): 0 09-05 13:16:54.728: I/some hardcoded tag(11279): 0 09-05 13:16:54.728: I/some hardcoded tag(11279): 0 09-05 13:16:54.728: I/some hardcoded tag(11279): 0 

我的新代码也将缓冲区重置为0,然后再读取最新的stream,因此消息处理程序只看到0,在执行此操作之前,日志显示如下:

 09-05 13:06:20.508: V/BluetoothSocket.cpp(10176): writeNative 09-05 13:06:20.533: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 2 09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 2 09-05 13:06:20.533: I/IN_BUF_AFTER(10176): -58 09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.538: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.538: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.553: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.553: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.578: I/IN_BUFFER(10176): We now send to handler. 09-05 13:06:20.578: I/IN_BUFFER(10176): Read Stream into Buffer: 09-05 13:06:20.578: V/BluetoothSocket.cpp(10176): readNative 09-05 13:06:20.578: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 17 09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 44 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 49 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 51 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 44 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 85 09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 13 09-05 13:06:20.598: I/IN_BUF_AFTER(10176): -57 09-05 13:06:20.598: I/IN_BUF_AFTER(10176): 3 09-05 13:06:20.613: I/IN_BUF_AFTER(10176): 6 09-05 13:06:20.613: I/IN_BUF_AFTER(10176): 0 ...truncated to save space ... 09-05 13:06:20.623: I/IN_BUF_AFTER(10176): 0 09-05 13:06:20.623: I/IN_BUFFER(10176): We now send to handler. 09-05 13:06:20.623: I/IN_BUFFER(10176): Read Stream into Buffer: 09-05 13:06:20.623: V/BluetoothSocket.cpp(10176): readNative 09-05 13:06:20.628: I/MESSAGE_READ(10176): I am reading 2 bytes 09-05 13:06:20.628: I/Content(10176): The entire array: 09-05 13:06:20.628: I/some hardcoded tag(10176): 48 09-05 13:06:20.628: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/MESSAGE_READ(10176): I am reading 17 bytes 09-05 13:06:20.688: I/Content(10176): The entire array: 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.688: I/some hardcoded tag(10176): 44 09-05 13:06:20.688: I/some hardcoded tag(10176): 48 09-05 13:06:20.693: I/some hardcoded tag(10176): 48 09-05 13:06:20.693: I/some hardcoded tag(10176): 49 09-05 13:06:20.693: I/some hardcoded tag(10176): 51 09-05 13:06:20.693: I/some hardcoded tag(10176): 48 09-05 13:06:20.693: I/some hardcoded tag(10176): 44 09-05 13:06:20.693: I/some hardcoded tag(10176): 85 09-05 13:06:20.693: I/some hardcoded tag(10176): 13 09-05 13:06:20.693: I/some hardcoded tag(10176): -57 09-05 13:06:20.693: I/some hardcoded tag(10176): 3 09-05 13:06:20.693: I/some hardcoded tag(10176): 6 09-05 13:06:21.788: V/BluetoothSocket.cpp(10176): writeNative 09-05 13:06:21.803: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 2 09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 2 09-05 13:06:21.803: I/IN_BUF_AFTER(10176): -58 09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.808: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.808: I/IN_BUF_AFTER(10176): 44 09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 49 09-05 13:06:21.823: I/IN_BUF_AFTER(10176): 51 09-05 13:06:21.823: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.828: I/IN_BUF_AFTER(10176): 44 09-05 13:06:21.828: I/IN_BUF_AFTER(10176): 85 09-05 13:06:21.833: I/IN_BUF_AFTER(10176): 13 09-05 13:06:21.848: I/IN_BUF_AFTER(10176): -57 09-05 13:06:21.848: I/IN_BUF_AFTER(10176): 3 09-05 13:06:21.848: I/IN_BUF_AFTER(10176): 6 09-05 13:06:21.853: I/IN_BUF_AFTER(10176): 0 ...truncated to save space ... 09-05 13:06:21.853: I/IN_BUF_AFTER(10176): 0 09-05 13:06:21.853: I/IN_BUFFER(10176): We now send to handler. 09-05 13:06:21.858: I/IN_BUFFER(10176): Read Stream into Buffer: 09-05 13:06:21.858: V/BluetoothSocket.cpp(10176): readNative 09-05 13:06:21.858: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 17 09-05 13:06:21.858: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 44 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 49 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 51 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 44 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 85 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 13 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): -57 09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 3 09-05 13:06:21.873: I/IN_BUF_AFTER(10176): 6 09-05 13:06:21.873: I/IN_BUF_AFTER(10176): 0 ...truncated to save space ... 09-05 13:06:21.893: I/IN_BUF_AFTER(10176): 0 09-05 13:06:21.893: I/IN_BUFFER(10176): We now send to handler. 09-05 13:06:21.893: I/IN_BUFFER(10176): Read Stream into Buffer: 09-05 13:06:21.898: V/BluetoothSocket.cpp(10176): readNative 09-05 13:06:21.903: I/MESSAGE_READ(10176): I am reading 2 bytes 09-05 13:06:21.903: I/Content(10176): The entire array: 09-05 13:06:21.903: I/some hardcoded tag(10176): 48 09-05 13:06:21.903: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/MESSAGE_READ(10176): I am reading 17 bytes 09-05 13:06:21.958: I/Content(10176): The entire array: 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 44 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 49 09-05 13:06:21.958: I/some hardcoded tag(10176): 51 09-05 13:06:21.958: I/some hardcoded tag(10176): 48 09-05 13:06:21.958: I/some hardcoded tag(10176): 44 09-05 13:06:21.958: I/some hardcoded tag(10176): 85 09-05 13:06:21.958: I/some hardcoded tag(10176): 13 09-05 13:06:21.958: I/some hardcoded tag(10176): -57 09-05 13:06:21.963: I/some hardcoded tag(10176): 3 09-05 13:06:21.963: I/some hardcoded tag(10176): 6 

我希望没有混淆这个问题,但实际上certificate了这个问题,很多人似乎在使用BluetoothChat演示代码的时候,已经被自动使用了。 不知何故,我们需要保持缓冲区被覆盖,直到消息处理程序已经读取它? 问候

阿德里安Wreyford

更新的代码更好地工作,因为睡眠!

 public void run() { Log.i(TAG, "BEGIN IN mConnectedThread"); byte[] buffer = new byte[1024]; int bytes; // Keep listening to the InputStream while connected while (true) { try { try { sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } bytes = mmInStream.available(); Log.i("IN_BUFFER", "mmInStream-available bytes: " + Integer.toString(bytes)+ " "); if (bytes>0){ for(int i=0; i<30; i++){ buffer[i] = 0;} // Read from the InputStream Log.i("IN_BUFFER", "Read Stream into Buffer:"); bytes = mmInStream.read(buffer); Log.i("IN_BUFFER", "The entire buffer after read stream into buffer: " + Integer.toString(bytes)+ " "); for(int i=0; i<30; i++) Log.i("IN_BUF_AFTER", buffer[i] + " "); // Send the obtained bytes to the UI Activity Log.i("IN_BUFFER", "We now send to handler."); mHandler.obtainMessage(BluetoothScale.MESSAGE_READ, bytes, -1, buffer) .sendToTarget();} } catch (IOException e) { Log.e(TAG, "disconnected", e); connectionLost(); // Start the service over to restart listening mode BluetoothScaleService.this.start(); break; } } } 

现在日志如下所示:

 09-05 20:57:15.833: V/BluetoothSocket.cpp(25368): availableNative 09-05 20:57:15.838: I/IN_BUFFER(25368): mmInStream-available bytes: 0 09-05 20:57:15.888: V/BluetoothSocket.cpp(25368): availableNative 09-05 20:57:15.888: I/IN_BUFFER(25368): mmInStream-available bytes: 0 09-05 20:57:15.943: V/BluetoothSocket.cpp(25368): availableNative 09-05 20:57:15.943: I/IN_BUFFER(25368): mmInStream-available bytes: 0 09-05 20:57:15.958: V/BluetoothSocket.cpp(25368): writeNative 09-05 20:57:15.988: V/BluetoothSocket.cpp(25368): availableNative 09-05 20:57:15.993: I/IN_BUFFER(25368): mmInStream-available bytes: 2 09-05 20:57:15.993: I/IN_BUFFER(25368): Read Stream into Buffer: 09-05 20:57:15.993: V/BluetoothSocket.cpp(25368): readNative 09-05 20:57:15.998: I/IN_BUFFER(25368): The entire buffer after read stream into buffer: 19 09-05 20:57:15.998: I/IN_BUF_AFTER(25368): 2 09-05 20:57:15.998: I/IN_BUF_AFTER(25368): -58 09-05 20:57:16.003: I/IN_BUF_AFTER(25368): 48 ...truncated to save space ... 09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 85 09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 13 09-05 20:57:16.033: I/IN_BUF_AFTER(25368): -57 09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 3 09-05 20:57:16.038: I/IN_BUF_AFTER(25368): 6 09-05 20:57:16.038: I/IN_BUF_AFTER(25368): 0 ...truncated to save space ... 09-05 20:57:16.043: I/IN_BUF_AFTER(25368): 0 09-05 20:57:16.043: I/IN_BUFFER(25368): We now send to handler. 09-05 20:57:16.058: I/MESSAGE_READ(25368): I am reading 19 bytes 09-05 20:57:16.058: I/Content(25368): The entire array: 09-05 20:57:16.058: I/some hardcoded tag(25368): 2 09-05 20:57:16.058: I/some hardcoded tag(25368): -58 09-05 20:57:16.058: I/some hardcoded tag(25368): 48 ...truncated to save space ... 09-05 20:57:16.063: I/some hardcoded tag(25368): 13 09-05 20:57:16.063: I/some hardcoded tag(25368): -57 09-05 20:57:16.063: I/some hardcoded tag(25368): 3 09-05 20:57:16.063: I/some hardcoded tag(25368): 6 09-05 20:57:16.093: V/BluetoothSocket.cpp(25368): availableNative 09-05 20:57:16.093: I/IN_BUFFER(25368): mmInStream-available bytes: 0 

请注意,mmInStream.available()返回2个字节,然后在下一行代码中,当我们读取缓冲区时,有19个字节被读取。真的很奇怪,它是如何填满这两个所谓的即时步骤。 睡眠似乎有足够的时间让处理程序在重写缓冲区之前从传递的缓冲区中读取消息。

我会期待的handler.obtainmessage …发送一个唯一的缓冲区,但似乎发送引用到线程缓冲区,从而麻烦。 我怎样才能每次发送一个唯一的缓冲区? Thx Adrian

在使用蓝牙聊天的例子之前,我看到有人遇到过这样的问题。 示例代码的问题是,发送给Handler的消息对象只包含对每个后续read()操作使用的实际byte[]数组的引用。 这意味着只要Handler获得消息并开始检查数组,蓝牙套接字上的后续read()操作就有机会向该数组写入更新的数据。

在这行代码中:

 mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) .sendToTarget() 

数组被复制; 而是消息只是将对象引用传递给同一个数组。

为什么蓝牙聊天的例子只是为了达到原来的目的,唯一的原因是它的目的是以人类打字速度传达短小的一串字符。 如果你发送的东西比这更快,那么Handler从这个数组中读取的内容就会变成垃圾。

答案是发送一个数组副本(例如System.arraycopy() )或使用一个简单的循环缓冲区,这是我自己的蓝牙应用程序使用。

事实上,前两个字节被破坏是一个奇怪的事实,但它可能只是在特定设备的蓝牙堆栈中的基础读取操作的具体实现。 简单的事实是,一旦你用这个缓冲区调用了read() ,你就不应该同时触及这个缓冲区或者关心它的内容。 也许在你的设备上的特定的蓝牙套接字读取实现与缓冲区中的前几个字节做一些事情,因为它们的内部操作有关。 但是当read()被阻塞时,你不应该关心缓冲区是什么样的有趣的中间状态,因为当时没有其他的线程应该尝试去理解它。 你所关心的是,当read()返回时,缓冲区处于有效状态,有效数据。

使用sleep()操作显然部分“解决”了问题的原因是因为在随后的read()操作在数组上执行操作之前,给您的Handler时间来查看数组是一种粗暴的方式。 虽然这不是一个好的解决scheme。

第二个问题是因为在Java中, byte是有符号的。 这就是debugging器将字节显示为有符号值的原因。 在您的应用程序中,如果您需要将给定的字节作为int并且该字节最初未经签名,则可以执行如下操作:

 int someValue = myByteArray[someIndex] & 0xff; 

我认为你所看到的问题,比如198转成-58就是因为Java使用带符号的字节,所以超过127就是负数。 所以198二进制被视为 – (256 – 198)= – 58。
解决scheme是使用如下代码将其转换为int:

 private int UByte(byte b){ if(b<0) // if negative return (int)( (b&0x7F) + 128 ); else return (int)b; } 

如前所述,数据不能得到保证,蓝牙是一个开放的数据stream,如UDP。

我想对rs232的一些理解是需要知道什么时候字节将打包在一起,或单独发送。

用一个微控制器项目,想象一下:

 if (data_ready) echo read_byte(); 

使用PIC芯片,生成的数据如下所示:

 0 0 0 0 0 0 0 h 0 0 0 0 e 0 0 0 0 0 0 l 0 0 0 0 0 0 l 0 0 0 0 0 0 o ... 

随着Android的微控制器项目,我正在做的事情,我正在做这样的事情:

 do {instream.read()} while (!DELIMETER) 

我发现在阅读蓝牙数据stream时,您需要高效地使用您的代码。 使用除read()以外的inputstream方法,我几乎总是会错过第一个字节。

我刚刚开始使用智能蓝牙技术,因为数据只有在可用时才会发送,所以我猜测它是完全不同的。

不知道这是为什么发生,但可能是一些奇怪的竞争条件。 你的代码的问题之一是它想要读取所有的时间数据! 即使没有数据。

所以,也许你读了前2个字节,然后再次读取input缓冲区,而没有前2个字节(debugging器在这种情况下显示错误的图片)。

你可以尝试改进的是通过添加这些行(不要试图复制粘贴,我正在调整代码到你以前的代码 – 它可能会或可能不会工作):

 bytes = mmInStream.getAvailable(); if (bytes>0) { Log.i("some hardcoded tag", "I am reading " + Integer.toString(bytes) + " bytes"); //you could also print the entire bytearray /* Log.i("some hardcoded tag", "The entire array:"); for(int i=0; i<bytes; i++) Log.i("some hardcoded tag", buffer[i] + " ");*/ mmInStream.read(buffer); //and your code .. // Send the obtained bytes to the UI Activity mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } 

注意我使用Log.i而不是debugging器。 对于multithreading的东西来说,如果你可以实现日志,并且不打扰你,那么对于multithreading的东西来说更好。另外它的执行速度更快,而且更容易debugging。

我几个星期前就和你一样。

我发现使用read(buffer)时读取计数是随机的。 所以你永远不会真正预测下一个数据包会有多大。

对我来说简单的解决方法是使用read()一次读取一个字节,并将其添加到本地缓冲区,直到接收到每个字节。

希望这可以帮助。