永久蓝牙连接
我有一个编写应用程序的任务,该应用程序将通过蓝牙连接到非移动电话设备,该设备每秒会发送一些数据(一些统计数据),并且设备在打磨一批数据后不应断开连接。我该怎么做?
I have a task to write application, that will connect to not-mobile-phone device by blue tooth and this device will send some data every second(some statistics) and device shouldn't disconnect after sanding one batch of data. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您打开蓝牙套接字连接,只要您保留对它的引用并且两端都不会关闭,它就会保持打开状态。
一个简单的示例是 蓝牙聊天 示例。它展示了如何连接两部手机,还展示了如何维持连接并检查连接是否仍然有效。
此外,在 Android 中,维护引用有时可能很棘手,因为
Activities
在方向更改时会重新启动。通常人们将其放入Service
中并以这种方式发送数据。这是维持连接的快速且简单的方法。If you open up a bluetooth socket connection, it will stay open as long as you hold the reference to it and neither end closes.
A simple example is the Bluetooth Chat example. It shows how to connect two phones, but it also shows how to maintain the connection and check if it's still alive.
Also, in Android, it can be sometimes tricky to maintain the reference because
Activities
restart on orientation changes. Generally people put it in aService
and send data that way. It's a quick and easy way of maintaining the connection.