HID scanner demo

Dependencies:   BLE_API WIFI_API_32kRAM nRF51822 mbed

BLE HID function in this demo is temporary working with Android phone ONLY. Will be fixed to support iOS device in next update.

Revision:
0:b0fc0661c081
Child:
1:51535675abf4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 02 01:35:08 2015 +0000
@@ -0,0 +1,91 @@
+#include "mbed.h"
+#include "BLE.h"
+#include "BatteryService.h"
+#include "DeviceInformationService.h"
+#include "HIDService.h"
+ 
+BLEDevice  ble;
+DigitalOut led01(LED1);
+Serial uart(p25,p23);
+
+unsigned char keyData;
+bool is_input = false; 
+static const char     DEVICE_NAME[]        = "HID_Keyboard";
+static const uint16_t uuid16_list[]        = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE};
+static volatile bool  triggerSensorPolling = false;
+
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    ble.startAdvertising(); // restart advertising
+}
+
+
+static uint8_t key_press_scan_buff[30];
+static uint8_t modifyKey[30];
+int main(void)
+{   uart.baud(9600);
+    uart.printf("Starting HID Service\n");
+    //uart.attach(&uart_rx);
+    led01 = 1;
+    ble.init();
+    bool enableBonding = false;
+    bool requireMITM   = false;
+    ble.initializeSecurity(enableBonding, requireMITM);
+    ble.onDisconnection(disconnectionCallback);
+ 
+    /* Setup primary service. */
+    HIDService hidService(ble);
+    /* Setup auxiliary service. */
+    DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+    /* Setup advertising. */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.setAdvertisingInterval(1000);
+    ble.startAdvertising();
+    uart.printf("Starting advertising\n");
+    int index = 0;
+    while (1) {
+        if (uart.readable() == 1) {
+            keyData = uart.getc();
+            uart.putc(keyData);
+            if(keyData <= 0x39 && keyData >= 0x30){             //number
+                if(keyData == 0x30){
+                    modifyKey[index] = 0x00;
+                    key_press_scan_buff[index] = 0x27;
+                    } else {
+                    modifyKey[index] = 0x00;
+                    key_press_scan_buff[index] = keyData-0x13;
+                    }
+                } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters
+                    modifyKey[index] = 0x00;
+                    key_press_scan_buff[index] = keyData-0x5d;
+                } else if(keyData <= 0x5a && keyData >= 0x41){  //uppercase letters
+                    modifyKey[index] = 0x02;
+                    key_press_scan_buff[index] = keyData-0x3d;
+                } else if (keyData == 0x20) {
+                    modifyKey[index] = 0x00;
+                    key_press_scan_buff[index] = 0x2c;
+                } else {
+                    modifyKey[index] = 0x00;
+                    //key_press_scan_buff[index] = 0x28;
+                    //key_press_scan_buff[index++] = 0x73;
+                    key_press_scan_buff[index] = 0x73;
+            }
+            index++;
+            if(keyData == 0x0a && ble.getGapState().connected){
+                for(int i = 0; i < index; i++){
+                //uart.putc(key_press_scan_buff[i]);
+                hidService.updateReport(modifyKey[i], key_press_scan_buff[i]);
+                wait(0.1);
+                }
+            index = 0;
+            memset(modifyKey, 0, 30);
+            memset(key_press_scan_buff, 0, 30);
+            }
+            
+        }
+    }
+}