skaner

Dependencies:   BLE_API TextLCD mbed nRF51822

Fork of BLE_LEDBlinker by Bluetooth Low Energy

Revision:
11:023d96b0e427
Parent:
10:507318f2afda
Child:
12:61812ca1f049
--- a/main.cpp	Tue Sep 29 11:52:34 2015 +0000
+++ b/main.cpp	Tue Jan 12 10:49:03 2016 +0000
@@ -19,25 +19,26 @@
 #include "ble/DiscoveredCharacteristic.h"
 #include "ble/DiscoveredService.h"
 
-BLE ble;
+DigitalOut alivenessLED(LED1, 1);
 
-DigitalOut alivenessLED(LED1, 1);
-bool triggerLedCharacteristic = false;
+bool                     triggerLedCharacteristic = false;
 DiscoveredCharacteristic ledCharacteristic;
 
+Ticker ticker;
+
 void periodicCallback(void) {
     alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
 }
 
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
-    if (params->peerAddr[0] != 0x29) { /* !ALERT! Alter this filter to suit your device. */
+    if (params->peerAddr[0] != 0x37) { /* !ALERT! Alter this filter to suit your device. */
         return;
     }
     printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
            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.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+    BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
 }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
@@ -67,8 +68,8 @@
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
     if (params->role == Gap::CENTRAL) {
-        ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
-        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
+        BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+        BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
     }
 }
 
@@ -97,11 +98,33 @@
     printf("disconnected\r\n");
 }
 
-int main(void) {
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    /* Initialization error handling should go here */
+}
 
-    ble.init();
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+ 
     ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
 
@@ -110,6 +133,17 @@
 
     ble.gap().setScanParams(500, 400);
     ble.gap().startScan(advertisementCallback);
+}
+
+int main(void) {
+    ticker.attach(periodicCallback, 1);
+
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
 
     while (true) {
         if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {