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]