본문 바로가기
프로젝트/Android Bluetooth HID

[Android] Bluetooth HID 키보드 앱 만들기 2. Bluetooth 연결/전송

by 바디스 2021. 3. 4.

Bluetooth 연결

BluetoothHIDdevice를 이용하여 핸드폰을 키보드로 인식되도록 하고 연결한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(
        "BleHidKeyboard",
        "Android BLE HID Keyboard",
        "Android",
        (byte0x00,
        descriptor
);
 
 
mBtHidDevice.registerApp(sdp, null, mBluetoothHidDeviceAppQosSettings, 
                         Executors.newSingleThreadExecutor(), new BluetoothHidDevice.Callback() {
    ... //omitted
});
 
mBtHidDevice.connect(device);
cs

 

 

SendReport  - 전송

Hid Device 종류마다 보내야할 데이터 형식이 다르다. 키보드의 경우 modifier , reservedkey6개의 형태로 데이터를 전송합니다.

MODIFIER는 CTRL, ALT, SHIFT 같은 특수키 입력을 뜻한다.

Bit Key
0 LEFT CTRL
1 LEFT SHIFT
2 LEFT ALT
3 LEFT GUI
4 RIGHT CTRL
5 RIGHT SHIFT
6 RIGHT ALT
7 RIGHT GUI

전송 형식에 따라 키보드는 여러개의 동시 입력이 가능하지만 현재 코드에서는 1개만 입력 받도록 하였습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void sendReport() {        
    byte state;
    byte modify;
 
    ...// get keyboard modify/state
 
    Log.d(TAG, "sendReport(): " + state +modify);
    for (BluetoothDevice btDev : mBtHidDevice.getConnectedDevices()) {
        mBtHidDevice.sendReport(btDev, 1new byte[]{
            modify,
            0,
            0,
            0,
            0,
            0,
            state,
        });
    }
}
cs

이렇게 sendReport로 신호를 보내면 해당 키 입력이 PC로 입력됩니다.

https://github.com/person003333/BluetoothHIDKeyboard

댓글