16

How can I get a list of all connected bluetooth devices for Android regardless of profile?

Alternatively, I see that you can get all connected devices for a specific profile via BluetoothManager.getConnectedDevices.

And I guess I could see which devices are connected by listening for connections/disconnections via ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED...seems error prone.

But I'm wondering if there's a simpler way to get the list of all connected bluetooth devices.

Steven Wexler
  • 16,589
  • 8
  • 53
  • 80

3 Answers3

5

To see a complete list, this is a 2-step operation:

  1. get list of currently paired devices
  2. scan for, or discover, all others in range

To get a list of, and iterate, the currently paired devices:

Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice d: pairedDevices) {
        String deviceName = d.getName();
        String macAddress = d.getAddress();
        Log.i(LOGTAG, "paired device: " + deviceName + " at " + macAddress);
        // do what you need/want this these list items
    }
}

Discovery is a little bit more of a complex operation. To do this, you'll need to tell the BluetoothAdapter to start scanning/discovering. As it finds things, it sends out Intents that you'll need to receive with a BroadcastReceiver.

First, we'll set up the receiver:

private void setupBluetoothReceiver()
{
    BroadcastRecevier btReceiver = new BroadcastReciver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            handleBtEvent(context, intent);
        }
    };
    IntentFilter eventFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    // this is not strictly necessary, but you may wish
    //  to know when the discovery cycle is done as well
    eventFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    myContext.registerReceiver(btReceiver, eventFilter);
}

private void handleBtEvent(Context context, Intent intent)
{
    String action = intent.getAction();
    Log.d(LOGTAG, "action received: " + action);

    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.i(LOGTAG, "found device: " + device.getName());
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        Log.d(LOGTAG, "discovery complete");
    }
}

Now all that is left is to tell the BluetoothAdapter to start scanning:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
// if already scanning ... cancel
if (btAdapter.isDiscovering()) {
    btAdapter.cancelDiscovery();
}

btAdapter.startDiscovery();
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
alpartis
  • 1,086
  • 14
  • 29
  • It would be most helpful if the down-voting readers would take the time to comment on what it is they don't like. The above code is exactly what I'm doing to achieve the OP's objective ... and it appears to be working as expected, at least for me. – alpartis May 07 '18 at 00:42
  • You don't need to pair a Bluetooth Low Energy device to connect to it. And ´getBondedDevices()´ would not return connected BLE devices. Therefore your code is not really doing what is requested. – Alexey Feb 01 '19 at 08:48
  • FYI, these codes only applicable for Bluetooth Classic devices. They aren't for BLE devices. https://developer.android.com/guide/topics/connectivity/bluetooth – ecle Feb 08 '19 at 10:22
  • 16
    It can get list of paired devices but I don't know which one is connected – Yeung Nov 05 '19 at 07:59
  • 2
    Can we get only currently Bluetooth connected device name ? – GNK Oct 14 '20 at 05:38
  • getBondedDevices() gives you currently paired devices, not currently connected devices. There can be paired devices that are not connected, and there can be connected devices that are not paired. – GPS Jan 03 '22 at 10:04
5

The best way to get your connected devices like the following:

  1. paired device

    val btManager = baseContext.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
    val pairedDevices = btManager.adapter.bondedDevices
    
    if (pairedDevices.size > 0) {
    
        for (device in pairedDevices) {
            val deviceName = device.name
            val macAddress = device.address
            val aliasing = device.alias
    
            Log.i(
                " pairedDevices ",
                "paired device: $deviceName at $macAddress + $aliasing " + isConnected(device)
            )
        }
    }
    
  2. check whatever is connected or no.

To check if the paired Device is connected or no, u need to use this method:

private fun isConnected(device: BluetoothDevice): Boolean {
    return try {
        val m: Method = device.javaClass.getMethod("isConnected")
        m.invoke(device) as Boolean
    } catch (e: Exception) {
        throw IllegalStateException(e)
    }
}

ref here.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • 2
    This is for bonded devices, not connected devices. i.e. pair a device and disconnect. it'll still be in the bonded devices. – Hiro Sep 22 '21 at 19:38
  • After getting the bounded devices, u are going to check if this bounded device is connected or not. This is the simplest way to get the connected devices on your Android – Abdallah AbuSalah Sep 23 '21 at 04:17
  • How about `getProfileProxy`? – Hiro Sep 23 '21 at 09:53
  • 1
    @AbdallahAbuSalah there is a difference between connected and bonding. A device that is connected is not necessarily bonded so you will miss devices. –  Oct 25 '21 at 17:45
2

That's how you get "connected" devices, not just paired devices. No need to check further state of it.

val btManager = view.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
val connectedDevices = btManager.getConnectedDevices(GATT)
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69