Android蓝牙连接设备

Ste*_*ler 7 android bluetooth android-bluetooth bluetooth-profile

无论个人资料如何,如何获取Android的所有已连接蓝牙设备的列表?

另外,我看到您可以通过BluetoothManager.getConnectedDevices获取特定配置文件的所有连接的设备。

而且我想我可以通过ACTION_ACL_CONNECTED / ACTION_ACL_DISCONNECTED侦听连接/断开连接来查看连接了哪些设备...似乎容易出错

但是我想知道是否有一种更简单的方法来获取所有已连接的蓝牙设备的列表。

alp*_*tis 6

要查看完整列表,这是一个 2 步操作:

  1. 获取当前配对设备的列表
  2. 扫描或发现范围内的所有其他人

要获取当前配对设备的列表并进行迭代:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

发现是一个更复杂的操作。为此,您需要告诉 BluetoothAdapter 开始扫描/发现。当它找到东西时,它会发送您需要使用 BroadcastReceiver 接收的 Intent。

首先,我们将设置接收器:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在剩下的就是告诉 BluetoothAdapter 开始扫描:

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

btAdapter.startDiscovery();
Run Code Online (Sandbox Code Playgroud)

  • 它可以获取已配对设备的列表,但我不知道连接的是哪一个 (9认同)
  • 我们可以只获取当前蓝牙连接的设备名称吗? (3认同)

小智 5

获取连接设备的最佳方式如下:

  1. 配对设备

    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)
            )
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 检查是否已连接。

要检查配对设备是否已连接,您需要使用此方法:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

参考这里

  • 这适用于绑定设备,而不是连接设备。即配对设备并断开连接。它仍将位于绑定设备中。 (3认同)