如何判断蓝牙设备类型

我们在开发Android的蓝牙应用时,可能需要知道扫描到的蓝牙设备是什么类型,然后过滤掉不符合要求的设备,只保留符合要求的设备,例如我们在车载系统上开发蓝牙电话应用时,我们希望只显示手机蓝牙设备,那么如何从搜索到的蓝牙设备中选择出手机蓝牙设备?

其实Android已经为我们提供了相应的API来识别设备类型,假如你已经获取到了蓝牙设备,设备对象为device:

那么可以通过下面的方法获取设备类型->

BluetoothClass bluetoothClass = device.getBluetoothClass();
final int deviceClass = bluetoothClass.getDeviceClass(); //设备类型(音频、手机、电脑、音箱等等)
final int majorDeviceClass = bluetoothClass.getMajorDeviceClass();//具体的设备类型(例如音频设备又分为音箱、耳机、麦克风等等)

然后这样判断

if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
    //音箱

} else if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE) {
    //麦克风

} else if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES) {
    //耳机

} else if (majorDeviceClass == BluetoothClass.Device.Major.COMPUTER) {
    //电脑

} else if (majorDeviceClass == BluetoothClass.Device.Major.PHONE) {
    //手机

} else if (majorDeviceClass == BluetoothClass.Device.Major.HEALTH) {
    //健康类设备

} else {
    //其它蓝牙设备

}

下面是我总结了一下蓝牙设备的类型:

设备类型

deviceClass [十六进制]

deviceClass [十进制]

majorDeviceClass

电脑

0x0100

256

公司电脑(260)

手机

0x0200

512

华为手机

音箱

0x0400

1024

华为耳机/米兔音箱(1028)/SANSUI A38S(1028)

 

本文参考资料:https://blog.csdn.net/weixin_34242819/article/details/92023989

你可能感兴趣的:(如何判断蓝牙设备类型)