无论是蓝牙耳机还是手机?
如何在android代码中区分蓝牙耳机和支持蓝牙的android设备。
我正在开发一个小型应用程序,因为我有一个功能,阻止通过蓝牙数据传输,但它需要允许通过蓝牙耳机通信。
我引用了他们建议我
仅配对/取消配对蓝牙设备。
Android: How to pair bluetooth devices programmatically?
or else
获取所有连接的设备。
In Android, how to get the profile of a connected bluetooth device?
是否可以在设备中获取与类型
设备连接?
请帮助我区分连接的蓝牙设备和耳机/安卓设备(手机)等,
提前谢谢你。
最佳答案
扫描并找到BluetoothDevice
后,调用方法BluetoothDevice.getBluetoothClass()
。这将返回一个BluetoothClass
对象,documentation声明如下:
表示Bluetooth类,该类描述了一般特性
以及设备的功能。例如,蓝牙类将
指定常规设备类型,如电话、计算机或
耳机,以及它是否能够提供音频或
电话。
因此,在允许用户选择要连接的设备或筛选显示的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.
}
如果您想进一步按设备类进行区分,可以找到不同的设备类常量here。