Android 获取蓝牙设备类型
生活随笔
收集整理的這篇文章主要介紹了
Android 获取蓝牙设备类型
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前我們分析了如何獲取已連接的藍牙設備地址
http://blog.csdn.net/jasonwang18/article/details/61214431
本篇我們分析如何獲取對應藍牙設備的類型,這個類型和profile不是同一個東西,而是具體藍牙的設備類型,比如手機、電腦、手柄、藍牙耳機等
我們看到手機搜索到的藍牙設備類型有三種,手機、電腦和普通藍牙
/*** Get the Bluetooth device type of the remote device.** <p>Requires {@link android.Manifest.permission#BLUETOOTH}** @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE}* {@link #DEVICE_TYPE_DUAL}.* {@link #DEVICE_TYPE_UNKNOWN} if it's not available*/@RequiresPermission(Manifest.permission.BLUETOOTH)public int getType() {if (sService == null) {Log.e(TAG, "BT not enabled. Cannot get Remote Device type");return DEVICE_TYPE_UNKNOWN;}try {return sService.getRemoteType(this);} catch (RemoteException e) {Log.e(TAG, "", e);}return DEVICE_TYPE_UNKNOWN;}我們看到BluetoothDevice中有一個方法getType,這個返回的是profile類型,也就是
DEVICE_TYPE_CLASSIC 經(jīng)典藍牙
DEVICE_TYPE_LE ? ? ? ? ? ?低功耗藍牙
DEVICE_TYPE_DUAL ? ? ? 雙向藍牙
然而并不是我們想要的東西
/*** Get the Bluetooth class of the remote device.* <p>Requires {@link android.Manifest.permission#BLUETOOTH}.** @return Bluetooth class object, or null on error*/@RequiresPermission(Manifest.permission.BLUETOOTH)public BluetoothClass getBluetoothClass() {if (sService == null) {Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");return null;}try {int classInt = sService.getRemoteClass(this);if (classInt == BluetoothClass.ERROR) return null;return new BluetoothClass(classInt);} catch (RemoteException e) {Log.e(TAG, "", e);}return null;}getBluetoothClass返回remoteDevice的class
public static class Major {private static final int BITMASK = 0x1F00;public static final int MISC = 0x0000;public static final int COMPUTER = 0x0100;public static final int PHONE = 0x0200;public static final int NETWORKING = 0x0300;public static final int AUDIO_VIDEO = 0x0400;public static final int PERIPHERAL = 0x0500;public static final int IMAGING = 0x0600;public static final int WEARABLE = 0x0700;public static final int TOY = 0x0800;public static final int HEALTH = 0x0900;public static final int UNCATEGORIZED = 0x1F00;}BluetoothClass定義了所有主要類型,比如麥克風MISC、COMPUTER、AUDIO等,另外還要主要類型下面的分類
/*** Return the major device class component of this {@link BluetoothClass}.* <p>Values returned from this function can be compared with the* public constants in {@link BluetoothClass.Device.Major} to determine* which major class is encoded in this Bluetooth class.** @return major device class component*/public int getMajorDeviceClass() {return (mClass & Device.Major.BITMASK);} getMajorDeviceClass獲取major的值 04-17 15:36:41.804 20430-20481/? I/Unity: GetBluetoothClass(Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37) 04-17 15:36:41.811 20430-20481/? I/Unity-connection: address:20:73:02:61:00:17 BluetoothClass:1344 04-17 15:36:41.811 20430-20481/? I/Unity-connection: address:20:73:02:61:00:17 BluetoothClass:1280
我們看到major的類型值是1280,也就是PERIPHERAL,外圍設備
連接的是一把游戲槍。子類型是PERIPHERAL_KEYBOARD
04-17 16:06:22.305 23043-23061/? I/Unity: GetBluetoothClass(Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37) 04-17 16:06:22.311 23043-23061/? I/Unity-connection: address:07:14:11:04:22:35 BluetoothClass:1480 04-17 16:06:22.311 23043-23061/? I/Unity-connection: address:07:14:11:04:22:35 BluetoothClass:1280public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;
連接的是游戲手柄,以上兩種類型的設備都屬于PROFILE_HID類型的設備。
總結
以上是生活随笔為你收集整理的Android 获取蓝牙设备类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA数据结构之红-黑树
- 下一篇: 更完美 联想乐Phone获取root权限