Search code examples
androidbluetoothrssi

how to detect the BLE device has gone without connecting to it in Android


I am working for a android BLE library for our team, the library is developed with 4.3 native framework, I try to keep a table of the found BLE device, and send a notification when I find a new BLE device who is not in my found device table, instead of informing each time when the device is found (default behaviors by android BLE framework), the problem is I can't detect the device is gone, in consideration of that I don't want to establish the BLE connection with it. Anyone can help me out? I suppose that there is some way to do this, like the iOS framework, u can be informed when the device is gone without connecting to it.


Solution

  • The basic strategy to "undiscover" a device is to keep track of the last time a device was discovered and once every second or so iterate through your list of devices. If the current time minus the last time the device was discovered is greater than some time delta (I use 12.5 seconds) then you consider the device undiscovered and remove it from your list.

    Some gotchas with this:

    • The minimum time it takes to undiscover a device is 12.5 seconds, which is pretty long if you sit down and count it out. You can try to use lower time delta thresholds but I've found that with some phones this results in a lot of false undiscoveries, because legitimate time in between discoveries can take a while.
    • You should not scan continuously, but rather at most do it in pulses of 5-10 seconds with small breaks in between.

    I've also heard of people using RSSI. Low RSSI means you undiscover it, but I experienced a lot of false positives with this approach as well.