Android BLE Beacon scanning
Asked Answered
A

3

14

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/

Afterthought answered 19/9, 2013 at 8:15 Comment(0)
F
10

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.

Firenze answered 31/10, 2013 at 15:21 Comment(3)
I am about to implement exactly that. But instead of running a continuous service I will probably create a broadcast receiver and wake it up every 1 minute. I used this before Geofences were officially added to teh play services. The problem with that is obviuously that the information / scanning is not shared among apps and it iwll be less efficient. But should work.Afterthought
Your suggestion is ok and could be used in some cases but it's more of a kind of naive implementation. Problem with this approach will definitely be power consumption. Note this line from the BLE documentation page "To find BLE devices, you use the startLeScan() method. This method takes a BluetoothAdapter.LeScanCallback as a parameter. You must implement this callback, because that is how scan results are returned. Because scanning is battery-intensive..." I think there are probably better ways to do this something along the lines of registering for system wide events like in geofencing APIIridotomy
+1 For reminding about docs saying it's battery-intensive. You also can connect to a device you haven't received advertising yet and set autoConnect to true. (The BluetoothDevice can be created like this: BluetoothAdapter.getDefaultAdapter().getRemoteDevice("20:00:A0:60:DD:05");) However, it only connects if you start scanning. There seems not to be some kind of background scanning yet. Maybe they add that later.Bossuet
W
1

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();
    }
Worldlywise answered 4/12, 2014 at 15:28 Comment(1)
Do you know perhaps if I can detect iBeacons with this code, or do I need extra libraries?Hezekiah
T
0

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+.

Tauto answered 1/2, 2017 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.