如何以编程方式区分android中连接的蓝牙设备? [英] How to differentiate the connected bluetooth device in android programmatically?
问题描述
是蓝牙耳机还是手机?
如何在android代码中区分蓝牙耳机和支持蓝牙的android设备.
how to differentiate the bluetooth headset and bluetooth enabled android device in android code.
我正在开发一个小应用程序,因为我具有阻止通过蓝牙传输数据的功能,但它需要允许通过蓝牙耳机进行通信.
I am developing a small application,in that I have a feature of blocking the data transfer via bluetooth but it need to allow communication via bluetooth headset.
我参考了示例和代码,因为它们建议我仅配对/取消配对蓝牙设备.Android:如何以编程方式配对蓝牙设备?
or else
获取所有连接的设备.在安卓中,如何获取配置文件连接的蓝牙设备?
我是否可以在设备中收到与类型相关的任何广播消息设备连接?
Whether can i get any broadcast message in device related to type of device connection?
请帮助我将连接的蓝牙设备区分为耳机/安卓设备(手机)等,
Please help me on this to differentiate the connected bluetooth device as headset /android devices(mobile phone).etc.,
提前致谢.
推荐答案
一旦你扫描并找到一个 BluetoothDevice
调用方法 BluetoothDevice.getBluetoothClass()
.这将返回一个 BluetoothClass
对象,文档 声明如下:
Once you scan and find a BluetoothDevice
call the method BluetoothDevice.getBluetoothClass()
. This will return a BluetoothClass
object and the documentation states the following:
代表一个蓝牙类,描述一般特性和设备的能力.例如,一个蓝牙类将指定一般设备类型,例如电话、计算机或耳机,以及它是否能够提供诸如音频或电话.
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
是否具有正确的设备类型.
So before you allow the user to select the device to connect to, or to filter the list of BluetoothDevice
s shown, try seeing if the BluetoothClass
has the correct device type.
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.
}
可以在此处找到不同的设备类常量,以防您想区分进一步按设备类别.
The different device class constants can be found here in case you want to differentiate by device class further.
这篇关于如何以编程方式区分android中连接的蓝牙设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!