在android中接收蓝牙传输的正确方法(停止位)
我正在尝试通过蓝牙从传感器获取一些数据。传感器正在向我发送 8 位数据包。问题是数据的长度不同(8 位的几倍),所以我无法确定数据的结尾。
现在我将字符串附加在一起并检查回车符,但问题是这是特定于我的传感器的(在字符串末尾我有回车符)。
一次测量的传感器数据如下:
547.8 5.688 22.40 .0000 .0000 .0000 \r\n
547.8 855.688 22.40 .0000 .0000 .0000 \r\n
我每秒将此传感器数据发送到 Android 设备。 Android 设备正在获取 8 位块,但我无法检测到停止位。
有没有一种方法可以检测停止位或我现在获得一个输入的全部数据的东西?
我正在接收蓝牙传输,就像 Android 蓝牙聊天示例一样。
谢谢你, 托尼
I'm trying to get some data from sensor via bluetooth. The sensor is sending me 8 bit packages. The problem is that data has different length (few times 8 bit) so I can not determine the end of data.
Now I'm appending string together and checking for carriage return, but the problem is that this is specific for my sensor (in the end of string i have carriage return ).
The sensor data for one measuring is like that:
547.8 5.688 22.40 .0000 .0000 .0000 \r\n
547.8 855.688 22.40 .0000 .0000 .0000 \r\n
I'm sending this sensor data every second to android device. Android device is getting 8 bit chunks, but I cannot detect the stop bit.
Is there a way to detect stop bit or something that I now that I got the whole data for one input?
I'm receiving bluetooth transfer like in android bluetooth chat example.
Thank you,
Toni
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据中总是需要有一种方法来找到每个块的末尾,并且人们只需继续读取直到找到该点。通常,这是一个完整的行(以换行符结尾),如下所示,或者它可能是前一个或两个字节(或字符)中的计数值。人们不能依赖一次读取或类似读取的数据。
请注意,“停止位”是在物理串行电缆上使用的东西,位于每个字节之后,因此这里对您没有用处。
There always needs to be a way in data to find the end of each block, and one just keeps reading until that point is found. Often that is a complete line (ends with newline) as here, or it could be a count value in the first one or two bytes (or characters). One can't rely on the data coming in one read or similar.
Note that a "stop bit" is something used on physical serial cables and comes after every byte so if of no use to you here.