Decreased BLE startScan detected devices on Android 5.0 Lollipop
Asked Answered
A

5

20

Short version:

In my tests with Android 5.0 Lollipop I have noticed android.bluetooth.le.BluetoothLeScanner detects BLE devices less frequently than Android 4.4 KitKat. Why is this and is there an alternative?

Long version:

I am developing an Android application, specifically for the Nexus 7 tablet, that focuses on detecting Bluetooth Low Energy (BLE) devices. The app is mainly interested in the RSSI value of the beacons, to determine their proximity to the tablet. This means I won't need to connect to the BLE device, since the RSSI value is passed to the scan callback when the device is detected.

In Android 4.4 KitKat, when I call BluetoothAdapter.startLeScan(LeScanCallback), my callback gets called only ONCE for every detected BLE device. (I have seen some discussions claim that this behaviour can differ per device) However, I am interested in the constantly changing RSSI value, so the currently recommended way is to continuously do startLeScan and stopLeScan with a set interval (250ms in my case):

public class TheOldWay {

    private static final int SCAN_INTERVAL_MS = 250;

    private Handler scanHandler = new Handler();
    private boolean isScanning = false;

    public void beginScanning() {
        scanHandler.post(scanRunnable);
    }

    private Runnable scanRunnable = new Runnable() {
        @Override
        public void run() {
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

            if (isScanning) {
                adapter.stopLeScan(leScanCallback);
            } else if (!adapter.startLeScan(leScanCallback)) {
                // an error occurred during startLeScan
            }

            isScanning = !isScanning;

            scanHandler.postDelayed(this, SCAN_INTERVAL_MS);
        }
    };

    private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            // use the RSSI value
        }
    };

}

Essentially this gives me the required results, but this process is very resource intensive and eventually leads to an unresponsive bluetooth adapter.

For these reasons I upgraded my Nexus 7 to Android 5.0 Lollipop, to see whether my BLE issues would be fixed. In Lollipop BluetoothAdapter.startLeScan(LeScanCallback) is deprecated and replaced with a new API that allows for some more control over the scanning process. From my first tests, it appears startScan does not continuously call my callback (on my Nexus 7) when the RSSI values change, so I still need to use the startScan / stopScan implementation:

@TargetApi(21)
public class TheNewWay {

    private static final int SCAN_INTERVAL_MS = 250;

    private Handler scanHandler = new Handler();
    private List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    private ScanSettings scanSettings;
    private boolean isScanning = false;

    public void beginScanning() {
        ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
        scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
        scanSettings = scanSettingsBuilder.build();

        scanHandler.post(scanRunnable);
    }

    private Runnable scanRunnable = new Runnable() {
        @Override
        public void run() {
            BluetoothLeScanner scanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();

            if (isScanning) {
                scanner.stopScan(scanCallback);
            } else {
                scanner.startScan(scanFilters, scanSettings, scanCallback);
            }

            isScanning = !isScanning;

            scanHandler.postDelayed(this, SCAN_INTERVAL_MS);
        }
    };

    private ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);

            int rssi = result.getRssi();

            // do something with RSSI value
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);

            // a scan error occurred
        }
    };

}

As you can see, I have configured the scanner using the ScanSettings class, which allows you to set the scanMode. I use ScanSettings.SCAN_MODE_LOW_LATENCY, which has the following documentation: "Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground." Sounds exactly like what I want, but unfortunately I only get a beacon detect every 15 - 30 seconds, where the KitKat version shows me the same beacon every 1 - 2 seconds on this scan interval.

Do you have any idea what could be the reason for this difference? Am I missing something, maybe some new settings? Are there alternative ways of doing the above?

Thanks a lot in advance!

Abel

PS: I wanted to include more links to resources I've used, but I don't have the rep points for it yet.

Anomaly answered 24/11, 2014 at 18:5 Comment(4)
In Lollipop "startScan does not continuously call my callback (on my Nexus 7) when the RSSI values change". But does it get called again at least every once in a while or only once? Sorry, I don't have a Nexus 7 to test. In my Nexus 5 it gets called around every second in SCAN_MODE_LOW_POWER.Quinine
@Anomaly I nearly got the same bad result as yours, I'm using One-plus one, CM ROM 5.0.2, some times the scanning called by over 30 seconds, but some times it's like dead. Do you have any solution now?Dorseydorsiferous
I redid the testing, my One plus one phone only scan 2 times, then that callback would never be fired any more.Dorseydorsiferous
"unfortunately I only get a beacon detect every 15 - 30 seconds, where the KitKat version shows me the same beacon every 1 - 2 seconds on this scan interval" what do you mean - it means that you have 5 tags and in Android 4 you get a callback one for each beacon with 1-2 interval and in Android 5 the interval is 15-30 seconds or what?Loidaloin
D
6

