This example demonstrates using the GattClient API to control BLE client devices. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_LEDBlinker

Revision:
81:816977af0b75
Parent:
77:85a0d3cdd896
--- a/source/main.cpp	Wed Feb 27 13:02:16 2019 +0000
+++ b/source/main.cpp	Thu Mar 07 09:38:15 2019 +0000
@@ -21,9 +21,11 @@
 #include "ble/DiscoveredService.h"
 #include "ble/gap/Gap.h"
 #include "ble/gap/AdvertisingDataParser.h"
+#include "LEDService.h"
 #include "pretty_printer.h"
 
 const static char PEER_NAME[] = "LED";
+const static char DEVICE_NAME[] = "LED";
 
 static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
 
@@ -91,7 +93,10 @@
         _event_queue(event_queue),
         _alive_led(LED1, 1),
         _actuated_led(LED2, 0),
-        _is_connecting(false) { }
+        _is_connecting(false),
+        _led_uuid(LEDService::LED_SERVICE_UUID),
+        _led_service(NULL),
+        _adv_data_builder(_adv_buffer) { }
 
     ~LEDBlinkerDemo() { }
 
@@ -105,6 +110,50 @@
         _event_queue.dispatch_forever();
     }
 
+    void start_advertising() {
+        /* Create advertising parameters and payload */
+
+        ble::AdvertisingParameters adv_parameters(
+            ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
+            ble::adv_interval_t(ble::millisecond_t(1000))
+        );
+
+        _adv_data_builder.setFlags();
+        _adv_data_builder.setLocalServiceList(mbed::make_Span(&_led_uuid, 1));
+        _adv_data_builder.setName(DEVICE_NAME);
+
+        /* Setup advertising */
+
+        ble_error_t error = _ble.gap().setAdvertisingParameters(
+            ble::LEGACY_ADVERTISING_HANDLE,
+            adv_parameters
+        );
+
+        if (error) {
+            printf("_ble.gap().setAdvertisingParameters() failed\r\n");
+            return;
+        }
+
+        error = _ble.gap().setAdvertisingPayload(
+            ble::LEGACY_ADVERTISING_HANDLE,
+            _adv_data_builder.getAdvertisingData()
+        );
+
+        if (error) {
+            printf("_ble.gap().setAdvertisingPayload() failed\r\n");
+            return;
+        }
+
+        /* Start advertising */
+
+        error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
+
+        if (error) {
+            printf("_ble.gap().startAdvertising() failed\r\n");
+            return;
+        }
+    }
+
 private:
     /** Callback triggered when the ble initialization process has finished */
     void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
@@ -118,6 +167,9 @@
         _ble.gattClient().onDataRead(trigger_toggled_write);
         _ble.gattClient().onDataWritten(trigger_read);
 
+        _led_service = new LEDService(_ble, false);
+        _ble.gattServer().onDataWritten(this, &LEDBlinkerDemo::on_data_written);
+
         ble::ScanParameters scan_params;
         _ble.gap().setScanParameters(scan_params);
         _ble.gap().startScan();
@@ -136,6 +188,7 @@
     }
 
     void onConnectionComplete(const ble::ConnectionCompleteEvent& event) {
+        printf("onConnectionComplete\r\n");
         if (event.getOwnRole() == ble::connection_role_t::CENTRAL) {
             _ble.gattClient().onServiceDiscoveryTermination(discovery_termination);
             _ble.gattClient().launchServiceDiscovery(
@@ -145,6 +198,10 @@
                 0xa000,
                 0xa001
             );
+            printf("Connected as Central, start advertising\r\n");
+            start_advertising();
+        } else if (event.getOwnRole() == ble::connection_role_t::PERIPHERAL) {
+            printf("Connected as Peripheral\r\n");
         } else {
             _ble.gap().startScan();
         }
@@ -203,14 +260,34 @@
         }
     }
 
+     /**
+     * This callback allows the LEDService to receive updates to the ledState Characteristic.
+     *
+     * @param[in] params Information about the characterisitc being updated.
+     */
+    void on_data_written(const GattWriteCallbackParams *params) {
+        printf("led_service getValueHandle() %x\r\n", _led_service->getValueHandle());
+        printf("on_data_written, handle:%x len:%d\r\n", params->handle, params->len);
+        if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) {
+            _actuated_led = *(params->data);
+        }
+    }
+
 private:
     BLE &_ble;
     events::EventQueue &_event_queue;
     DigitalOut _alive_led;
     DigitalOut _actuated_led;
     bool _is_connecting;
+
+    UUID _led_uuid;
+    LEDService *_led_service;
+
+    uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
+    ble::AdvertisingDataBuilder _adv_data_builder;
 };
 
+
 /** Schedule processing of events from the BLE middleware in the event queue. */
 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
     event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));