Difference in Bluetooth Check and Discovery on Android

There are two approaches in Android to find out Bluetooth devices. 1. Using bluetoothAdapter.startScan 2. bluetoothAdapter.discover

which approach is better.

Second question, In the onLeScan callback, how to check if the scan has stopped.

+3


source to share


3 answers


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.

+3


source


startScan()

will scan for LE devices, startDiscovery()

for regular Bluetooth devices.



As far as I know, startLeScan()

either startScan()

will be checked as long as methods are called stopLeScan()

or stopScan()

, and you have to call them.

0


source


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.

0


source







All Articles