永久蓝牙连接蓝牙

2023-09-07 11:46:23 作者:XX最甜了

我有一个任务写入应用程序,将蓝牙连接到没有移动电话设备和本设备将打磨一批数据后,发送一些数据每秒(一些统计数据)和设备不应该断开。我怎样才能做到这一点?

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?

推荐答案

如果你打开​​一个蓝牙套接字连接,它会继续开放,只要你持有对它的引用既不年底关闭。

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.

此外,在Android中,它可以有时棘手保持对方向变化的引用,因为活动重新启动。一般人把它放在一个服务和发送数据的方式。这是保持连接的快速简便的方法。

Also, in Android, it can be sometimes tricky to maintain the reference because Activities restart on orientation changes. Generally people put it in a Service and send data that way. It's a quick and easy way of maintaining the connection.