我已经设置了一个套接字来接收来自蓝牙设备的数据。接收缓冲区设置为在退出线程之前收集8个字节的数据,但缓冲区不会前进以存储下一个数据字节。我将缓冲区设置为8并循环直到缓冲区已满。
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = btSocket.getInputStream();
tmpOut = btSocket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
InputStream mmInStream = btSocket.getInputStream();
byte[] readBuffer = new byte[8];
int read = mmInStream.read(readBuffer);
while(read != -1){
Log.d(TAG, " SizeRR " + read);
read = mmInStream.read(readBuffer);
}
} catch (IOException e) {
break;
}
}
}
Log.d(SizeRR 1)读取8次
答案 0 :(得分:0)
InputStream.read()旨在返回一个抽象int,因此它将始终打印0-255之间的数字。
尝试使用read(byte [] buffer)方法,它从流中写入buffer.length数据量并将数据直接复制到数组中:
public void run() {
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
InputStream mmInStream = btSocket.getInputStream();
byte[] readBuffer = new byte[8];
mmInStream.read(readBuffer);
} catch (IOException e) {
break;
}
}
}