蓝牙 Android RFCOMM/SPP 错误处理建议 [英] Bluetooth Android RFCOMM / SPP error handling suggestions
问题描述
我计划在 Android 设备和使用商业蓝牙模块的自定义传感器之间使用一种通信协议.我会使用据说向用户提供简单可靠的数据流,类似于 TCP"的 SPP 配置文件.
I am planning a communication protocol to be used between an Android device and a custom sensor that would use a commercial Bluetooth module. I would use the SPP profile that is said to "provide a simple reliable data stream to the user, similar to TCP."
我对蓝牙技术不太熟悉,对设计这样的协议有一些疑问.
I am not so familiar with the Bluetooth technology and have some questions about designing such a protocol.
首先,我不清楚我是否必须担心传输损坏的数据.底层协议栈是否会保证我从 InputStream
Android 读取的字节与 UART 在传感器端接收的字节相同?我是否必须定义自己的受 CRC 或其他东西保护的数据包,否则会过大吗?
First of all it is not clear to me if I have to worry corrupted data being transferred or not. Will the underlying protocoll stack guarantee me that the bytes I read from the InputStream
Android gives are the same that the UART receives on the sensor side? Do I have to define my own data packages protected with CRC or stuff or would it be overkill?
从 Android 蓝牙文档中我不清楚如果信号很弱并且丢失了一段时间,我的应用程序会发生什么.我是立即得到 IOException
还是平台为我提供了一些隐藏的错误处理和恢复,让我看不到这种短暂的中断?
It is not clear to me from the Android Bluetooth documentation what happens in my application if the signal is weak and is lost for a while. Do I get IOException
at once or the platform provides me some hidden error handling and recovery that would make such short outages invisible to me?
感谢您的建议.
推荐答案
您不必担心 SPP 上的数据损坏,蓝牙提供 2 级 CRC,一种在基带,然后一种在 L2CAP 级别,两者都具有针对检测到的任何损坏的重传机制 - 因此应用程序将只收到良好的数据包.
You need not worry about data corruption over SPP , Bluetooth provides 2 levels of CRC , one at the baseband and then one at the L2CAP level, Both have retransmission mechanism for any detected corruptions - thus applications will receive only good packets.
关于第二个问题 - 在您连接的 Socket 流上是的,如果底层蓝牙连接断开,您将收到 IOExceptions,Android 无法处理自动恢复 - 通常应用程序需要检测断开连接的原因并尝试重新连接视情况而定.因此,如果您想从上次成功接收/发送的点开始继续数据交换,那么您的应用程序应该维护某种序列号和轻协议以实现恢复.另一种选择是在重新连接时简单地从数据交换开始(因此这取决于您的用例)
About the second question - Yes on the Socket streams that you are connected on you will receive IOExceptions if the underlying Bluetooth connections get disconnected, Android cannot handle automatic restore - typically applications need to detect the reason for disconnection and try re-connect as applicable. So if you want to continue your data exchange from the point you last successfully received / sent then for it your application should maintain some sort of sequence number and light protocol to achieve a resume. The other option is to simply start from the beaning of data exchange upon a re-connect (So it depends on your usecase)
这篇关于蓝牙 Android RFCOMM/SPP 错误处理建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!