I have gotten very different results with a Nexus 5 running the new Android 5.0 scanning APIs. Detections of BLE packets came in at near real time when using SCAN_MODE_LOW_LATENCY, at every 100ms for BLE beacons transmitting at 10Hz.

You can read the full results here:

http://developer.radiusnetworks.com/2014/10/28/android-5.0-scanning.html

These tests are based off of running the open source Android Beacon Library 2.0's experimental android-l-apis branch here.

It is not obvious what the difference is in your test results, but it is possible that starting and stopping scanning is changing the results.

EDIT: it is possible the hardware is the difference. See a report of similar timings on the Nexus 4: https://github.com/AltBeacon/android-beacon-library/issues/59#issuecomment-64281446

Distil answered 24/11, 2014 at 20:41 Comment(6)
Thanks for the suggestions, @davidgyoung! Any updates on this situation by any chance? :)Anomaly
I have not been able to reproduce the symptoms you describe, so I don't know anything more than described above.Distil
hi david could you please help me to add scan interval in lescan of bluetooth in android devices using service.Mitsue
@johny-kumar, please post a new questionDistil
here is the url of my problem #28215716Mitsue
@Distil I'll report the case of mine, One plus one phone, using latest(Feb 4th 2015) CM android 5.0 ROM, tested all settings in ScanSettings, the behavior is: only scanning 2 times, then that callback never fired, like dead. beside nexus 5, do you have other android device tested and recommend to the BLE application develop for now?Dorseydorsiferous
D
5

I don't have 50 reputation for a comment yet, so bear with me, this comment will be in the form of an answer. In your code, shouldn't this part:

if (isScanning) { scanner.startScan(...)

be this instead:

if (!isScanning) {
 scanner.startScan(...)

Because following your code, you're calling stopScan() before starting a scan. It may not have a direct effect on the result if the stopScan() method is idempotent/safe. But you know, for the sake of code intelligibility you should edit the question. And do the same to your code, sometimes byzantine things are at play ;)

Have you tried larger values for SCAN_INTERVAL_MS? If yes, how large?

Deserve answered 26/2, 2015 at 16:0 Comment(1)
Thanks, I've edited my question. My code is correct. :) Yes, I've tried a scan interval up to 1000 ms, but that just means you get the scan results less frequently. When using a scan interval < 200, the bluetooth adapter is not always able to detect devices.Anomaly
B
2

I have experienced very similar results with my Nexus 4, in both KitKat and Lollipop.

With KitKat the bluetooth adapter also eventually went unresponsive; at first I though that it could be related to a short scan interval (200ms) but increasing that number to even a second didn't help, in that matter I found that, when unresponsive disabling and enabling the adapter programmatically, sometimes solves the problem. Unfortunately I can't say that it works all the time.

Now with Lollipop, in which I had high hopes to solve this issues, I experienced the same behaviour that you describe. I also had to use the startScan / stopScan implementation, getting similar results regarding the detection times. Sadly, I haven't found a work around to get results more quickly.

Based on what you describe I suppose it could be a hardware issue, even though the Nexus 7 and Nexus 4 are from different manufacturers (Asus and LG).

I know I'm not providing much help here besides trying to answer your question about you missing something; I don't think so, I think the problem is something like the hardware or the bluetooth API that still doesn't behave the way it should across different devices.

Bobcat answered 25/11, 2014 at 15:50 Comment(1)
Thanks, @marp. For now I will keep working with the startScan / stopScan procedure.Anomaly
C
1

Beyond API 21 android uses SCAN_MODE_LOW_POWER by default. SCAN_MODE_LOW_POWER

Try SCAN_MODE_BALANCED and see if it gets better.
SCAN_MODE_BALANCED

Carhop answered 27/1, 2016 at 12:52 Comment(0)
G
0

if you search for BW13_DayOne_Session1 Bluetooth Advanced on google, you will find a pdf document that gives you the latencies for devices based on the settings for discovery (see page 8). I'm guessing your problem has to do with these timings. You can verify by figuring out the advertising configuration for the device you are testing (Adv Int, Duty Cycle) then figure out what the API settings are doing for configuring the scan interval, etc. Once you have these, you can then use that table to interpolate to see if your getting the results you expect.

I know this is a software site, but often when interfacing with hardware you need to know the protocol otherwise your shooting in the dark.

Gulosity answered 27/2, 2015 at 19:7 Comment(2)
Could you post that pdf? I am unable to find it.Fraught
The only Google hits for that search term are this page and mirrors of it!Befitting

© 2022 - 2024 — McMap. All rights reserved.