Demo Glucose Service

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

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

Revision:
67:aa3a1bbed328
Parent:
66:72ca05b7a092
--- a/main.cpp	Thu Jul 02 01:32:04 2015 +0000
+++ b/main.cpp	Thu Jul 02 05:25:19 2015 +0000
@@ -1,91 +1,71 @@
 #include "mbed.h"
 #include "BLE.h"
-#include "BatteryService.h"
 #include "DeviceInformationService.h"
-#include "HIDService.h"
+#include "GlucoseService.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};
+ 
+const static char     DEVICE_NAME[]        = "Glucose Machine";
+static const uint16_t uuid16_list[]        = {GattService::UUID_GLUCOSE_SERVICE,
+                                              GattService::UUID_DEVICE_INFORMATION_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];
+ 
+void periodicCallback(void)
+{
+    led01 = !led01; /* Do blinky on LED1 while we're waiting for BLE events */
+    triggerSensorPolling = true;
+}
+ 
 int main(void)
-{   uart.baud(9600);
-    uart.printf("Starting HID Service\n");
-    //uart.attach(&uart_rx);
+{   uart.printf("Starting Glucose Service\n");
     led01 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+ 
     ble.init();
-    bool enableBonding = false;
-    bool requireMITM   = false;
-    ble.initializeSecurity(enableBonding, requireMITM);
     ble.onDisconnection(disconnectionCallback);
  
     /* Setup primary service. */
-    HIDService hidService(ble);
+    float glucoseValue = 100.0f; // 100 Kg/L
+    GlucoseService GluService(ble, glucoseValue);
+ 
     /* Setup auxiliary service. */
-    DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+    DeviceInformationService deviceInfo(ble, "ARM", "Model1", "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 (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);
+        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);
                 }
-            index = 0;
-            memset(modifyKey, 0, 30);
-            memset(key_press_scan_buff, 0, 30);
-            }
-            
+        } else {
+            ble.waitForEvent();
         }
     }
-}
+}
\ No newline at end of file