【问题标题】:How to differentiate the connected bluetooth device in android programmatically?如何以编程方式区分android中连接的蓝牙设备?
【发布时间】:2016-07-19 15:02:18
【问题描述】:

是蓝牙耳机还是手机?

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

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

请帮助我将连接的蓝牙设备区分为耳机/android设备(手机)等,

提前谢谢你。

【问题讨论】:

标签: android bluetooth android-bluetooth android-broadcast


【解决方案1】:

扫描并找到BluetoothDevice 后,调用方法BluetoothDevice.getBluetoothClass()。这将返回一个BluetoothClass 对象,documentation 声明如下:

表示蓝牙类,描述一般特征 和设备的功能。例如,蓝牙类将 指定一般设备类型,例如电话、计算机或 耳机,以及它是否能够提供诸如音频或 电话。

因此,在您允许用户选择要连接的设备或过滤显示的BluetoothDevices 列表之前,请尝试查看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 找到不同的设备类常量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多