BLE Central for Magic Light

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light by HM_IOT_Demo

Revision:
2:4b53d13d9851
Parent:
1:2f1203d70643
Child:
3:d6f80e11a7f4
--- a/main.cpp	Sun Nov 22 10:28:59 2015 +0000
+++ b/main.cpp	Sun Nov 22 11:51:48 2015 +0000
@@ -2,10 +2,13 @@
 #include "ble/BLE.h"
 #include "ble/DiscoveredCharacteristic.h"
 #include "ble/DiscoveredService.h"
+#include "ble_radio_notification.h"
 
 BLE ble;
 Serial pc(USBTX, USBRX);
 
+DiscoveredCharacteristic accelChar;
+
 const uint8_t MPU6050_service_uuid[] = {
     0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
 };
@@ -24,7 +27,7 @@
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
            params->rssi, params->isScanResponse, params->type);
     
-    ble.stopScan();
+    
     ble.connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
 }
 
@@ -34,12 +37,31 @@
 
 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
     pc.printf("characteristicDiscoveryCallback\r\n");
-    ble.gattClient().read(characteristicP->getConnHandle(), characteristicP->getValueHandle(), 0);
+    
+    const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
+    bool flag = true;
+
+    if (characteristicP->getUUID().shortOrLong() != UUID::UUID_TYPE_SHORT) {
+        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++){
+            if( longUUIDBytes[i]!=MPU6050_Accel_Characteristic_uuid[i]){
+                flag=false;
+            }
+        }
+        if(flag == true) {
+            accelChar = *characteristicP;
+            ble.gattClient().read(characteristicP->getConnHandle(), characteristicP->getValueHandle(), 0);
+        }
+    }
+
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
     pc.printf("GAP_EVT_CONNECTED");
-    ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
+    
+    if (params->role == Gap::CENTRAL) {
+        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
+        //ble.stopScan();
+    }
 }
 
 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
@@ -52,10 +74,18 @@
         pc.printf("data %x", response->data[i]);  
     }
     pc.printf("\r\n");
+    accelChar.read();
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){//(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
     pc.printf("disconnected\r\n");
+    wait(8.0);
+    ble.gap().startScan(scanCallback);
+}
+
+void your_radio_callback_handler(bool radio_active)
+{
+
 }
 
 int main(void) {
@@ -64,6 +94,9 @@
     pc.printf("start\r\n");
 
     ble.init();
+    ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
+                                NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
+                                your_radio_callback_handler);
     ble.onConnection(connectionCallback);
     ble.onDisconnection(disconnectionCallback);
     ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);