Question

Bluetooth LE was added in Android 4.3 but it seems there is no background scanning mode which would wake up an app once it has registered to be notified via a available BLE UUID in proximity.

This exactly seems to be possible via iOS7 and iBeacons API. Does anyone know if there is such a feature in Android 4.3 or if there is a good workaround for periiodically scanning the BLE environment for BLE devices?

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

http://techcrunch.com/2013/09/11/estimote-details-ios-7-ibeacon-support-for-its-contextual-proximity-shopping-devices/

Was it helpful?

Solution

I think there is a workaround as below: You need to implement a service and create thread to while loop to call the mBluetoothAdapter.startLeScan(mLeScanCallback), then you can check whether be trigeed by specific device and further to search specific UUID.

OTHER TIPS

Directly from the android example, you can use a handler:

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

            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
        invalidateOptionsMenu();
    }

In my experience it's best to create a Service that stays running. Important is to only scan for the beacons you are interested in, otherwise you will waste a lot of cpu power parsing beacon messages.

This library can help you wit scanning for just the iBeacons you are interested in: https://github.com/inthepocket/ibeacon-scanner-android

Also, when scanning is not possible (due to Bluetooth off, Location off, permission revoked), you have to restart scanning every time when all needed conditions are met again, here for you will need broadcast listeners.

Conditions to be able to scan:

  • Have a Bluetooth LE chip: any.

  • Have Bluetooth on: any.

  • Have location on: Android 6+.

  • Have location runtime permission: Android 6+.

  • Maximum start 5 scans in 30 seconds: Android 7+.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top