日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 获取蓝牙设备类型

發布時間:2024/1/8 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 获取蓝牙设备类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前我們分析了如何獲取已連接的藍牙設備地址

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 經典藍牙

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等,另外還要主要類型下面的分類

// Devices in the COMPUTER major classpublic static final int COMPUTER_UNCATEGORIZED = 0x0100;public static final int COMPUTER_DESKTOP = 0x0104;public static final int COMPUTER_SERVER = 0x0108;public static final int COMPUTER_LAPTOP = 0x010C;public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110;public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114;public static final int COMPUTER_WEARABLE = 0x0118;比如所有COMPUTER類型的細分類型

// Devices in PERIPHERAL major class/*** @hide*/public static final int PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500;/*** @hide*/public static final int PERIPHERAL_KEYBOARD = 0x0540;/*** @hide*/public static final int PERIPHERAL_POINTING = 0x0580;/*** @hide*/public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;所有外圍設備的細分


/*** 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:1280
public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;

連接的是游戲手柄,以上兩種類型的設備都屬于PROFILE_HID類型的設備。




總結

以上是生活随笔為你收集整理的Android 获取蓝牙设备类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。