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

标签 android bluetooth android-bluetooth android-broadcast

是蓝牙耳机还是手机?

如何在android代码中区分蓝牙耳机和启用蓝牙的android设备。

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

请帮我区分连接的蓝牙设备是耳机/安卓设备(手机)等,

提前谢谢你。

最佳答案

扫描并找到 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/

相关文章:

bluetooth - 我们可以将 Android 智能手机蓝牙角色更改为从属吗

java - 将 ArrayList<CustomClass> 存储到 SharedPreferences

android - 什么是 admob 中的印象点击率、请求 RPM、印象 RPM

java - 我可以调试 bluecove 库吗?

iphone - 尽快在2台iOS设备之间发送数据

java - Android 示例蓝牙聊天应用程序中此空分配的目的是什么

java - 我必须在哪里初始化处理程序?

从 .lib 切换到 .so 后,Android 应用程序崩溃

Android - 如何从网络服务器下载文件

android - 无法运行 python-bluez RFCOMM 服务器示例脚本