AndroidにキーボードとしてBLEモジュールを認識させて、ショートカットを叩けます。 ショートカットキー btn1:Alt + Tab btn2: Enter

Dependencies:   BLE_API mbed nRF51822

Committer:
leibun
Date:
Tue Sep 15 02:53:19 2015 +0000
Revision:
0:f9f11f6ee09d
First commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leibun 0:f9f11f6ee09d 1 #include "mbed.h"
leibun 0:f9f11f6ee09d 2 #include "BLE.h"
leibun 0:f9f11f6ee09d 3 #include "BatteryService.h"
leibun 0:f9f11f6ee09d 4 #include "DeviceInformationService.h"
leibun 0:f9f11f6ee09d 5 #include "HIDService.h"
leibun 0:f9f11f6ee09d 6
leibun 0:f9f11f6ee09d 7 BLEDevice ble;
leibun 0:f9f11f6ee09d 8
leibun 0:f9f11f6ee09d 9 Serial uart(USBTX, USBRX);
leibun 0:f9f11f6ee09d 10 DigitalOut led01(LED1);
leibun 0:f9f11f6ee09d 11 DigitalOut RFSWIO(LED2);
leibun 0:f9f11f6ee09d 12 Ticker flipper;
leibun 0:f9f11f6ee09d 13 DigitalIn btn01(P0_17);
leibun 0:f9f11f6ee09d 14 DigitalIn btn02(P0_16);
leibun 0:f9f11f6ee09d 15 DigitalIn btn03(P0_21);
leibun 0:f9f11f6ee09d 16
leibun 0:f9f11f6ee09d 17 bool flip_lock = false;
leibun 0:f9f11f6ee09d 18
leibun 0:f9f11f6ee09d 19 void flip() {
leibun 0:f9f11f6ee09d 20 if (!flip_lock) RFSWIO = !RFSWIO;
leibun 0:f9f11f6ee09d 21 }
leibun 0:f9f11f6ee09d 22
leibun 0:f9f11f6ee09d 23 unsigned char keyData;
leibun 0:f9f11f6ee09d 24 static const char DEVICE_NAME[] = "HID_Android_ShortCutKey";
leibun 0:f9f11f6ee09d 25 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE};
leibun 0:f9f11f6ee09d 26 static volatile bool triggerSensorPolling = false;
leibun 0:f9f11f6ee09d 27
leibun 0:f9f11f6ee09d 28 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
leibun 0:f9f11f6ee09d 29 {
leibun 0:f9f11f6ee09d 30 ble.startAdvertising(); // restart advertising
leibun 0:f9f11f6ee09d 31 }
leibun 0:f9f11f6ee09d 32
leibun 0:f9f11f6ee09d 33 char msg[25] = "NNN40 CONNECTED\n";
leibun 0:f9f11f6ee09d 34 int main(void)
leibun 0:f9f11f6ee09d 35 {
leibun 0:f9f11f6ee09d 36 btn01.mode(PullUp);
leibun 0:f9f11f6ee09d 37 btn02.mode(PullUp);
leibun 0:f9f11f6ee09d 38 btn03.mode(PullUp);
leibun 0:f9f11f6ee09d 39
leibun 0:f9f11f6ee09d 40 uart.baud(115200);
leibun 0:f9f11f6ee09d 41 uart.printf("Starting HID Service\r\n");
leibun 0:f9f11f6ee09d 42 RFSWIO = 1;
leibun 0:f9f11f6ee09d 43 led01 = 1;
leibun 0:f9f11f6ee09d 44 memset(msg, 0, 25);
leibun 0:f9f11f6ee09d 45 /*======BLE setup========*/
leibun 0:f9f11f6ee09d 46 ble.init();
leibun 0:f9f11f6ee09d 47 bool enableBonding = true;
leibun 0:f9f11f6ee09d 48 bool requireMITM = true;
leibun 0:f9f11f6ee09d 49 ble.initializeSecurity(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE); //IO_CAPS_DISPLAY_ONLY, IO_CAPS_NONE
leibun 0:f9f11f6ee09d 50 ble.onDisconnection(disconnectionCallback);
leibun 0:f9f11f6ee09d 51
leibun 0:f9f11f6ee09d 52 /* Setup primary service. */
leibun 0:f9f11f6ee09d 53 HIDService hidService(ble);
leibun 0:f9f11f6ee09d 54 /* Setup auxiliary service. */
leibun 0:f9f11f6ee09d 55 DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
leibun 0:f9f11f6ee09d 56 /* Setup advertising. */
leibun 0:f9f11f6ee09d 57 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
leibun 0:f9f11f6ee09d 58 ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
leibun 0:f9f11f6ee09d 59 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
leibun 0:f9f11f6ee09d 60 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
leibun 0:f9f11f6ee09d 61 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
leibun 0:f9f11f6ee09d 62 ble.setAdvertisingInterval(1000);
leibun 0:f9f11f6ee09d 63
leibun 0:f9f11f6ee09d 64 RFSWIO = 1;
leibun 0:f9f11f6ee09d 65 ble.startAdvertising();
leibun 0:f9f11f6ee09d 66 uart.printf("Starting advertising\r\n");
leibun 0:f9f11f6ee09d 67 wait(5);
leibun 0:f9f11f6ee09d 68 flipper.attach(&flip, 0.15);
leibun 0:f9f11f6ee09d 69
leibun 0:f9f11f6ee09d 70 while (1) {
leibun 0:f9f11f6ee09d 71 if(ble.getGapState().connected){
leibun 0:f9f11f6ee09d 72 if(!btn01){
leibun 0:f9f11f6ee09d 73 hidService.updateReport(0x04, 0x2B);//0x04 ALT(LEFT) 0x2B Keyboard TAB
leibun 0:f9f11f6ee09d 74 wait(0.03);
leibun 0:f9f11f6ee09d 75 hidService.updateReport(0x04, 0x00);//0x04 ALT(LEFT) 0x2B
leibun 0:f9f11f6ee09d 76 wait(0.5);
leibun 0:f9f11f6ee09d 77 }
leibun 0:f9f11f6ee09d 78 if(!btn02){
leibun 0:f9f11f6ee09d 79 hidService.updateReport(0x00, 0x28);//0x28 Keyboard RETURN(Enter)
leibun 0:f9f11f6ee09d 80 wait(0.03);
leibun 0:f9f11f6ee09d 81 hidService.updateReport(0x00, 0x00);
leibun 0:f9f11f6ee09d 82 wait(0.5);
leibun 0:f9f11f6ee09d 83 }
leibun 0:f9f11f6ee09d 84 if(!btn03){
leibun 0:f9f11f6ee09d 85 hidService.updateReport(0x08, 0x07);//0x08 Left GUI 0x07 Keyboard d
leibun 0:f9f11f6ee09d 86 wait(0.03);
leibun 0:f9f11f6ee09d 87 hidService.updateReport(0x00, 0x00);
leibun 0:f9f11f6ee09d 88 wait(0.5);
leibun 0:f9f11f6ee09d 89 }
leibun 0:f9f11f6ee09d 90 }
leibun 0:f9f11f6ee09d 91 }
leibun 0:f9f11f6ee09d 92 }