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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android Bluetooth HID Device模拟蓝牙键盘鼠标

發布時間:2023/12/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Bluetooth HID Device模拟蓝牙键盘鼠标 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自Android 9開放BluetoothHidDevice功能后,Android平臺可以很簡單的通過BluetoothHidDevice模擬鍵盤鼠標等藍牙hid device角色。

Step 1:獲取BluetoothHidDevice實例

mBluetoothAdapter.getProfileProxy(ctx, mProfileServiceListener, BluetoothProfile.HID_DEVICE);

Step 2:注冊/解除注冊HID實例

public BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {@Overridepublic void onServiceDisconnected(int profile) {if (profile == BluetoothProfile.HID_DEVICE) {//解除hid device注冊mHidDevice.unregisterApp();}}@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.HID_DEVICE) {mHidDevice = (BluetoothHidDevice) proxy;//注冊hid deviceBluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(HidConfig.NAME, HidConfig.DESCRIPTION, HidConfig.PROVIDER, BluetoothHidDevice.SUBCLASS1_COMBO, HidConfig.KEYBOARD_DESCRIPTOR);mHidDevice.registerApp(sdp, null, null, Executors.newCachedThreadPool(), mCallback);}}};public final BluetoothHidDevice.Callback mCallback = new BluetoothHidDevice.Callback() {@Overridepublic void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) {IsRegisted = registered;}@Overridepublic void onConnectionStateChanged(BluetoothDevice device, int state) {if (state == BluetoothProfile.STATE_DISCONNECTED) {connected = false;Log.d(TAG, "hid state is disconnected");} else if (state == BluetoothProfile.STATE_CONNECTED) {connected = true;Log.d(TAG, "hid state is connected");} else if (state == BluetoothProfile.STATE_CONNECTING) {Log.d(TAG, "hid state is connecting");}}};

在獲取到BluetoothHidDevice實例后通過registerApp注冊hid device,此時hid host角色會被禁用,因此在不需要hid device功能時要及時解除hid device的注冊。

registerApp函數中最重要的一個參數BluetoothHidDeviceAppSdpSettings,主要是給對端host提供hid device角色的名稱,描述信息,供應商信息,以及Hid device的Reports Descriptor

Hid report description描述符生成工具:USB官網HID報告描述符生成工具 - USB中文網

public class HidConfig {public final static String NAME = "My Keyboard";public final static String DESCRIPTION = "Lgd's Keyboard";public final static String PROVIDER = "Lgd";public static final byte[] KEYBOARD_DESCRIPTOR ={(byte) 0x05, (byte) 0x01, // USAGE_PAGE (Generic Desktop)(byte) 0x09, (byte) 0x06, // USAGE (Keyboard)(byte) 0xa1, (byte) 0x01, // COLLECTION (Application)(byte) 0x85, (byte) 0x02, //REPORT_ID (2)(byte) 0x05, (byte) 0x07, // USAGE_PAGE (Keyboard)(byte) 0x19, (byte) 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)(byte) 0x29, (byte) 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)(byte) 0x15, (byte) 0x00, // LOGICAL_MINIMUM (0)(byte) 0x25, (byte) 0x01, // LOGICAL_MAXIMUM (1)(byte) 0x75, (byte) 0x01, // REPORT_SIZE (1)(byte) 0x95, (byte) 0x08, // REPORT_COUNT (8)(byte) 0x81, (byte) 0x02, // INPUT (Data,Var,Abs)(byte) 0x95, (byte) 0x01, // REPORT_COUNT (1)(byte) 0x75, (byte) 0x08, // REPORT_SIZE (8)(byte) 0x81, (byte) 0x03, // INPUT (Cnst,Var,Abs)(byte) 0x95, (byte) 0x05, // REPORT_COUNT (5)(byte) 0x75, (byte) 0x01, // REPORT_SIZE (1)(byte) 0x05, (byte) 0x08, // USAGE_PAGE (LEDs)(byte) 0x19, (byte) 0x01, // USAGE_MINIMUM (Num Lock)(byte) 0x29, (byte) 0x05, // USAGE_MAXIMUM (Kana)(byte) 0x91, (byte) 0x02, // OUTPUT (Data,Var,Abs)(byte) 0x95, (byte) 0x01, // REPORT_COUNT (1)(byte) 0x75, (byte) 0x03, // REPORT_SIZE (3)(byte) 0x91, (byte) 0x03, // OUTPUT (Cnst,Var,Abs)(byte) 0x95, (byte) 0x06, // REPORT_COUNT (6)(byte) 0x75, (byte) 0x08, // REPORT_SIZE (8)(byte) 0x15, (byte) 0x00, // LOGICAL_MINIMUM (0)(byte) 0x25, (byte) 0x65, // LOGICAL_MAXIMUM (101)(byte) 0x05, (byte) 0x07, // USAGE_PAGE (Keyboard)(byte) 0x19, (byte) 0x00, // USAGE_MINIMUM (Reserved (no event indicated))(byte) 0x29, (byte) 0x65, // USAGE_MAXIMUM (Keyboard Application)(byte) 0x81, (byte) 0x00, // INPUT (Data,Ary,Abs)(byte) 0xc0 // END_COLLECTION};}

上面的描述符只是適配鍵盤,可以不定義report id,如果是多個hid功能復合的設備,例如復合鍵盤鼠標,就需要再添加鼠標的報告描述符,同時每個功能都需要有對應的report id。

  • 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  • 0x09, 0x02, // USAGE (Mouse)
  • 0xa1, 0x01, // COLLECTION (Application)
  • 0x85, 0x01, // REPORT_ID (1)
  • 0x75, 0x10, // REPORT_SIZE (16)
  • 0x95, 0x01, // REPORT_COUNT (1)
  • 0x15, 0x0b, // LOGICAL_MINIMUM (11)
  • 0x25, 0x1c, // LOGICAL_MAXIMUM (28)
  • 0x09, 0x30, // USAGE (X)
  • 0x81, 0x22, // INPUT (Data,Var,Abs,NPrf)
  • 0xc0, // END_COLLECTION
  • 0x09, 0x06, // USAGE (Keyboard)
  • 0xa1, 0x01, // COLLECTION (Application)
  • 0x85, 0x02, // REPORT_ID (2)
  • 0x75, 0x08, // REPORT_SIZE (8)
  • 0x95, 0x01, // REPORT_COUNT (1)
  • 0x15, 0x00, // LOGICAL_MINIMUM (0)
  • 0x25, 0x40, // LOGICAL_MAXIMUM (64)
  • 0x09, 0x31, // USAGE (Y)
  • 0x81, 0x02, // INPUT (Data,Var,Abs)
  • 0xc0
  • Setp 4:通過sendReport想host發送按鍵信息

    第二個參數是report description中定義的report id,如果沒有定義傳入0。

    第三個參數是按鍵數據,根據report description,總共有8字節,前2字節是功能鍵,后6字節是對應的鍵值信息

    mHidDevice.sendReport(device, 2, new byte[]{0, 0, (byte) code, 0, 0, 0, 0, 0});

    Hid相關資料請參考:HID 簡介 - USB中文網

    總結

    以上是生活随笔為你收集整理的Android Bluetooth HID Device模拟蓝牙键盘鼠标的全部內容,希望文章能夠幫你解決所遇到的問題。

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