[英]Android bluetooth get connected devices
How can I get a list of all connected bluetooth devices for Android regardless of profile?无论配置文件如何,如何获取所有已连接的 Android 蓝牙设备的列表?
Alternatively, I see that you can get all connected devices for a specific profile via BluetoothManager.getConnectedDevices .或者,我看到您可以通过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 .而且我想我可以通过ACTION_ACL_CONNECTED / ACTION_ACL_DISCONNECTED侦听连接/断开连接来查看哪些设备已连接......似乎容易出错。
But I'm wondering if there's a simpler way to get the list of all connected bluetooth devices.但我想知道是否有更简单的方法来获取所有连接的蓝牙设备的列表。
To see a complete list, this is a 2-step operation:要查看完整列表,这是一个 2 步操作:
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.
为此,您需要告诉 BluetoothAdapter 开始扫描/发现。 As it finds things, it sends out Intents that you'll need to receive with a BroadcastReceiver.
当它找到东西时,它会发送您需要使用 BroadcastReceiver 接收的 Intent。
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 开始扫描:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
// if already scanning ... cancel
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
btAdapter.startDiscovery();
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)
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.