如何通过蓝牙一起读取所有字节?
我有一个使用蓝牙从其他设备接收一些数据(字节)的应用程序。一切都很顺利,但是我在接收所有字节时遇到了一个小问题。收到字节后,我将它们显示在 Toast 上只是为了测试它们。当另一个设备一起发送 10 个字节时(例如:“ABCDEFGHIJ”),程序将仅获取第一个字节“A”并将其显示在 Toast 上,然后进入第二次迭代并读取其他 9 个字节并显示“ BCDEFGHIJ”在吐司上。这是我的代码:
byte[] buffer = new byte[1024]; // Read 1K character at a time.
int bytes = 0; // Number of bytes.
while(true)
{
try
{
// Read from the InputStream.
bytes = bInStream.read(buffer);
// Send the obtained bytes to the MainActivity.
mainActivityHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
catch(IOException e)
{
connectionLost();
break;
}
}
在 MainActivity 中,我有:
// The Handler that gets information back from the BluetoothManager.
private final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer.
String readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(MainActivity.this, readMessage, Toast.LENGTH_SHORT).show();
break;
// ...
}
}
};
如何一起接收所有字节?!
I have an application that uses the bluetooth to receive some data (bytes) from other device. everything is going well, but I have a small issue on receiving the bytes all together. After receiving the bytes I show them on a Toast just to test them. When the other device sends 10 bytes together (for example: "ABCDEFGHIJ"), the program will take the first byte "A" only and show it on a Toast, then go to the second iteration and read the other 9 bytes and show "BCDEFGHIJ" on the Toast. Here is my code:
byte[] buffer = new byte[1024]; // Read 1K character at a time.
int bytes = 0; // Number of bytes.
while(true)
{
try
{
// Read from the InputStream.
bytes = bInStream.read(buffer);
// Send the obtained bytes to the MainActivity.
mainActivityHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
catch(IOException e)
{
connectionLost();
break;
}
}
In the MainActivity, I have:
// The Handler that gets information back from the BluetoothManager.
private final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer.
String readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(MainActivity.this, readMessage, Toast.LENGTH_SHORT).show();
break;
// ...
}
}
};
How can I receive all the bytes together?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
嗯,罪魁祸首很可能是你发送消息的方式。您的接收没有问题,它将接收与写入一样多的字节(最多 1024)。
如果您无法控制消息的发送方式,您可能可以一次读取一个字节,然后在遇到预定义的终止符时向您发送一条处理程序消息。例如:“ABCDEFGHIJ#”,其中 # 是终止符。
Mmm most likely the culprit is in the way you're sending the messages. Your receive has no problems, it will receive as many bytes (up to your 1024) as it is written to.
If you have no control over the way the messages are sent you can probably read one byte at a time and then send a handler message you when you hit a predefined terminator. Ex: "ABCDEFGHIJ#" where # is the terminator.
蓝牙连接是基于流的,而不是基于数据包的。不保证或尝试保留分组化。因此,任意数量的写入都可以导致任意数量的读取,只要保证字节流是正确的即可。如果需要检测数据包,则需要提供自己的数据包结构来包装数据。例如,在每个数据包之前添加一个长度字段,以便您可以在接收端进行重建。
The bluetooth connection is stream based, not packet based. There is no guarantee or attempt to preserve packetization. So any number of writes can result in any number of reads, just the stream of bytes are guaranteed to be correct. If you need to detect packets, you need to provide your own packet structure to wrap your data. For example, add a length field before each data packet so you can reconstruct on the receiving side.
@broody 接受的答案是正确的。但如果数据本身包含“#”,则可能难以获取数据。因此,根据我的观点,最好的方法是附加 '\n' 后跟 '\r' (或任何其他不太可能作为数据的字符 参考 ASCII 表)在将数据发送到 Android 应用程序的设备中。它仅充当换行并标记数据结束。
例如: ABCDEFGH\n\r
那么你的代码可以是这样的:
希望它有帮助
问候
The accepted answer from @broody is correct. But if the data itself contains '#', it might be difficult to fetch the data. So the best approach according to me is appending '\n' followed by '\r' (or any other characters which are unlikely to feature as data Refer ASCII Table) in the Device which sends data to your Android app. It just acts as Line feed and marks end of data.
Eg: ABCDEFGH\n\r
Then your code can be something like this :
Hope it helps
Regards
要一起读取所有字节,您需要在代码中用“\n”或“\r”或“\r\n”分隔数据。
例如:如果您想通过蓝牙将数据从 Arduino 发送到 Android 应用程序:
(Arduino 代码):
现在,要读取发送的数据(变量“count”的值),这里是 Android Studio 的代码:
这是:(在 onCreate() 方法中):
To read all bytes together you need to separate the data by "\n" or "\r" or "\r\n" in your code..
For example: If you want to send data from Arduino to Android app via Bluetooth:
(Arduino code):
And now, to read the sent data (value of variable 'count'), here is the code of Android Studio:
And this:(in onCreate() method):
以上答案都不适合我。 Thread.sleep(1000) 给了我解决方案
None of the above answers worked for me . Thread.sleep(1000) gave me the solution
处理长于缓冲区的消息(可能很危险,因为如果不清除就会占用内存)的一种方法是分块处理它们:
A way to handle longer-than-buffer messages (can be dangerous because can take up your memory if never cleared) is to handle them in chunks: