如何以编程方式区分android中连接的蓝牙设备?

beg*_*ner 8 android bluetooth android-broadcast android-bluetooth

无论是蓝牙耳机还是手机?

如何在Android代码中区分蓝牙耳机和支持蓝牙的Android设备.

我正在开发一个小应用程序,因为我有通过蓝牙阻止数据传输的功能,但它需要允许通过蓝牙耳机进行通信.

请帮我解决这个问题,将连接的蓝牙设备区分为耳机/安卓设备(手机)等.,

先感谢您.

Geo*_*gan 8

一旦你扫描并找到一个BluetoothDevice调用方法BluetoothDevice.getBluetoothClass().这将返回一个BluetoothClass对象,文档说明如下:

表示蓝牙类,描述设备的一般特征和功能.例如,蓝牙类将指定一般设备类型,例如电话,计算机或耳机,以及它是否能够提供诸如音频或电话之类的服务.

因此,在允许用户选择要连接的设备或过滤显示的列表之前BluetoothDevice,请尝试查看是否BluetoothClass具有正确的设备类型.

BluetoothClass bluetoothClass = bluetoothDevice.getBluetoothClass();
if(bluetoothClass.getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES) {
    // allow user to select this device. I'm not sure exactly which type
    // headphones will be but this is a good guess. You can connect to
    // your Bluetooth headset to find out for sure.
}
Run Code Online (Sandbox Code Playgroud)

如果您希望进一步区分设备类,可以在此处找到不同的设备类常量.