Android 获取蓝牙设备类型

本文介绍如何在Android中获取蓝牙设备的类型,区别于蓝牙Profile,设备类型包括手机、电脑、手柄等。通过BluetoothDevice的getBluetoothClass方法获取BluetoothClass,它定义了主要类型如计算机、音频设备等,以及子类型,帮助识别如游戏手柄、键盘等设备。

之前我们分析了如何获取已连接的蓝牙设备地址

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 class
        public 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类型的设备。




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值