Ble scanning callback only get called several times then stopped

前端 未结 2 1342
终归单人心
终归单人心 2020-12-15 10:54

I have 2 phones with Android 5.0.2, they both installed the latest Radius Beacon\'s App: Locate Beacon, meanwhile, I turned on 2 IBeacon sender, and can see

2条回答
  •  既然无缘
    2020-12-15 11:46

    Different Android devices behave differently when scanning for connectable BLE advertisements. On some devices (e.g. the Nexus 4), the scanning APIs only get one callback per scan for transmitters sending a connectable advertisement, whereas they get a scan callback for every advertisement for non-connectable advertisements. Other devices (e.g. the Nexus 5) provide a scan callback every single advertisement regardless of whether it is connectable.

    The Locate app you mention uses the open source Android Beacon Library to detect beacons. It is built on top of the same scanning APIs you show in your question, but it gets around this problem by defining a scan period (1.1 seconds by default in the foreground) and stopping and restarting a scan at this interval. Stopping and restarting the scan causes Android to send a new callback.

    A few other notes here:

    • This issue of getting multiple scan callbacks for connectable devices applies to both the 4.x and 5.x scanning APIs.

    • It is unclear whether the difference in delivering scan callbacks for connectable advertisements on different devices is due to Android firmware differences or bluetooth hardware chipset differences.

    • There doesn't seem to be a way to detect if a device requires a scan restart to get additional callbacks for connectable advertisements, so if you are targeting a wide variety of devices, you need to plan to stop and restart scanning.

    • Using Android's raw scanning APIs is a great way to understand how BLE beacons work. But there are lots of complexities with working with BLE beacons (this is just one example) which is why using a SDK like the Android Beacon Library is a good choice to keep you from pulling your hair out.

    Full disclosure: I am the author of the Locate app in the lead developer on the Android Beacon Library open source project.

提交回复
热议问题