Example for BLE HID scanner

Fork of BLE_HIDScanner_DELTA by Silvia Chen

Revision:
5:8f0f8b8a85eb
Parent:
4:fd7572166cbb
--- a/main.cpp	Wed Jul 26 03:50:46 2017 +0000
+++ b/main.cpp	Wed Jul 26 09:55:34 2017 +0000
@@ -21,6 +21,8 @@
 #include "HIDService.h"
 #include "ble/services/UARTService.h"
 
+#define KEY_BUF_SIZE 50  //Buffer used to store keys to send (store up to 25 characters)
+
 //DigitalOut led1(LED1);
 Serial uart(USBTX, USBRX);
 InterruptIn testHID_btn(BUTTON1);
@@ -36,8 +38,8 @@
 bool isSecuritySetup = false;
 unsigned char serial_buf[20];
 uint16_t serialLen = 0;
-static uint8_t key_press_scan_buff[50];
-static uint8_t modifyKey[50];
+static uint8_t key_press_scan_buff[KEY_BUF_SIZE];
+static uint8_t modifyKey[KEY_BUF_SIZE];
 int index_b = 0;
 
 DeviceInformationService *deviceInfo;
@@ -199,7 +201,6 @@
     ble.gap().onDisconnection(disconnectionCallback);
     ble.gap().onConnection(onConnectionCallback);
     ble.gap().onTimeout(onTimeoutCallback);
-    //ble.gattServer().onDataRead(onDataReadCallback);
     ble.gattServer().onDataWritten(serverDataWrittenCallback);
     ble.gattServer().onDataSent(dataSentCallback);
 
@@ -247,22 +248,26 @@
                 keyData = uart.getc();
                 //uart.printf("keyData: %c\r\n", keyData);
                 transferReportData(keyData);
-                if(keyData == 0x0a && ble.getGapState().connected){
+                if(keyData == 0x0a && ble.getGapState().connected){ //detecting LF and check the ble state
                     for(int i = 0; i < index_b ; i++){
-                        //uart.printf("m[%x] k[%x] ", modifyKey[i], key_press_scan_buff[i]);
                         hidService->updateReport(modifyKey[i], key_press_scan_buff[i]);
                         ble.waitForEvent();
-                        wait(0.01);
+                        wait(0.01); //This is necessary
                     }
             
                     index_b = 0;
-                    memset(modifyKey, 0, 50);
-                    memset(key_press_scan_buff, 0, 50);
+                    memset(modifyKey, 0, KEY_BUF_SIZE);
+                    memset(key_press_scan_buff, 0, KEY_BUF_SIZE);
                 }
             } else if(triggerSensorPolling) {
                 //Demo HID/Serial at the same time
                 triggerSensorPolling = false;
                 
+                //Write data via RX characteristic
+                ble.gattServer().write(uartServicePtr->getRXCharacteristicHandle(), static_cast<const uint8_t *>(serial_buf), serialLen);
+                
+                /*
+                //Write data via HID
                 for(int j=0;j<serialLen;j++) {
                     keyData = serial_buf[j];
                     //uart.printf("keyData: %c\r\n", keyData);
@@ -277,8 +282,9 @@
                 }
             
                 index_b = 0;
-                memset(modifyKey, 0, 50);
-                memset(key_press_scan_buff, 0, 50);
+                memset(modifyKey, 0, KEY_BUF_SIZE);
+                memset(key_press_scan_buff, 0, KEY_BUF_SIZE);
+                */
             } else {
                 ble.waitForEvent();
             }