This application shows how to use GAP to transmit a simple value to disconnected peer listening for advertisement every time that a value is updated. The canonical source for this example lives at: https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_GAPButton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <events/mbed_events.h>
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "pretty_printer.h"
00021 
00022 static const char DEVICE_NAME[] = "GAPButton";
00023 
00024 static EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00025 
00026 /** Demonstrate advertising, scanning and connecting
00027  */
00028 class GapButtonDemo : private mbed::NonCopyable<GapButtonDemo>, public ble::Gap::EventHandler
00029 {
00030 public:
00031     GapButtonDemo(BLE& ble, events::EventQueue& event_queue) :
00032         _ble(ble),
00033         _event_queue(event_queue),
00034         _led1(LED1, 0),
00035         /* We can arbiturarily choose the GAPButton service UUID to be 0xAA00
00036          * as long as it does not overlap with the UUIDs defined here:
00037          * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx */
00038         _button_uuid(0xAA00),
00039         _button(BLE_BUTTON_PIN_NAME, BLE_BUTTON_PIN_PULL),
00040         _adv_data_builder(_adv_buffer) { }
00041 
00042     ~GapButtonDemo()
00043     {
00044         if (_ble.hasInitialized()) {
00045             _ble.shutdown();
00046         }
00047     }
00048 
00049     /** Start BLE interface initialisation */
00050     void run()
00051     {
00052         if (_ble.hasInitialized()) {
00053             printf("Ble instance already initialised.\r\n");
00054             return;
00055         }
00056 
00057         /* handle gap events */
00058         _ble.gap().setEventHandler(this);
00059 
00060         ble_error_t error = _ble.init(this, &GapButtonDemo::on_init_complete);
00061 
00062         if (error) {
00063             printf("Error returned by BLE::init.\r\n");
00064             return;
00065         }
00066 
00067         /* to show we're running we'll blink every 500ms */
00068         _event_queue.call_every(500, this, &GapButtonDemo::blink);
00069 
00070         /* this will not return until shutdown */
00071         _event_queue.dispatch_forever();
00072     }
00073 
00074 private:
00075     /** This is called when BLE interface is initialised and starts the first mode */
00076     void on_init_complete(BLE::InitializationCompleteCallbackContext *event)
00077     {
00078         if (event->error) {
00079             printf("Error during the initialisation\r\n");
00080             return;
00081         }
00082 
00083         print_mac_address();
00084 
00085         _button.rise(Callback<void()>(this, &GapButtonDemo::button_pressed_callback));
00086 
00087         start_advertising();
00088     }
00089 
00090     void start_advertising() {
00091         /* Create advertising parameters and payload */
00092 
00093         ble::AdvertisingParameters adv_parameters(
00094             ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
00095             ble::adv_interval_t(ble::millisecond_t(1000))
00096         );
00097 
00098         _adv_data_builder.setFlags();
00099         _adv_data_builder.setLocalServiceList(mbed::make_Span(&_button_uuid, 1));
00100         _adv_data_builder.setName(DEVICE_NAME);
00101 
00102         update_button_payload();
00103 
00104         /* Setup advertising */
00105 
00106         ble_error_t error = _ble.gap().setAdvertisingParameters(
00107             ble::LEGACY_ADVERTISING_HANDLE,
00108             adv_parameters
00109         );
00110 
00111         if (error) {
00112             print_error(error, "_ble.gap().setAdvertisingParameters() failed");
00113             return;
00114         }
00115 
00116         error = _ble.gap().setAdvertisingPayload(
00117             ble::LEGACY_ADVERTISING_HANDLE,
00118             _adv_data_builder.getAdvertisingData()
00119         );
00120 
00121         if (error) {
00122             print_error(error, "_ble.gap().setAdvertisingPayload() failed");
00123             return;
00124         }
00125 
00126         /* Start advertising */
00127 
00128         error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
00129 
00130         if (error) {
00131             print_error(error, "_ble.gap().startAdvertising() failed");
00132             return;
00133         }
00134     }
00135 
00136     void update_button_payload()
00137     {
00138         /* The Service Data data type consists of a service UUID with the data associated with that service. */
00139         ble_error_t error = _adv_data_builder.setServiceData(
00140             _button_uuid,
00141             mbed::make_Span(&_button_count, 1)
00142         );
00143 
00144         if (error != BLE_ERROR_NONE) {
00145             print_error(error, "Updating payload failed");
00146         }
00147     }
00148 
00149     void button_pressed_callback()
00150     {
00151         ++_button_count;
00152 
00153         /* Calling BLE api in interrupt context may cause race conditions
00154            Using mbed-events to schedule calls to BLE api for safety */
00155         _event_queue.call(Callback<void()>(this, &GapButtonDemo::update_button_payload));
00156     }
00157 
00158     /** Blink LED to show we're running */
00159     void blink(void)
00160     {
00161         _led1 = !_led1;
00162     }
00163 
00164 private:
00165     /* Event handler */
00166 
00167     void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
00168         _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
00169     }
00170 
00171 private:
00172     BLE                &_ble;
00173     events::EventQueue &_event_queue;
00174 
00175     DigitalOut  _led1;
00176     const UUID  _button_uuid;
00177 
00178     InterruptIn _button;
00179     uint8_t     _button_count;
00180 
00181     uint8_t     _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
00182     ble::AdvertisingDataBuilder _adv_data_builder;
00183 };
00184 
00185 /** Schedule processing of events from the BLE middleware in the event queue. */
00186 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
00187     event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
00188 }
00189 
00190 int main()
00191 {
00192     BLE &ble = BLE::Instance();
00193 
00194     /* this will inform us of all events so we can schedule their handling
00195      * using our event queue */
00196     ble.onEventsToProcess(schedule_ble_events);
00197 
00198     GapButtonDemo demo(ble, event_queue);
00199 
00200     demo.run();
00201 
00202     return 0;
00203 }