user3173991
user3173991

Reputation: 51

Android - Would LeScanCallback of an App get called if the App goes onPause (to the background)?

I have been reviewing the BLTE sample code on http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

public class DeviceScanActivity extends ListActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private Handler mHandler;

    // Stops scanning after 10 seconds.
    private static final long SCAN_PERIOD = 10000;
    ...
    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);
                }
            }, SCAN_PERIOD);

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

    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi,
                byte[] scanRecord) {
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   mLeDeviceListAdapter.addDevice(device);
                   mLeDeviceListAdapter.notifyDataSetChanged();
               }
           });
       }
    };

Now, I note that this code gives a LeScanCallback when invoking startLeScan.

Say this App gets through the startLeScan. And then this App goes to onPause(), say another App goes to the foreground. And the stopLEScan is never called.

At this point, does the Android continues to do LE scan?

At one point of the startLeScan, it turns on the actual hardware. But the App has put onPause() and the hardware might continue to be on.

Upvotes: 3

Views: 2036

Answers (1)

Ash Eldritch
Ash Eldritch

Reputation: 1514

Yes, the scan continues to run even when your app is in the background, and your onLeScan() callback will get called if it finds a bluetooth device.

This was easy to test by just starting the scan then exiting the app via the home button, turning on my bluetooth peripheral, and watching the logs. You can also drop a break point in your callback, which will get hit even if your app is not in the foreground.

Scanning drains battery, so depending on what you're trying to accomplish, best practice would be to stop the scan in your onPause() mBluetoothAdapter.stopLeScan(mDeviceFoundCallback) and if need be restart the scan in your onResume() mBluetoothAdapter.startLeScan(mDeviceFoundCallback).

If for some reason you need long-running background Bluetooth activity (e.g. you're implementing the Proximity Profile and need to regularly check signal strength) then that functionality should go into a Service implementation, as shown in the doc you link to: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

Upvotes: 1

Related Questions