How to differentiate the connected bluetooth device in android programmatically?

丶灬走出姿态 提交于 2019-12-10 02:58:47

问题


Whether it is a bluetooth headset or mobile phones?

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.

  • I referred the examples and codes in that they suggest me to pair/unpair the bluetooth devices only. Android: How to pair bluetooth devices programmatically?

                             or else
    
  • get all connected devices. In Android, how to get the profile of a connected bluetooth device?

  • 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.,

Thank you in advance.


回答1:


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.

So before you allow the user to select the device to connect to, or to filter the list of BluetoothDevices 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.



来源:https://stackoverflow.com/questions/36328653/how-to-differentiate-the-connected-bluetooth-device-in-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!