简体   繁体   English

如何从 Android 连接到配对的 ble 设备?

[英]How to connect to paired ble device from Android?

I have already paired peripheral ble device (iPhone) with my Android phone.我已经将外围设备 (iPhone) 与我的 Android 手机配对。 Now I want from my Android code connect to this iPhone.现在我想从我的 Android 代码连接到这台 iPhone。

I received list of paired devices by mBluetoothAdapter.getBondedDevices() and try to connect to the proper one but connection fails ( GATT_ERROR ).我收到了mBluetoothAdapter.getBondedDevices()的配对设备列表,并尝试连接到正确的设备,但连接失败( GATT_ERROR )。

I know the mac address is randomizing but on the other hand this iPhone is paired and I am authorized to connect it.我知道 mac 地址是随机的,但另一方面,这部 iPhone 已配对,我有权连接它。 Can I do this?我可以这样做吗?

In Bluetooth Low Energy, Pairing and Bonding are two different things as follows:-在低功耗蓝牙中,配对和绑定是两个不同的东西,如下所示:-

  • Pairing is the process of encrypting the connection and exchanging security information.配对是加密连接和交换安全信息的过程。
  • Bonding is the process of saving the exchanged security information so that the device is properly detected (if it has a random address) and can immediately be paired to without further exchange of information.绑定是保存交换的安全信息的过程,以便正确检测设备(如果它具有随机地址)并可以立即配对,而无需进一步交换信息。

In your case, if the devices are "paired" then this means that they are already in an active connection (This is different to classic Bluetooth where a connection and pairing are mutually exclusive).在您的情况下,如果设备已“配对”,则这意味着它们已经处于活动连接中(这与连接和配对互斥的经典蓝牙不同)。 If this is the case, then try to list active connections to make sure that the iPhone is not one of them.如果是这种情况,请尝试列出活动连接以确保 iPhone 不是其中之一。

If you are "bonded" and not paired, then this means that there is no active connection and another one needs to be created.如果您是“绑定”且未配对,则这意味着没有活动连接,需要创建另一个连接。 You will need to make sure that your peripheral device is advertising and that the Android device can scan and detect it.您需要确保您的外围设备正在做广告并且 Android 设备可以扫描并检测到它。 Once it is detected through the scan, connect to the iPhone and this should work without an issue.通过扫描检测到它后,连接到 iPhone,这应该可以正常工作。

This might be a useful reference:-这可能是一个有用的参考:-

Should one create a bond with a Bluetooth LE device 是否应该与蓝牙 LE 设备建立绑定

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM