网站建设企业站,外贸五金网站,今天的新闻是什么,织梦网站0day漏洞Android 蓝牙设备类型判断
一、前言
Android 蓝牙设备有各种类型#xff0c;比如蓝牙手机#xff0c;蓝牙耳机#xff0c;蓝牙手环#xff0c;蓝牙鼠标键盘#xff0c;蓝牙玩具#xff0c;健康检测设备等等。
有些场景需要具体区分就需要进行判断了。一个是扫描到后图…Android 蓝牙设备类型判断
一、前言
Android 蓝牙设备有各种类型比如蓝牙手机蓝牙耳机蓝牙手环蓝牙鼠标键盘蓝牙玩具健康检测设备等等。
有些场景需要具体区分就需要进行判断了。一个是扫描到后图标显示设备类型还是可能是具体处理的区别。
这些设备的类型硬件设备本身其实是有定义的 但是也不排除有些设备定义不正确这些都是要我们开发者进行调试才清楚的。
二、蓝牙类型获取api
BluetoothDevice bluetoothDevice; //蓝牙设备对象怎么获取后面接收
int type device.getType(); //1简单类型
BluetoothClass bluetoothClass device.getBluetoothClass();
int deviceClassType bluetoothClass.getDeviceClass(); //2clss tyep
int deviceType bluetoothClass.getMajorDeviceClass(); //3具体类型
getMajorDeviceClass 才是详细类型的数值。
其实把这几个type 都打印出来就能看到数值了。
三、各个蓝牙类型获取api详解
1、getType() 方法的类型
这个方法获取的是大致的类型。
具体类型的定义
packages\modules\Bluetooth\framework\java\android\bluetooth\BluetoothDevice.java
/*** Bluetooth device type, Unknown*/public static final int DEVICE_TYPE_UNKNOWN 0;/*** Bluetooth device type, Classic - BR/EDR devices经典-BR/EDR设备*/public static final int DEVICE_TYPE_CLASSIC 1;/*** Bluetooth device type, Low Energy - LE-only低能耗-仅限LE*/public static final int DEVICE_TYPE_LE 2;/*** Bluetooth device type, Dual Mode - BR/EDR/LE双模式-BR/EDR/LE*/public static final int DEVICE_TYPE_DUAL 3;Android13 的蓝牙源码是在这个目录旧的源码是在framework 目录下的。
比如手机设备的值为3蓝牙耳机、蓝牙鼠标键盘值为1 但是这个不能区分具体类型比较。所以getType() 一般不怎么使用。
2、getDeviceClass()方法的类型
这个类型没怎么用过网上也没几个人用。 因为他定义的类型代码中并未进行分类需要自己去蓝牙相关协议对比确认。
具体类型的定义
packages\modules\Bluetooth\framework\java\android\bluetooth\BluetoothClass.java /*** Return the (major and minor) device class component of this* {link BluetoothClass}.* pValues returned from this function can be compared with the* public constants in {link BluetoothClass.Device} to determine which* device class is encoded in this Bluetooth class.** return device class component*/public int getDeviceClass() {return (mClass Device.BITMASK);}/*** Return the Bluetooth Class of Device (CoD) value including the* {link BluetoothClass.Service}, {link BluetoothClass.Device.Major} and* minor device fields.** pThis value is an integer representation of Bluetooth CoD as in* Bluetooth specification.** see a hrefBluetooth CoDhttps://www.bluetooth.com/specifications/assigned-numbers/baseband/a** hide*/TestApi //这个是隐藏的也是蓝牙设备类型的COD数值。public int getClassOfDevice() {return mClass;}
这里看不出啥但是可以知道与蓝牙的COD数值相关COD 具体类型的定义有兴趣的可以自己搜索。
下面的 getMajorDeviceClass 方法的值也是基于 蓝牙COD的。
从后面日志看 getDeviceClass 方法还更具体一些。
3、getMajorDeviceClass()
这个类型是在 DeviceClass 对象基础上的类型
下面的代码类型定义都是在 BluetoothClass.java 这个类里面。
具体类型的定义 //获取具体类型public int getMajorDeviceClass() {return (mClass Device.Major.BITMASK);}//内部类定义具体数据public static class Device {private static final int BITMASK 0x1FFC;/*** Defines all major device class constants.* pSee {link BluetoothClass.Device} for minor classes.*/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;}。。。}注意这里是16进制的数值日志打印中是十进制的数需要转换后对比。
4、查看日志打印 //监听各种蓝牙广播public void registerBroadcast(Context context) {DebugLog.debug();final IntentFilter intentFilter new IntentFilter();intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);intentFilter.addAction(BluetoothDevice.ACTION_FOUND);context.registerReceiver(mReceiver, intentFilter);}//打印广播后的日志private final BroadcastReceiver mReceiver new BroadcastReceiver() {Overridepublic void onReceive(Context context, Intent intent) {final String action intent.getAction();DebugLog.info(action action);if (action null) {DebugLog.error(action null!);return;}BluetoothDevice device intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);if (device null) {DebugLog.error(device null!);return;}int type device.getType();int getDeviceClassType device.getBluetoothClass().getDeviceClass();int getMajorDeviceClassType device.getBluetoothClass().getMajorDeviceClass();DebugLog.info(device device);DebugLog.info(getAlias device.getAlias());DebugLog.info(type type);DebugLog.info(getDeviceClassType getDeviceClassType);DebugLog.info(getMajorDeviceClassType getMajorDeviceClassType);}};这里有蓝牙手机、耳机、音箱、鼠标、键盘、遥控器。 具体日志打印如下 //普通手机
action android.bluetooth.device.action.ACL_CONNECTED
device F0:5C:77:F4:5E:65
getAlias Pixel 4
type 1最开始打印这个 --》 type 3后面打印这个
getDeviceClassType 524 // --0x20c
getMajorDeviceClassType 512 // --0x200//蓝牙耳机
action android.bluetooth.device.action.BOND_STATE_CHANGED//其他广播也打印这边不一一写
device 0D:3F:91:E2:FF:D3
getAlias Y-12
type 1
getDeviceClassType 1028 // --0x404
getMajorDeviceClassType 1024 // --0x400//蓝牙音箱和耳机一样
action android.bluetooth.device.action.BOND_STATE_CHANGED
device 11:75:58:11:AC:7C
getAlias A19
type 1
getDeviceClassType 1028
getMajorDeviceClassType 1024//蓝牙鼠标
action android.bluetooth.device.action.ACL_CONNECTED
device 34:88:5D:AC:32:D1
getAlias Bluetooth Mouse M336/M337/M535
type 1
getDeviceClassType 1408 // --0x580
getMajorDeviceClassType 1280 // --0x500//蓝牙键盘
action android.bluetooth.device.action.PAIRING_REQUEST
device 34:88:5D:C1:4A:9D
getAlias Keyboard K380
type 1
getDeviceClassType 1344 // --0x540
getMajorDeviceClassType 1280//蓝牙遥控器
action android.bluetooth.device.action.BOND_STATE_CHANGED
device 54:03:84:90:FB:72
getAlias Horion control FB72
type 2
getDeviceClassType 1292// --0x50c
getMajorDeviceClassType 1280
Android系统代码蓝牙连接界面上也是使用的 getMajorDeviceClassType 返回的值显示对应的图标。
上面也看到了 getDeviceClassType 可以具体区分蓝牙键盘和鼠标。 其他没遇见的类型可以自己打印这些数据查看一下信息即可。
类型判断参考 https://www.codenong.com/js12b69fe68246/
从上面日志看 getMajorDeviceClassType 很多蓝牙设备会相同但是 getDeviceClassType 不一样。 getDeviceClassType 类型还更详细 看起来 getDeviceClassType 是在 getMajorDeviceClassType 的类型上加上具体类型值。
蓝牙类型 SOD相关值介绍
https://blog.csdn.net/Atlas12345/article/details/104872833
四、蓝牙对象 BluetoothDevice 获取
1、广播监听 IntentFilter filter new IntentFilter(BluetoothDevice.ACTION_FOUND); //发现设备广播
filter.addAction(BluetoothDevice.ACTION_DISCOVERY_FINISHED));//搜索结束广播
...private BroadcastReceiver receiver new BroadcastReceiver(){Overridepublic void onReceive(Context context, Intent intent){String action intent.getAction();if(BluetoothDevice.ACTION_FOUND.equals(action)){//发现蓝牙设备BluetoothDevice device0 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);} else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {//蓝牙绑定状态改变BluetoothDevice device1 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);}else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {//蓝牙耳机连接状态BluetoothDevice device3 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);}else if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {//蓝牙配对请求状态}...//还有其他蓝牙相关广播}
};
这里可以看到蓝牙相关的广播都是会有蓝牙对象回调的
2、遍历所有扫描到的蓝牙设备 // LocalBluetoothManager 是SettingsLib里面的类估计要源码应用或者导入SettingsLib包的应用才能获取到这个类。LocalBluetoothManager mBluetoothManager LocalBluetoothManager.getInstance(context, mOnInitCallback);//已连接/绑定设备列表SetBluetoothDevice bondedDevices mBluetoothManager.getBluetoothAdapter().getBondedDevices();LogUtil.debug(bondedDevices size bondedDevices.size());//未从已连接设备列表中找到断开的api从所有的设备列表中进行判断连接状态后断开CollectionCachedBluetoothDevice cachedDevices mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();LogUtil.debug(cachedDevices size cachedDevices.size());
这个方法普通应用是无法调用到的
普通应用只能扫描发现蓝牙把搜索的蓝牙放到列表中进行判断处理
五、总结
1、蓝牙类型获取api
BluetoothDevice bluetoothDevice; //蓝牙设备对象
int type device.getType(); //1功耗类型
BluetoothClass bluetoothClass device.getBluetoothClass();
int deviceClassType bluetoothClass.getDeviceClass(); //2更细的具体类型系统代码中未定义相关数值
int deviceType bluetoothClass.getMajorDeviceClass(); //3主要使用的具体类型能简单区分类别
系统应用中也是使用 getMajorDeviceClass 进行判断的。
2、具体类型的定义
BluetoothClass.java 这个类里面。//获取具体类型public int getMajorDeviceClass() {return (mClass Device.Major.BITMASK);}//内部类定义具体数据public static class Device {private static final int BITMASK 0x1FFC;/*** Defines all major device class constants.* pSee {link BluetoothClass.Device} for minor classes.*/public static class Major {private static final int BITMASK 0x1F00;public static final int MISC 0x0000;public static final int COMPUTER 0x0100; //电脑设备对应10进制 256public static final int PHONE 0x0200; //手机设备512public static final int NETWORKING 0x0300; //网络设备 不清楚什么设备768public static final int AUDIO_VIDEO 0x0400; //音频视频设备蓝牙耳机音响1024public static final int PERIPHERAL 0x0500; //外部设备比如蓝牙鼠标键盘蓝牙遥控器1280public static final int IMAGING 0x0600; //影像设备1536public static final int WEARABLE 0x0700; //穿戴设备1792public static final int TOY 0x0800; //玩具设备2048public static final int HEALTH 0x0900; //健康检测设备2304public static final int UNCATEGORIZED 0x1F00;}。。。}