数据看起来像这样:
'<' Ch1 Ch2 seq crc''
Ch1,Ch2 - uint16
seq,crc - uint8
整体有8个字节
重新接受后我需要Ch1或Ch2"打包"到整数并在图表上显示。
代码中的注释片段是我的解决方案,但目前我无法测试(我不知道这是否有效)所以我想问你这是不是很好的方法呢?也许有更好的方法可以做到这一点?
这是代码:
class ConnectedThread extends Thread {
private static final String TAG = "BluetoothCommandService";
public static final int EXIT_CMD = -1;
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public static int data;
public static void shiftRight(byte[] list)
{
if (list.length < 2) return;
byte last = list[list.length - 1];
for(int i = list.length - 1; i > 0; i--) {
list[i] = list[i - 1];
}
list[0] = last;
}
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte [] buffer = new byte[8];
while (true) {
try {
/*
buffer[0] = (byte) mmInStream.read();
shiftRight(buffer);
if((buffer[7]=='<')&(buffer[1]=='>')){
data = ((int) buffer[1] << 8) | ((int) buffer[2] & 0xFF);
*/
}
}catch (IOException e) {
Log.e(TAG, "connection lost", e);
break;
}
}
}
shiftRight()
public static void shiftRight(byte[] list)
{
if (list.length < 2) return;
byte last = list[list.length - 1];
for(int i = list.length - 1; i > 0; i--) {
list[i] = list[i - 1];
}
list[0] = last;
}
感谢您的时间。