Android 蓝牙:配对设备列表
我有一个带有 SPP 配置文件和蓝牙版本 2.1 的蓝牙设备。
我有一个应用程序连接到该设备并与其通信。该设备使用“Just Works”配对技术。
我在某些手机(如三星 Galaxy 平板电脑、Galaxy S)上遇到问题。
问题是用户退出应用程序后,我正在关闭套接字并断开与设备的连接。成功断开连接后,可以观察到该设备的条目已从配对设备列表中删除。
I have a bluetooth device with SPP profile and bluetooth version 2.1.
I have an app which connects to that device and communicates with it. The device uses "Just Works" pairing technique.
I am facing a problem on certain phones like Samsung Galaxy tablet, Galaxy S.
The problem is after the user exits from the app, I am closing the sockets and disconnecting from the device. After successful disconnection, it is observed that the device's entry is removed from the paired devices list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有使用过平板电脑,但我确实为 Android 手机编写了一个使用 SPP 的应用程序。我发现,为了使蓝牙稳定,我必须手动与我想要通信的设备绑定。我们使用下面的代码从应用程序内启动绑定,它应该保留绑定,就像您通过设置菜单手动配对一样。
这是一般流程:
1) 注册一个BroadcastReceiver来监听BluetoothDevice.ACTION_BOND_STATE_CHANGED
2)设备发现后,您应该有一个BluetoothDevice对象。
3)使用反射调用BluetoothDeviceObject上的“createBond”方法
3a) 在打开套接字之前等待绑定状态更改事件
4) 等待绑定状态从 BOND_BONDING 更改为 BOND_BONDED
Inside a BroadcastReciever:
5) 打开套接字并进行通信
您还可以通过反射使用 'removeBond' 方法来删除从配对列表中选择您的设备。
希望这有帮助!
I haven't worked with tablets, but I did write an app that used SPP for Android phones. What I found was that in order to get Bluetooth to be stable, I have to manually bond with the device I want to communicate with. We used the code below to initiated bonding from within the app, and it should preserve the bonding just as if you manually paired through the settings menu.
Here is the general flow:
1) Register a BroadcastReceiver to listen for BluetoothDevice.ACTION_BOND_STATE_CHANGED
2) After device discovery you should have a BluetoothDevice object.
3) Use reflection to call 'createBond' method on a BluetoothDeviceObject
3a) Wait for bond state change events before opening sockets
4) Wait for the bond state to change from BOND_BONDING to BOND_BONDED
Inside a BroadcastReciever:
5) Open sockets and communicate
You can also you the 'removeBond' method via reflection to remove your device from the pairing list.
Hope this helps!
如果配对是由于您的应用程序连接而发生的,我猜测某些设备会将其视为临时配对,并且在连接断开后不会将设备保留在配对列表中。
要将设备保留在配对列表中,您应该通过蓝牙设置菜单手动配对。配对后,您的程序可以连接/断开连接,并且设备将保留在配对列表中。
If the pairing is happening as a consequence of your app connecting, I am guessing that some devices will consider it as a temporary pairing and will not keep the device in the paired list after the connection is disconnected.
To retain the device in paired list you should manually pair through the Bluetooth settings menu. Once paired your program can connect / disconnect and the device will retain itself in the paired list.
我在使用索尼 Xperia X10 时也遇到了同样的问题。我设法通过更改蓝牙设备端的安全级别设置来使其“记住”配对(因为我也在开发该设备)。
我不确定“临时配对”的解释,这取决于制造商,不同的手机对与同一设备的连接有不同的反应没有多大意义。
然而,对我来说,无限制的部分是个问题。通常,当用户在应用程序在后台连接时取消设备配对时,蓝牙堆栈似乎会崩溃。我仍然不知道如何正确管理 ACTION_BOND_STATE_CHANGED 事件。
I also experienced the same problem with a Sony Xperia X10. I managed to make it "remember" the pairing by changing the security level settings on the bluetooth device side (as I am developing the device also).
I am not sure about the "temporary pairing" explanation, that would be manufacturer dependent, it doesn't make much sense that different phones would react differently to a connection with the same device.
However it is the unbounding part that is a problem for me. Typically the Bluetooth stack seems to crash when the user is unpairing a device while the application is connected in the background. I still haven't figure out how to manage the ACTION_BOND_STATE_CHANGED event properly.