Reputation: 17299
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.
Upvotes: 18
Views: 24901
Reputation: 126
The best way to get your connected devices like the following:
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)
)
}
}
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.
Upvotes: 6
Reputation: 4458
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)
Upvotes: 1
Reputation: 1122
To see a complete list, this is a 2-step operation:
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();
Upvotes: 4