Demonstration of possible usage of the GattServer

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Mon Mar 25 15:01:13 2019 +0000
Parent:
18:187c849a116a
Commit message:
Merge pull request #225 from paul-szczepanek-arm/master

fix using mixed APIs in Gattserver example
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble

Changed in this revision

source/BLEProcess.h Show annotated file Show diff for this revision Revisions of this file
--- a/source/BLEProcess.h	Wed Feb 27 13:01:46 2019 +0000
+++ b/source/BLEProcess.h	Mon Mar 25 15:01:13 2019 +0000
@@ -128,20 +128,22 @@
         printf("Ble instance initialized\r\n");
 
         Gap &gap = _ble_interface.gap();
-        ble_error_t error = gap.setAdvertisingPayload(make_advertising_data());
-        if (error) {
-            printf("Error %u during gap.setAdvertisingPayload\r\n", error);
+        gap.onConnection(this, &BLEProcess::when_connection);
+        gap.onDisconnection(this, &BLEProcess::when_disconnection);
+
+        if (!set_advertising_parameters()) {
             return;
         }
 
-        gap.setAdvertisingParams(make_advertising_params());
+        if (!set_advertising_data()) {
+            return;
+        }
 
-        gap.onConnection(this, &BLEProcess::when_connection);
-        gap.onDisconnection(this, &BLEProcess::when_disconnection);
+        if (!start_advertising()) {
+            return;
+        }
 
-        start_advertising();
-
-       if (_post_init_cb) {
+        if (_post_init_cb) {
             _post_init_cb(_ble_interface, _event_queue);
         }
     }
@@ -157,45 +159,59 @@
         start_advertising();
     }
 
-    void start_advertising(void)
+    bool start_advertising(void)
     {
-        ble_error_t error = _ble_interface.gap().startAdvertising();
+        Gap &gap = _ble_interface.gap();
+
+        /* Start advertising the set */
+        ble_error_t error = gap.startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
+
         if (error) {
             printf("Error %u during gap.startAdvertising.\r\n", error);
-            return;
+            return false;
         } else {
             printf("Advertising started.\r\n");
+            return true;
         }
     }
 
-    static GapAdvertisingData make_advertising_data(void)
+    bool set_advertising_parameters()
     {
-        static const uint8_t device_name[] = "GattServer";
-        GapAdvertisingData advertising_data;
+        Gap &gap = _ble_interface.gap();
 
-        // add advertising flags
-        advertising_data.addFlags(
-            GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
-            GapAdvertisingData::BREDR_NOT_SUPPORTED
+        ble_error_t error = gap.setAdvertisingParameters(
+            ble::LEGACY_ADVERTISING_HANDLE,
+            ble::AdvertisingParameters()
         );
 
-        // add device name
-        advertising_data.addData(
-            GapAdvertisingData::COMPLETE_LOCAL_NAME,
-            device_name,
-            sizeof(device_name)
+        if (error) {
+            printf("Gap::setAdvertisingParameters() failed with error %d", error);
+            return false;
+        }
+
+        return true;
+    }
+
+    bool set_advertising_data()
+    {
+        Gap &gap = _ble_interface.gap();
+
+        /* Use the simple builder to construct the payload; it fails at runtime
+         * if there is not enough space left in the buffer */
+        ble_error_t error = gap.setAdvertisingPayload(
+            ble::LEGACY_ADVERTISING_HANDLE,
+            ble::AdvertisingDataSimpleBuilder<ble::LEGACY_ADVERTISING_MAX_SIZE>()
+                .setFlags()
+                .setName("GattServer")
+                .getAdvertisingData()
         );
 
-        return advertising_data;
-    }
+        if (error) {
+            printf("Gap::setAdvertisingPayload() failed with error %d", error);
+            return false;
+        }
 
-    static GapAdvertisingParams make_advertising_params(void)
-    {
-        return GapAdvertisingParams(
-            /* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
-            /* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500),
-            /* timeout */ 0
-        );
+        return true;
     }
 
     events::EventQueue &_event_queue;