Android Bluetooth device scan/discovery for CLassic and Low Energy Devices sequentially

3.9k Views Asked by At

I am developing an Android app which searches for Classic and Low Energy Bluetooth devices such that when I press "search" button it will show me all Bluetooth devices (low energy and classic) in range. Since classic BT discovery and LE scanning are different things, I have to implement them separately and combine them in one function such that

searchFirstLowEnergyThenClassic() or searchFirstClassicThenLowEnergy()

In order to implement this, I have to know when the discovery/scanning ends so that I immediately start scan/discovery for other technology.

Here is my implementation:

  1. Started Classic BT discovery
  2. Received BluetoothAdapter.ACTION_DISCOVERY_FINISHED
  3. Started BLE Scaning -> onReceive action equals(ACTION_DISCOVERY_FINISHED)
  4. Stop search when BLE Scan ended

This looks ok but there is a problem when I extend the behavior. When I want search, I start searching first with LE scan or Classic discovery based on the last connected technology. For example if last time the device is connected to a Classic BT device, searchFirstClassicThenLowEnergy() is run. Otherwise, searchFirstLowEnergyThenClassic().

So as you might guess, it gets more complicated. For example, when the Classic BT discovery ends, the app should know whether the search ended or it should proceed with LE scan.

There is also this issue. When the user stops search during the scan/discovery of first technology, it will recieve BluetoothAdapter.ACTION_DISCOVERY_FINISHED but it shouldn't start LE scan since the search is terminated by user.

I implemented this using some flags (not working properly, though) but my code looks very dirty.

else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // Classic Bluetooth discovery ended
            lastOpenedType = getLastOpenedType();
            if (lastOpenedType == BT_CLASSIC && !isSearchStoppedByUser()) {
                // Search should continue with low energy scan
                startBtLeScanning();
            } else if (lastOpenedType != BT_CLASSIC && !isSearchStoppedByUser()){
                // Search ended
                searchProgressLayout.setVisibility(View.INVISIBLE);
            } else {
                // Search ended by user
                searchProgressLayout.setVisibility(View.INVISIBLE);
            }
      }

In short, I am asking if someone has a more brilliant and simple solution on this?

PS. A solution without broadcast intent is much appreciated if possible.

2

There are 2 best solutions below

2
On

BluetoothAdapter's startDiscovery() method searches for both classic and BLE devices. Once you get the result from the scan, you can separate them based on the type of the device. For example:

int deviceType = device.getType();

if(deviceType == BluetoothDevice.DEVICE_TYPE_CLASSIC)
{

}
else if(deviceType == BluetoothDevice.DEVICE_TYPE_LE)
{

}
else if(deviceType == BluetoothDevice.DEVICE_TYPE_DUAL)
{   

}

So, it's not needed to search separately.

0
On

startLESCan is more advanced and better method of scanning bluetooth devices with some advancements like take less energy consumption.