4

I've this code to begin to scan BLE devices:

Log.i("timeChar", "Begin");
customBluetoothManager.scanLeDevice(true);

Then scanLeDevice() are invoked, that manages the Bluetooth Adapter for start the BLE scan:

public void scanLeDevice(final boolean enable) {
        mHandler = new Handler();

        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

And now, the callback for each device that is found through the scanning:

// Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi,
                                     byte[] scanRecord) {

                    String name = device.getName();
                    if (name != null && name.compareTo(bluetoothDeviceName) == 0) {
                            Log.i("timeChar", "Device found");
                    }
                }
            };

Each time that I'm going to scan my BLE device (that has a Peripheral role), it takes around 11 seconds to detect it...

05-31 17:32:39.139 27545-9668/app I/timeChar: Begin
05-31 17:32:50.149 27545-27545/app I/timeChar: Device found

Is there any way to reduce this time?

3
  • 1
    While self promotional I found this article interesting regarding BLE: atmosphere.anaren.com/wiki/… Commented May 31, 2017 at 15:54
  • Thanks for that wiki! I'll take a look :)
    – adlagar
    Commented Jun 1, 2017 at 8:11
  • this link does not work. Any new link ?
    – matdev
    Commented Jan 25, 2021 at 17:20

2 Answers 2

3

The BLE scan and device advertisement is done at intervals. If the interval between adverts is long, let s say a second, then android might miss it. In order to fix this from the app side:

//global ScanSettings settings;
ScanSettings.Builder settingBuilder = new ScanSettings.Builder();
settingBuilder.setScanMode(SCAN_MODE_LOW_LATENCY);
settings = settingBuilder.build();

which you later pass to the scan method

mBluetoothAdapter.getBluetoothLeScanner().startScan(null, settings, mLeScanCallback);
1

In order to do so you should take into account:

  1. Defined interval set in the BLE Beacon. In order to save battery you can define the time interval between each packet sent by the BLE Beacon (from 100 ms to some seconds). Therefore first check what is the time interval of your Beacons.

  2. The time set in Android OS to indicate you that a new packet has been received (callback function). As indicated by Marcin you can do so as easy as:

scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY ).build();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.