Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bluetooth get connected devices

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.

like image 452
Steven Wexler Avatar asked May 04 '15 21:05

Steven Wexler


People also ask

Why is my Bluetooth not finding available devices?

If your Bluetooth devices won't connect, it's likely because the devices are out of range, or aren't in pairing mode. If you're having persistent Bluetooth connection problems, try resetting your devices, or having your phone or tablet "forget" the connection. Visit Business Insider's homepage for more stories.


2 Answers

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();
like image 134
alpartis Avatar answered Sep 24 '22 06:09

alpartis


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)
like image 41
M. Usman Khan Avatar answered Sep 23 '22 06:09

M. Usman Khan