蓝牙hid协议源码解析

本文档详细介绍了蓝牙HID协议,包括人机交互设备的使用场景和市场产品,如蓝牙键盘、鼠标和游戏手柄。重点解析了蓝牙HID协议的客户端和服务端代码路径,开发流程和接口,特别是如何获取代理对象、注册应用和实现BluetoothHidDeviceCallback的回调方法。同时,文章详细阐述了蓝牙鼠标和键盘的功能实现,如滑动、点击、翻页及按键事件。最后,探讨了源码解析和未来研究方向,包括未理解的回调方法和相关类的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,概述

1.1 HID协议

HID协议: Hunman Interface Device Profile人机交互设备协议

使用场景:支持人机交互设备之间的控制

市场产品:蓝牙键盘,蓝牙鼠标,蓝牙游戏手柄等。

1.2 代码路径

客户端: frameworks\base\core\java\android\bluetooth


服务端: packages\apps\Bluetooth\src\com\android\bluetooth\ hid

HidDevService.java                 hid协议的服务端

开发流程和健康设备类似,但是稍微麻烦

2,接口

接口如下


3,开发步骤

在官方文档中有一个建立通信的流程:

1、调用getProfileProxy(Context,BluetoothProfile.ServiceListener, int)来获取代理对象的连接。

2、创建BluetoothHidDeviceAppSdpSettings, BluetoothHidDeviceAppQosSettings对象,创建BluetoothHidDeviceCallback回调,调用registerApp方法注册

3、将手机与设备配对,并且进行连接。

4、实现BluetoothHidDeviceCallback的7个回调方法

5、调用sendReport方法分别实现蓝牙鼠标,蓝牙键盘等。

3.1 获取客户端代理对象

一般在oncreate方法中,直接调用getProfileProxy方法,这个没什么好说的。

BluetoothAdapter.getDefaultAdapter().getProfileProxy(getApplicationContext(), 
				mProfileServiceListener,BluetoothProfile. HID_DEVICE);

private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
		
		@Override
		public void onServiceDisconnected(int profile) {
				if (profile == BluetoothProfile.HEALTH){
				     mBluetoothHealth = null;
			}
		}
		
		@SuppressLint("NewApi")
		@Override
		public void onServiceConnected(int profile, BluetoothProfile proxy) {
			if (profile == BluetoothProfile.HEALTH) {
				mHidDevice = (BluetoothHidDevice) proxy;
                 // 获取代理对象之后就进行注册
                 ...
			}
		}
	};

一般经过这个步骤,客户端的BluetoothHidDevice对象已经和服务端的HidDevService对象绑定了。

3.2 注册registerApp

BluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(
                            HidConsts.NAME, HidConsts.DESCRIPTION, HidConsts.PROVIDER,
                            BluetoothHidDevice.SUBCLASS1_COMBO, HidConsts.DESCRIPTOR);
BluetoothHidDeviceAppQosSettings inQos = new BluetoothHidDeviceAppQosSettings(
                        BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 200, 2, 200,
                        10000 /* 10 ms */, 10000 /* 10 ms */);
BluetoothHidDeviceAppQosSettings outQos = new BluetoothHidDeviceAppQosSettings(
                        BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 900, 9, 900,
                        10000 /* 10 ms */, 10000 /* 10 ms */);
boolean result = mHidDevice.registerApp(sdp, inQos, outQos, mCallback);

HidConsts类的定义如下:

public class HidConsts {

    public final static String NAME = "HID Device Testapp";

    public final static String DESCRIPTION = "";

    public final static String PROVIDER = "Codeaurora";

    /* @formatter:off */
    public final static byte[] DESCRIPTOR = {
        (byte) 0x05, (byte) 0x01,                    // USAGE_PAGE (Generic Desktop)
        (byte) 0x09, (byte) 0x02,                    // USAGE (Mouse)
        (byte) 0xa1, (byte) 0x01,                    // COLLECTION (Application)
        (byte) 0x09, (byte) 0x01,                    //   USAGE (Pointer)
        (byte) 0xa1, (byte) 0x00,                    //   COLLECTION (Physical)
        (byte) 0x85, (byte) 0x02,                    //     REPORT_ID (2)
        (byte) 0x05, (byte) 0x09,                    //     USAGE_PAGE (Button)
        (byte) 0x19, (byte) 0x01,                    //     USAGE_MINIMUM (Button 1)
        (byte) 0x29, (byte) 0x03,                    //     USAGE_MAXIMUM (Button 3)
        (byte) 0x15, (byte) 0x00,                    //     LOGICAL_MINIMUM (0)
        (byte) 0x25, (byte) 0x01,                    //     LOGICAL_MAXIMUM (1)
        (byte) 0x95, (byte) 0x03,                    //     REPORT_COUNT (3)
        (byte) 0x75, (byte) 0x01,                    //     REPORT_SIZE (1)
        (byte) 0x81, (byte) 0x02,                    //     INPUT (Data,Var,Abs)
        (byte) 0x95, (byte) 0x01,                    //     REPORT_COUNT (1)
        (byte) 0x75, (byte) 0x05,                    //     REPORT_SIZE (5)
        (byte) 0x81, (byte) 0x03,                    //     INPUT (Cnst,Var,Abs)
        (byte) 0x05, (byte) 0x01,                    //     USAGE_PAGE (Generic Desktop)
        (byte) 0x09, (byte) 0x30,                    //     USAGE (X)
        (byte) 0x09, (byte) 0x31,                    //     USAGE (Y)
        (byte) 0x15, (byte) 0x81,                    //     LOGICAL_MINIMUM (-127)
        (byte) 0x25, (byte) 0x7f,                    //     LOGICAL_MAXIMUM (127)
        (byte) 0x75, (byte) 0x08,                    //     REPORT_SIZE (8)
        (byte) 0x95, (byte) 0x02,                    //     REPORT_COUNT (2)
        (byte) 0x81, (byte) 0x06,                    //     INPUT (Data,Var,Rel)
        (byte) 0x09, (byte) 0x38,                    //     USAGE (Wheel)
        (byte) 0x15, (byte) 0x81,                    //     LOGICAL_MINIMUM (-127)
        (byte) 0x25, (byte) 0x7f,                    //     LOGICAL_MAXIMUM (127)
        (byte) 0x75, (byte) 0x08,                    //     REPORT_SIZE (8)
        (byte) 0x95, (byte) 0x01,                    //     REPORT_COUNT (1)
        (byte) 0x81, (byte) 0x06,                    //     INPUT (Data,Var,Rel)
        (byte) 0xc0,                  
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值