Difference in Bluetooth Check and Discovery on Android
These methods apply to different versions of Bluetooth. Which one to use depends on which device you have.
Classic Bluetooth is used BluetoothAdapter.startDiscovery()
to find discoverable devices.
Low-level Bluetooth support was added in API level 18 it uses BluetoothAdapter.startLeScan(ScanCallback)
. API level 21 replaces this with BluetoothLeScanner.startScan()
.
See this example code on how to scan LE devices. Q onLeScan
if you find that the device is just calling scanLeDevice(false);
.
The onLeScan callback does not check if the scan was stopped. You have to give the command stopLeScan()
yourself.
source to share
I personally use the startDiscovery () method for the BluetoothAdapter and I am using the broadcast receiver to see if I have a scan result, scan stopped, etc.
BroadcastReceiver:
BroadcastReceiver scanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase( BluetoothDevice.ACTION_FOUND)) {
// device found
} else if (action.equalsIgnoreCase(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
// discoveryFinished
} else if (action.equalsIgnoreCase(
BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
// discoveryStarted
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
activity.registerReceiver(scanReceiver, filter);
You must register this broadcast receiver before starting the search.
source to share