BluetoothAdapter won't stop scanning for BLE devices

Every time you call getLeScanCallback you create new instance of ScanCallback and lost reference to previous instance.

stopScan stops ongoing scan regardless what instance you pass BUT if it is a different instance (than the one you used to start) it will not remove old instance of callback so still some events will be delivered to it before it stops.


Scan results will not stop immediately, but will eventually stop. There is a queue of pending results that you can flush with:

scanner.flushPendingScanResults();

But even then, the documentation is clear that it will send whatever it has found to the callback. This happens even if you have stopped scanning. Poor API design, but it is what it is.

The easiest I think is to set a flag to ignore the scan results after you have stopped them.


I haven't noticed any issues with your code, it seems to be fine. Perhaps there's an issue with your getLeScanCallback() method?

Instead of having a method to return your ScanCallback, you could declare it as a member variable so you would have leScanCallback = new ScanCallback() and then you could just refer to this member variable instead when trying to stop the scan.

bluetoothAdapter.getBluetoothLeScanner().stopScan(leScanCallback);

Depending on what version of Android you're supporting, it might be worthwhile adding the startLeScan and stopLeScan methods which Rajesh Panchal suggested for pre Lollipop Android devices.