问题
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