Android - Cannot find a device in onScanResult callback after successful connection

假装没事ソ 提交于 2021-02-10 16:48:22

问题


I'm scanning for BLE devices with my mobile. Whenever I get a callback of onScanResult I check if peripheral is iBeacon with specific minorId, if it is, I do device connection. Problem is that after successful connection onScanResult returns different devices around me, but none of them are the ones I just connected to. It's crucial for me to find connected devices in onScanResult. Is this normal behaviour? What could cause this? This happens when I connect to device - and only then.


回答1:


This is the default behaviour according to the Bluetooth specification. For Bluetooth Low Energy devices, there are 6 predefined states:-

  • Standby State
  • Advertising State
  • Scanning State
  • Initiating State
  • Connection State
  • Synchronization State

In the spec, it is stated: "The Link Layer state machine allows only one state to be active at a time". This is demonstarated in the state machine below:-

This is why once you are connected to the advertising device, it switches state to "Connection" and remains in that state. If you had access to the device's code you could force it to advertise after connection, but not all devices support this feature.

For more information, check the Bluetooth Specification v5.1, Vol 6, Part B, Section 1: Link Layer States.



来源:https://stackoverflow.com/questions/57969346/android-cannot-find-a-device-in-onscanresult-callback-after-successful-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!