list connected bluetooth devices?

Viewed 20698

How can I list all connected bluetooth devices on android ?

thanks!

8 Answers
  • First you need to retrieve the BluetoothAdapter:

final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

  • Second you need to make sure Bluetooth is available and turned on :

if (btAdapter != null && btAdapter.isEnabled()) // null means no Bluetooth!

If the Bluetooth is not turned out you can either use btAdapter.enable() which is not recommended in the documentation or ask the user to do it : Programmatically enabling bluetooth on Android

  • Third you need to define an array of states (to filter out unconnected devices):

final int[] states = new int[] {BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING};

  • Fourth, you create a BluetoothProfile.ServiceListener which contains two callbacks triggered when a service is connected and disconnected :

    final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
        }
    
        @Override
        public void onServiceDisconnected(int profile) {
        }
    };
    

Now since you have to repeat the querying process for all available Bluetooth Profiles in the Android SDK (A2Dp, GATT, GATT_SERVER, Handset, Health, SAP) you should proceed as follow :

In onServiceConnected, place a condition that check what is the current profile so that we add the found devices into the correct collection and we use : proxy.getDevicesMatchingConnectionStates(states) to filter out unconnected devices:

switch (profile) {
    case BluetoothProfile.A2DP:
        ad2dpDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.GATT: // NOTE ! Requires SDK 18 !
        gattDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.GATT_SERVER: // NOTE ! Requires SDK 18 !
        gattServerDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.HEADSET: 
        headsetDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.HEALTH: // NOTE ! Requires SDK 14 !
        healthDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.SAP: // NOTE ! Requires SDK 23 !
        sapDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
}

And finally, the last thing to do is start the querying process :

btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.A2DP);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT_SERVER); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEADSET);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEALTH); // NOTE ! Requires SDK 14 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.SAP); // NOTE ! Requires SDK 23 !

source: https://stackoverflow.com/a/34790442/2715054

So you get the list of paired devices.

 BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevicesList = btAdapter.getBondedDevices();


    for (BluetoothDevice pairedDevice : pairedDevicesList) {
    Log.d("BT", "pairedDevice.getName(): " + pairedDevice.getName());
    Log.d("BT", "pairedDevice.getAddress(): " + pairedDevice.getAddress());

    saveValuePreference(getApplicationContext(), pairedDevice.getName(), pairedDevice.getAddress());

}

Android system doesn't let you query for all "currently" connected devices. It however, you can query for paired devices. You will need to use a broadcast receiver to listen to ACTION_ACL_{CONNECTED|DISCONNECTED} events along with STATE_BONDED event to update your application states to track what's currently connected.

I found a solution and it works on android 10

Kotlin

private val serviceListener: ServiceListener = object : ServiceListener {
    var name: String? = null
    var address: String? = null
    var threadName: String? = null
    override fun onServiceDisconnected(profile: Int) {}
    override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
        for (device in proxy.connectedDevices) {
            name = device.name
            address = device.address
            threadName = Thread.currentThread().name
            Toast.makeText(
                this@MainActivity,
                "$name $address$threadName",
                Toast.LENGTH_SHORT
            ).show()
            Log.i(
                "onServiceConnected",
                "|" + device.name + " | " + device.address + " | " + proxy.getConnectionState(
                    device
                ) + "(connected = "
                        + BluetoothProfile.STATE_CONNECTED + ")"
            )
        }
        BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
    }
}

Call this method in main thread

BluetoothAdapter.getDefaultAdapter()
            .getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET)

Java

original code

Related