是蓝牙耳机还是手机?
如何在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陈述如下:
Represents a Bluetooth class, which describes general characteristics and capabilities of a device. For example, a Bluetooth class will specify the general device type such as a phone, a computer, or headset, and whether it's capable of services such as audio or telephony.
因此,在允许用户选择要连接的设备或过滤显示的 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如果您想进一步区分设备类别。
关于android - 如何以编程方式区分android中连接的蓝牙设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328653/