蓝牙套接字连接在 Android 上如何工作?
我是蓝牙新手。据我所知,在 Android 上我可以通过 BluetoothSocket 连接作为客户端连接到另一个蓝牙设备。如何处理收到的数据?数据以什么格式从服务器发送到客户端?
I am new to bluetooth. I understand on Android I can connect to another bluetooth device as a client through a BluetoothSocket connection. How do I handle data that is received? In what format does data get sent from the server to the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回顾一下你的问题:
处理数据的理想方法是使用线程(尽管可以使用服务)。通过无限循环运行的单个连接线程将尝试尝试从套接字连接提供的流中读取数据。
数据通过字节(特别是字节数组)从服务器发送到客户端。如果您知道所接收数据的类型,则有很多函数可以将此数据转换为所需的变量类型。
有关蓝牙如何在设备之间工作的完整示例,我强烈建议您查看 BluetoothChat 示例应用程序。还有蓝牙概述。
Going through your questions:
The ideal way to handle data is with threads (although it is possible to use a service). A single connected thread running through an infinite loop will attempt to try to read data from the stream that is provided by the socket connection.
Data is sent from server to client via bytes (specifically arrays of bytes). If you know the type of data being received, there are plenty of functions to convert this data to the desired variable type.
For a complete example of how bluetooth works between devices, I'd highly recommend taking a look at the source of the BluetoothChat Sample application. There's also the Bluetooth overview.