问题
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.
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?
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?
Thanks for any advice.
回答1:
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.
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)
来源:https://stackoverflow.com/questions/14498530/bluetooth-android-rfcomm-spp-error-handling-suggestions