Android判断蓝牙设备类型

最近有个需求是蓝牙界面只显示耳机,不显示其他设备,所以看了一下蓝牙相关代码。

在BluetoothClass中定义了蓝牙设备的类型

frameworks/base/core/java/android/bluetooth/BluetoothClass.java

java 复制代码
    public static class Device {
        private static final int BITMASK = 0x1FFC;

        /**
         * Defines all major device class constants.
         * <p>See {@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;
        }

        // 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;

        // Devices in the PHONE major class
        public static final int PHONE_UNCATEGORIZED = 0x0200;
        public static final int PHONE_CELLULAR = 0x0204;
        public static final int PHONE_CORDLESS = 0x0208;
        public static final int PHONE_SMART = 0x020C;
        public static final int PHONE_MODEM_OR_GATEWAY = 0x0210;
        public static final int PHONE_ISDN = 0x0214;

        // Minor classes for the AUDIO_VIDEO major class
        public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400;
        public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404;
        public static final int AUDIO_VIDEO_HANDSFREE = 0x0408;
        //public static final int AUDIO_VIDEO_RESERVED              = 0x040C;
        public static final int AUDIO_VIDEO_MICROPHONE = 0x0410;
        public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414;
        public static final int AUDIO_VIDEO_HEADPHONES = 0x0418;
        public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C;
        public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420;
        public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424;
        public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428;
        public static final int AUDIO_VIDEO_VCR = 0x042C;
        public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430;
        public static final int AUDIO_VIDEO_CAMCORDER = 0x0434;
        public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438;
        public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C;
        public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440;
        //public static final int AUDIO_VIDEO_RESERVED              = 0x0444;
        public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448;

        // Devices in the WEARABLE major class
        public static final int WEARABLE_UNCATEGORIZED = 0x0700;
        public static final int WEARABLE_WRIST_WATCH = 0x0704;
        public static final int WEARABLE_PAGER = 0x0708;
        public static final int WEARABLE_JACKET = 0x070C;
        public static final int WEARABLE_HELMET = 0x0710;
        public static final int WEARABLE_GLASSES = 0x0714;

        // Devices in the TOY major class
        public static final int TOY_UNCATEGORIZED = 0x0800;
        public static final int TOY_ROBOT = 0x0804;
        public static final int TOY_VEHICLE = 0x0808;
        public static final int TOY_DOLL_ACTION_FIGURE = 0x080C;
        public static final int TOY_CONTROLLER = 0x0810;
        public static final int TOY_GAME = 0x0814;

        // Devices in the HEALTH major class
        public static final int HEALTH_UNCATEGORIZED = 0x0900;
        public static final int HEALTH_BLOOD_PRESSURE = 0x0904;
        public static final int HEALTH_THERMOMETER = 0x0908;
        public static final int HEALTH_WEIGHING = 0x090C;
        public static final int HEALTH_GLUCOSE = 0x0910;
        public static final int HEALTH_PULSE_OXIMETER = 0x0914;
        public static final int HEALTH_PULSE_RATE = 0x0918;
        public static final int HEALTH_DATA_DISPLAY = 0x091C;

        // Devices in PERIPHERAL major class
        public static final int PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500;
        public static final int PERIPHERAL_KEYBOARD = 0x0540;
        public static final int PERIPHERAL_POINTING = 0x0580;
        public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;
    }

可以通过下面的方式获取类型,DeviceClass和MajorDeviceClass分别对应上面的Major和其他。

CachedBluetoothDevice.getBtClass().getMajorDeviceClass()

CachedBluetoothDevice.getBtClass().getDeviceClass()

java 复制代码
  /**
     * 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);
    }

    /**
     * Return the (major and minor) device class component of this
     * {@link BluetoothClass}.
     * <p>Values 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);
    }

Major类型具体解释如下,和代码中也可以一一对应:

所以我们只需要判断CachedBluetoothDevice.getBtClass().getMajorDeviceClass() == BluetoothClass.Device.Major.AUDIO_VIDEO)就可以了

相关推荐
_Sem18 分钟前
KMP实战:从单端到跨平台的完整迁移指南
android·前端·app
從南走到北21 分钟前
JAVA国际版任务悬赏发布接单系统源码支持IOS+Android+H5
android·java·ios·微信·微信小程序·小程序
vistaup38 分钟前
Android ContentProvier
android·数据库
我是场40 分钟前
Android Camera 从应用到硬件之- 枚举Camera - 1
android
4Forsee40 分钟前
【Android】View 事件分发机制与源码解析
android·java·前端
咕噜签名分发冰淇淋41 分钟前
苹果ios安卓apk应用APP文件怎么修改手机APP显示的名称
android·ios·智能手机
应用市场42 分钟前
从零开始打造Android桌面Launcher应用:原理剖析与完整实现
android
叶羽西43 分钟前
Android15增强型视觉系统(EVS)
android
沅霖44 分钟前
android kotlin语言中的协程
android·开发语言·kotlin
齊家治國平天下1 小时前
Android 14 系统启动流程深度解析:rc文件的语法、解析及常见语法详解
android·init·rc·init.rc