Demo Glucose Service
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
BLE_Glucose_demo implements the Glucose Service which enables a collector device to connect and interact with.
There is a brief sample code edited with Android Studio for demo this BLE_Glucose_demo example, and it is public on github, everyone can clone it by this URL: https://github.com/Marcomissyou/BluetoothLeGlucose.git. It is convenient for you to development your BLE idea.
There is also provided apk file so you can download and install it directly then demo this code, but make sure your Android phone supports Bluetooth 4.0. /media/uploads/Marcomissyou/bleglucoseservice.apk
Diff: main.cpp
- Revision:
- 65:e419508fefc4
- Parent:
- 63:fc6117c32419
- Child:
- 66:72ca05b7a092
--- a/main.cpp Fri May 29 07:16:24 2015 +0000 +++ b/main.cpp Tue Jun 30 03:00:11 2015 +0000 @@ -1,74 +1,91 @@ #include "mbed.h" -#include "BLEDevice.h" +#include "BLE.h" +#include "BatteryService.h" #include "DeviceInformationService.h" -#include "GlucoseService.h" - -#define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0 +#include "HIDService.h" BLEDevice ble; DigitalOut led01(LED1); Serial uart(p25,p23); - -const static char DEVICE_NAME[] = "Glucose Machine"; -static const uint16_t uuid16_list[] = {GattService::UUID_GLUCOSE_SERVICE, - GattService::UUID_DEVICE_INFORMATION_SERVICE}; + +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 } - -void periodicCallback(void) -{ - led01 = !led01; /* Do blinky on LED1 while we're waiting for BLE events */ - triggerSensorPolling = true; -} - + + +static uint8_t key_press_scan_buff[30]; +static uint8_t modifyKey[30]; int main(void) -{ uart.printf("Starting Glucose Service\n"); +{ uart.baud(9600); + uart.printf("Starting HID Service\n"); + //uart.attach(&uart_rx); led01 = 1; - Ticker ticker; - ticker.attach(periodicCallback, 1); - ble.init(); + bool enableBonding = true; + bool requireMITM = false; + ble.initializeSecurity(enableBonding, requireMITM); ble.onDisconnection(disconnectionCallback); /* Setup primary service. */ - float glucoseValue = 100.0f; // 100 Kg/L - GlucoseService GluService(ble, glucoseValue); - + HIDService hidService(ble); /* Setup auxiliary service. */ - DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); + 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.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_GLUCOSE_METER); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(1000); ble.startAdvertising(); - + uart.printf("Starting advertising\n"); + int index = 0; while (1) { - if (triggerSensorPolling && ble.getGapState().connected) { - triggerSensorPolling = false; - glucoseValue = glucoseValue + 1.0f; - GluService.updateGlucoseMeasurement(glucoseValue); - if (glucoseValue > 175.0f) { - glucoseValue = 100.0f; - } - if(GluService.is_GluReceived()){ - uint8_t *RxBuffer; - RxBuffer = GluService.getControlPoint(); - GluService.CleanFlag(); - printf("Receive data Op Code = %d\n", RxBuffer[0]-48); - printf("Receive data Operator = %d\n", RxBuffer[1]-48); - printf("Receive data Operand[0] = %d\n", RxBuffer[2]-48); - printf("Receive data Operand[1] = %d\n", RxBuffer[3]-48); + 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); } - - } else { - ble.waitForEvent(); + index = 0; + memset(modifyKey, 0, 30); + memset(key_press_scan_buff, 0, 30); + } + } } -} \ No newline at end of file +}