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函數(shù)中最重要的一個參數(shù)BluetoothHidDeviceAppSdpSettings,主要是給對端host提供hid device角色的名稱,描述信息,供應(yīng)商信息,以及Hid device的Reports Descriptor。
Hid report description描述符生成工具:USB官網(wǎng)HID報告描述符生成工具 - USB中文網(wǎng)
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功能復(fù)合的設(shè)備,例如復(fù)合鍵盤鼠標,就需要再添加鼠標的報告描述符,同時每個功能都需要有對應(yīng)的report id。
Setp 4:通過sendReport想host發(fā)送按鍵信息
第二個參數(shù)是report description中定義的report id,如果沒有定義傳入0。
第三個參數(shù)是按鍵數(shù)據(jù),根據(jù)report description,總共有8字節(jié),前2字節(jié)是功能鍵,后6字節(jié)是對應(yīng)的鍵值信息
mHidDevice.sendReport(device, 2, new byte[]{0, 0, (byte) code, 0, 0, 0, 0, 0});Hid相關(guān)資料請參考:HID 簡介 - USB中文網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的Android Bluetooth HID Device模拟蓝牙键盘鼠标的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java简体繁体转换
- 下一篇: 奥克兰oracle,IEM奥克兰前瞻: