安卓 - 列出已连接的蓝牙设备

4

我有一个应用程序,它将通过蓝牙接收语音输入(如果可用),如果不可用,则通过手机麦克风读取。我似乎找不到一种方法来检查是否已连接任何蓝牙设备(不仅仅是配对)。有什么建议吗?


你有这个问题的解决方案吗?我正在寻找同样的答案,请提供回答。 - Gangadhar Nimballi
不好意思,我从未得到解决方案。 - user2812299
3个回答

2
以下方法适用于我 ( API >= 18 ) :
final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> gattServerConnectedDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
for (BluetoothDevice device : gattServerConnectedDevices) {
    Log.d("Tag", "Found connected device: " + device.getAddress());
}

3
这仅适用于Gatt设备。我需要耳机、蓝牙耳机等设备。 - user2812299
我可以从这个API获取我的蓝牙鼠标,它对我很有效。 - Lai Lee

1

1
请问你能提供一个示例吗?我尝试过使用广播接收器,但我想在onCreate方法中查询。 - user2812299
1
请注意,这仅适用于BluetoothProfile.GATT和BluetoothProfile.GATT_SERVER。 - swooby

1

通过BluethoothHeadset服务,可以列出已连接的耳机设备。

   btAdapter.getProfileProxy(context, object : BluetoothProfile.ServiceListener {
            override fun onServiceDisconnected(p0: Int) {
                //
            }

            override fun onServiceConnected(p0: Int, headset: BluetoothProfile?) {
                headset?.connectedDevices?.forEach {
                    Timber.d("${it.name} ${it.address}")
                }
            }
        }, BluetoothProfile.HEADSET)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接