Filter bluetooth devices with Extended Inquiry Response in Android

Viewed 42

I'm trying to filter a set of Bluetooth devices in classic mode when they are only paired to the Android system. I can't find any API to get these extended inquiry responses on Android. Is there anyone who can help me find any documentation or a sample on this matter?

EDITED

Some information on the extended inquiry response, and it's beyond the public methods we have on BluetoothDevice class. I need to get some manufacture specific values using the extended inquiry response.

EIR response values The Extended Inquiry Response includes the following:

  • device local name

  • service class UUIDs of each service the device supports

  • transmission power

  • manufacturer specific values

1 Answers

You can, for example, filter the paired Bluetooth device list to show only Serial Port Profile (SPP) devices in Bluetooth Classic mode. But first, you must obtain the dangerous Manifest.permission.BLUETOOTH_CONNECT permission from the user for Android SDK API 31+ (I am not going to show how; please refer https://developer.android.com/guide/topics/connectivity/bluetooth/permissions).

    public static final UUID BT_SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    public void getDevices() {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        Map<String, String> map = new LinkedHashMap<>();

        if (mBluetoothAdapter != null) {
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            pairedDevices.stream().forEach(device->{
                if (device.getType() == BluetoothDevice.DEVICE_TYPE_CLASSIC) {
                    String[] uuids = Arrays.stream(device.getUuids()).map(uuid->uuid.getUuid().toString()).toArray(String[]::new);
                    boolean found = StringUtils.containsAnyIgnoreCase(BT_SPP_UUID.toString(), uuids);

                    if (found) {
                        // BT device name-mac address pair
                        map.put(StringUtils.defaultIfEmpty(device.getName(), "None"), device.getAddress());
                    }

                    Log.e(BluetoothDevicePreference.TAG,
                            String.format("getDevices(BT) ... BT_SPP_UUID ? %s -> name = %s, mac addr = %s, type = %s, alias = %s, BT class = %s, class = %s, uuids = %s",
                                    found ? "YES": "NO",
                                    device.getName(),
                                    device.getAddress(),
                                    device.getType(),
                                    device.getAlias(),
                                    device.getBluetoothClass(),
                                    device.getClass(),
                                    Arrays.toString(uuids)));
                }
            });
            CharSequence[] entries = map.entrySet().stream().map(entry->entry.getKey()).toArray(String[]::new);
            CharSequence[] entryValues = map.entrySet().stream().map(entry->entry.getValue()).toArray(String[]::new);

            // populate the list preference
            if (!map.isEmpty()) {
                listPreference.setEntries(entries);
                listPreference.setEntryValues(entryValues);
            } else {
                listPreference.setEntries(new String[]{"None"});
                listPreference.setEntryValues(new String[]{""});
            }
        }
        else {
            getContext().sendBroadcast(new Intent(INTENT_ACTION_BT_NOT_SUPPORTED));
        }
    }


getDevices(BT) ... BT_SPP_UUID ? NO -> name = JBL GO 2, mac addr = 30:C0:1B:64:B8:0E, type = 1, alias = null, BT class = 200414, class = class android.bluetooth.BluetoothDevice, uuids = [00001108-0000-1000-8000-00805f9b34fb, 0000111e-0000-1000-8000-00805f9b34fb, 0000110b-0000-1000-8000-00805f9b34fb, 0000110e-0000-1000-8000-00805f9b34fb, 00000000-0000-1000-8000-00805f9b34fb]
getDevices(BT) ... BT_SPP_UUID ? YES -> name = BT563300, mac addr = 00:07:80:C5:3C:8D, type = 1, alias = null, BT class = 1f00, class = class android.bluetooth.BluetoothDevice, uuids = [00001101-0000-1000-8000-00805f9b34fb, 00000000-0000-1000-8000-00805f9b34fb]
getDevices(BT) ... BT_SPP_UUID ? YES -> name = SerialADT24, mac addr = 34:C9:F0:83:5C:24, type = 1, alias = null, BT class = 1f00, class = class android.bluetooth.BluetoothDevice, uuids = [00001101-0000-1000-8000-00805f9b34fb, 00000000-0000-1000-8000-00805f9b34fb]

Related