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
Button count over GAP
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 value is a count of how many times a button on the device was pressed (the code actually monitors the button's releases, not press downs).
- We transmit the value in the SERVICE_DATA field of the advertising payload.
Running the application
Requirements
The sample application can be seen on any BLE scanner on a smartphone. If you don't have a scanner on your phone, please install :
- nRF Master Control Panel for Android.
- LightBlue for iPhone.
Hardware requirements are in the main readme.
Building instructions
Building with mbed CLI
If you'd like to use mbed CLI to build this, then you should refer to the main readme. The instructions here relate to using the developer.mbed.org Online Compiler
In order to build this example in the mbed Online Compiler, first import the example using the ‘Import’ button on the right hand side.
Next, select a platform to build for. This must either be a platform that supports BLE, for example the NRF51-DK, or one of the following:
List of platforms supporting Bluetooth Low Energy
Or you must also add a piece of hardware and the supporting library that includes a Bluetooth Low Energy driver for that hardware, for example the K64F or NUCLEO_F401RE with the X-NUCLEO-IDB05A1
List of components supporting Bluetooth Low Energy.
Once you have selected your platform, compile the example and drag and drop the resulting binary onto your board.
For general instructions on using the mbed Online Compiler, please see the mbed Handbook
Checking for success
Note: Screens captures depicted below show what is expected from this example if the scanner used is nRF Master Control Panel version 4.0.5. If you encounter any difficulties consider trying another scanner or another version of nRF Master Control Panel. Alternative scanners may require reference to their manuals.
- Build the application and install it on your board as explained in the building instructions.
- Open the BLE scanner on your phone.
- Start a scan.
figure 1 How to start scan using nRF Master Control Panel 4.0.5.
- Find your device; it should be named `GAPButton`; and look at the advertisement broadcasted by your device (there is no need to connect to your device).
figure 2 Scan results using nRF Master Control Panel 4.0.5.
- The Service Data field of the advertisement packet broadcasted by your device reflects the button press count. The starting value is 0.
figure 3 Initial state of the button using nRF Master Control Panel 4.0.5.
- Press the button on the device.
figure 3 State after 1 button press using nRF Master Control Panel 4.0.5.
- The Service Data field value of the advertisement packet should change every time you press the button.
figure 3 State after 6 button press using nRF Master Control Panel 4.0.5.
Note
Since broadcasting is not reliable and your phone may scan intermittently, it is possible that your phone will miss button updates.
source/main.cpp@83:5f43cd2a93e6, 2019-04-26 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Apr 26 18:01:01 2019 +0100
- Revision:
- 83:5f43cd2a93e6
- Parent:
- 78:3f848aa75a8a
Merge pull request #230 from LMESTM/NUCLEO_WB55RG_readme
Update readme with some NUCLEO_WB55RG basic info
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 3:f0ed4199b362 | 1 | /* mbed Microcontroller Library |
mbed_official | 78:3f848aa75a8a | 2 | * Copyright (c) 2006-2018 ARM Limited |
mbed_official | 3:f0ed4199b362 | 3 | * |
mbed_official | 3:f0ed4199b362 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 3:f0ed4199b362 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 3:f0ed4199b362 | 6 | * You may obtain a copy of the License at |
mbed_official | 3:f0ed4199b362 | 7 | * |
mbed_official | 3:f0ed4199b362 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 3:f0ed4199b362 | 9 | * |
mbed_official | 3:f0ed4199b362 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 3:f0ed4199b362 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 3:f0ed4199b362 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 3:f0ed4199b362 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 3:f0ed4199b362 | 14 | * limitations under the License. |
mbed_official | 3:f0ed4199b362 | 15 | */ |
mbed_official | 3:f0ed4199b362 | 16 | |
mbed_official | 14:332bb3fa072b | 17 | #include <events/mbed_events.h> |
mbed_official | 3:f0ed4199b362 | 18 | #include <mbed.h> |
mbed_official | 3:f0ed4199b362 | 19 | #include "ble/BLE.h" |
mbed_official | 78:3f848aa75a8a | 20 | #include "pretty_printer.h" |
mbed_official | 3:f0ed4199b362 | 21 | |
mbed_official | 78:3f848aa75a8a | 22 | static const char DEVICE_NAME[] = "GAPButton"; |
mbed_official | 3:f0ed4199b362 | 23 | |
mbed_official | 78:3f848aa75a8a | 24 | static EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
mbed_official | 3:f0ed4199b362 | 25 | |
mbed_official | 78:3f848aa75a8a | 26 | /** Demonstrate advertising, scanning and connecting |
mbed_official | 78:3f848aa75a8a | 27 | */ |
mbed_official | 78:3f848aa75a8a | 28 | class GapButtonDemo : private mbed::NonCopyable<GapButtonDemo>, public ble::Gap::EventHandler |
mbed_official | 3:f0ed4199b362 | 29 | { |
mbed_official | 78:3f848aa75a8a | 30 | public: |
mbed_official | 78:3f848aa75a8a | 31 | GapButtonDemo(BLE& ble, events::EventQueue& event_queue) : |
mbed_official | 78:3f848aa75a8a | 32 | _ble(ble), |
mbed_official | 78:3f848aa75a8a | 33 | _event_queue(event_queue), |
mbed_official | 78:3f848aa75a8a | 34 | _led1(LED1, 0), |
mbed_official | 78:3f848aa75a8a | 35 | /* We can arbiturarily choose the GAPButton service UUID to be 0xAA00 |
mbed_official | 78:3f848aa75a8a | 36 | * as long as it does not overlap with the UUIDs defined here: |
mbed_official | 78:3f848aa75a8a | 37 | * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx */ |
mbed_official | 78:3f848aa75a8a | 38 | _button_uuid(0xAA00), |
mbed_official | 83:5f43cd2a93e6 | 39 | _button(BLE_BUTTON_PIN_NAME, BLE_BUTTON_PIN_PULL), |
mbed_official | 78:3f848aa75a8a | 40 | _adv_data_builder(_adv_buffer) { } |
mbed_official | 78:3f848aa75a8a | 41 | |
mbed_official | 78:3f848aa75a8a | 42 | ~GapButtonDemo() |
mbed_official | 78:3f848aa75a8a | 43 | { |
mbed_official | 78:3f848aa75a8a | 44 | if (_ble.hasInitialized()) { |
mbed_official | 78:3f848aa75a8a | 45 | _ble.shutdown(); |
mbed_official | 78:3f848aa75a8a | 46 | } |
mbed_official | 3:f0ed4199b362 | 47 | } |
mbed_official | 78:3f848aa75a8a | 48 | |
mbed_official | 78:3f848aa75a8a | 49 | /** Start BLE interface initialisation */ |
mbed_official | 78:3f848aa75a8a | 50 | void run() |
mbed_official | 78:3f848aa75a8a | 51 | { |
mbed_official | 78:3f848aa75a8a | 52 | if (_ble.hasInitialized()) { |
mbed_official | 78:3f848aa75a8a | 53 | printf("Ble instance already initialised.\r\n"); |
mbed_official | 78:3f848aa75a8a | 54 | return; |
mbed_official | 78:3f848aa75a8a | 55 | } |
mbed_official | 3:f0ed4199b362 | 56 | |
mbed_official | 78:3f848aa75a8a | 57 | /* handle gap events */ |
mbed_official | 78:3f848aa75a8a | 58 | _ble.gap().setEventHandler(this); |
mbed_official | 78:3f848aa75a8a | 59 | |
mbed_official | 78:3f848aa75a8a | 60 | ble_error_t error = _ble.init(this, &GapButtonDemo::on_init_complete); |
mbed_official | 3:f0ed4199b362 | 61 | |
mbed_official | 78:3f848aa75a8a | 62 | if (error) { |
mbed_official | 78:3f848aa75a8a | 63 | printf("Error returned by BLE::init.\r\n"); |
mbed_official | 78:3f848aa75a8a | 64 | return; |
mbed_official | 78:3f848aa75a8a | 65 | } |
mbed_official | 3:f0ed4199b362 | 66 | |
mbed_official | 78:3f848aa75a8a | 67 | /* to show we're running we'll blink every 500ms */ |
mbed_official | 78:3f848aa75a8a | 68 | _event_queue.call_every(500, this, &GapButtonDemo::blink); |
mbed_official | 3:f0ed4199b362 | 69 | |
mbed_official | 78:3f848aa75a8a | 70 | /* this will not return until shutdown */ |
mbed_official | 78:3f848aa75a8a | 71 | _event_queue.dispatch_forever(); |
mbed_official | 78:3f848aa75a8a | 72 | } |
mbed_official | 3:f0ed4199b362 | 73 | |
mbed_official | 78:3f848aa75a8a | 74 | private: |
mbed_official | 78:3f848aa75a8a | 75 | /** This is called when BLE interface is initialised and starts the first mode */ |
mbed_official | 78:3f848aa75a8a | 76 | void on_init_complete(BLE::InitializationCompleteCallbackContext *event) |
mbed_official | 78:3f848aa75a8a | 77 | { |
mbed_official | 78:3f848aa75a8a | 78 | if (event->error) { |
mbed_official | 78:3f848aa75a8a | 79 | printf("Error during the initialisation\r\n"); |
mbed_official | 78:3f848aa75a8a | 80 | return; |
mbed_official | 78:3f848aa75a8a | 81 | } |
mbed_official | 47:70f571463f31 | 82 | |
mbed_official | 78:3f848aa75a8a | 83 | print_mac_address(); |
mbed_official | 3:f0ed4199b362 | 84 | |
mbed_official | 78:3f848aa75a8a | 85 | _button.rise(Callback<void()>(this, &GapButtonDemo::button_pressed_callback)); |
mbed_official | 78:3f848aa75a8a | 86 | |
mbed_official | 78:3f848aa75a8a | 87 | start_advertising(); |
mbed_official | 3:f0ed4199b362 | 88 | } |
mbed_official | 3:f0ed4199b362 | 89 | |
mbed_official | 78:3f848aa75a8a | 90 | void start_advertising() { |
mbed_official | 78:3f848aa75a8a | 91 | /* Create advertising parameters and payload */ |
mbed_official | 78:3f848aa75a8a | 92 | |
mbed_official | 78:3f848aa75a8a | 93 | ble::AdvertisingParameters adv_parameters( |
mbed_official | 78:3f848aa75a8a | 94 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED, |
mbed_official | 78:3f848aa75a8a | 95 | ble::adv_interval_t(ble::millisecond_t(1000)) |
mbed_official | 78:3f848aa75a8a | 96 | ); |
mbed_official | 78:3f848aa75a8a | 97 | |
mbed_official | 78:3f848aa75a8a | 98 | _adv_data_builder.setFlags(); |
mbed_official | 78:3f848aa75a8a | 99 | _adv_data_builder.setLocalServiceList(mbed::make_Span(&_button_uuid, 1)); |
mbed_official | 78:3f848aa75a8a | 100 | _adv_data_builder.setName(DEVICE_NAME); |
mbed_official | 78:3f848aa75a8a | 101 | |
mbed_official | 78:3f848aa75a8a | 102 | update_button_payload(); |
mbed_official | 78:3f848aa75a8a | 103 | |
mbed_official | 78:3f848aa75a8a | 104 | /* Setup advertising */ |
mbed_official | 78:3f848aa75a8a | 105 | |
mbed_official | 78:3f848aa75a8a | 106 | ble_error_t error = _ble.gap().setAdvertisingParameters( |
mbed_official | 78:3f848aa75a8a | 107 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 78:3f848aa75a8a | 108 | adv_parameters |
mbed_official | 78:3f848aa75a8a | 109 | ); |
mbed_official | 3:f0ed4199b362 | 110 | |
mbed_official | 78:3f848aa75a8a | 111 | if (error) { |
mbed_official | 78:3f848aa75a8a | 112 | print_error(error, "_ble.gap().setAdvertisingParameters() failed"); |
mbed_official | 78:3f848aa75a8a | 113 | return; |
mbed_official | 78:3f848aa75a8a | 114 | } |
mbed_official | 78:3f848aa75a8a | 115 | |
mbed_official | 78:3f848aa75a8a | 116 | error = _ble.gap().setAdvertisingPayload( |
mbed_official | 78:3f848aa75a8a | 117 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 78:3f848aa75a8a | 118 | _adv_data_builder.getAdvertisingData() |
mbed_official | 78:3f848aa75a8a | 119 | ); |
mbed_official | 3:f0ed4199b362 | 120 | |
mbed_official | 78:3f848aa75a8a | 121 | if (error) { |
mbed_official | 78:3f848aa75a8a | 122 | print_error(error, "_ble.gap().setAdvertisingPayload() failed"); |
mbed_official | 78:3f848aa75a8a | 123 | return; |
mbed_official | 78:3f848aa75a8a | 124 | } |
mbed_official | 78:3f848aa75a8a | 125 | |
mbed_official | 78:3f848aa75a8a | 126 | /* Start advertising */ |
mbed_official | 78:3f848aa75a8a | 127 | |
mbed_official | 78:3f848aa75a8a | 128 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 78:3f848aa75a8a | 129 | |
mbed_official | 78:3f848aa75a8a | 130 | if (error) { |
mbed_official | 78:3f848aa75a8a | 131 | print_error(error, "_ble.gap().startAdvertising() failed"); |
mbed_official | 78:3f848aa75a8a | 132 | return; |
mbed_official | 78:3f848aa75a8a | 133 | } |
mbed_official | 3:f0ed4199b362 | 134 | } |
mbed_official | 3:f0ed4199b362 | 135 | |
mbed_official | 78:3f848aa75a8a | 136 | void update_button_payload() |
mbed_official | 78:3f848aa75a8a | 137 | { |
mbed_official | 78:3f848aa75a8a | 138 | /* The Service Data data type consists of a service UUID with the data associated with that service. */ |
mbed_official | 78:3f848aa75a8a | 139 | ble_error_t error = _adv_data_builder.setServiceData( |
mbed_official | 78:3f848aa75a8a | 140 | _button_uuid, |
mbed_official | 78:3f848aa75a8a | 141 | mbed::make_Span(&_button_count, 1) |
mbed_official | 78:3f848aa75a8a | 142 | ); |
mbed_official | 78:3f848aa75a8a | 143 | |
mbed_official | 78:3f848aa75a8a | 144 | if (error != BLE_ERROR_NONE) { |
mbed_official | 78:3f848aa75a8a | 145 | print_error(error, "Updating payload failed"); |
mbed_official | 78:3f848aa75a8a | 146 | } |
mbed_official | 78:3f848aa75a8a | 147 | } |
mbed_official | 78:3f848aa75a8a | 148 | |
mbed_official | 78:3f848aa75a8a | 149 | void button_pressed_callback() |
mbed_official | 78:3f848aa75a8a | 150 | { |
mbed_official | 78:3f848aa75a8a | 151 | ++_button_count; |
mbed_official | 78:3f848aa75a8a | 152 | |
mbed_official | 78:3f848aa75a8a | 153 | /* Calling BLE api in interrupt context may cause race conditions |
mbed_official | 78:3f848aa75a8a | 154 | Using mbed-events to schedule calls to BLE api for safety */ |
mbed_official | 78:3f848aa75a8a | 155 | _event_queue.call(Callback<void()>(this, &GapButtonDemo::update_button_payload)); |
mbed_official | 3:f0ed4199b362 | 156 | } |
mbed_official | 3:f0ed4199b362 | 157 | |
mbed_official | 78:3f848aa75a8a | 158 | /** Blink LED to show we're running */ |
mbed_official | 78:3f848aa75a8a | 159 | void blink(void) |
mbed_official | 78:3f848aa75a8a | 160 | { |
mbed_official | 78:3f848aa75a8a | 161 | _led1 = !_led1; |
mbed_official | 78:3f848aa75a8a | 162 | } |
mbed_official | 3:f0ed4199b362 | 163 | |
mbed_official | 78:3f848aa75a8a | 164 | private: |
mbed_official | 78:3f848aa75a8a | 165 | /* Event handler */ |
mbed_official | 3:f0ed4199b362 | 166 | |
mbed_official | 78:3f848aa75a8a | 167 | void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { |
mbed_official | 78:3f848aa75a8a | 168 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 3:f0ed4199b362 | 169 | } |
mbed_official | 47:70f571463f31 | 170 | |
mbed_official | 78:3f848aa75a8a | 171 | private: |
mbed_official | 78:3f848aa75a8a | 172 | BLE &_ble; |
mbed_official | 78:3f848aa75a8a | 173 | events::EventQueue &_event_queue; |
mbed_official | 78:3f848aa75a8a | 174 | |
mbed_official | 78:3f848aa75a8a | 175 | DigitalOut _led1; |
mbed_official | 78:3f848aa75a8a | 176 | const UUID _button_uuid; |
mbed_official | 3:f0ed4199b362 | 177 | |
mbed_official | 78:3f848aa75a8a | 178 | InterruptIn _button; |
mbed_official | 78:3f848aa75a8a | 179 | uint8_t _button_count; |
mbed_official | 78:3f848aa75a8a | 180 | |
mbed_official | 78:3f848aa75a8a | 181 | uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
mbed_official | 78:3f848aa75a8a | 182 | ble::AdvertisingDataBuilder _adv_data_builder; |
mbed_official | 78:3f848aa75a8a | 183 | }; |
mbed_official | 78:3f848aa75a8a | 184 | |
mbed_official | 78:3f848aa75a8a | 185 | /** Schedule processing of events from the BLE middleware in the event queue. */ |
mbed_official | 78:3f848aa75a8a | 186 | void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { |
mbed_official | 78:3f848aa75a8a | 187 | event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); |
mbed_official | 3:f0ed4199b362 | 188 | } |
mbed_official | 3:f0ed4199b362 | 189 | |
mbed_official | 3:f0ed4199b362 | 190 | int main() |
mbed_official | 3:f0ed4199b362 | 191 | { |
mbed_official | 78:3f848aa75a8a | 192 | BLE &ble = BLE::Instance(); |
mbed_official | 3:f0ed4199b362 | 193 | |
mbed_official | 78:3f848aa75a8a | 194 | /* this will inform us of all events so we can schedule their handling |
mbed_official | 78:3f848aa75a8a | 195 | * using our event queue */ |
mbed_official | 78:3f848aa75a8a | 196 | ble.onEventsToProcess(schedule_ble_events); |
mbed_official | 3:f0ed4199b362 | 197 | |
mbed_official | 78:3f848aa75a8a | 198 | GapButtonDemo demo(ble, event_queue); |
mbed_official | 3:f0ed4199b362 | 199 | |
mbed_official | 78:3f848aa75a8a | 200 | demo.run(); |
mbed_official | 3:f0ed4199b362 | 201 | |
mbed_official | 3:f0ed4199b362 | 202 | return 0; |
mbed_official | 3:f0ed4199b362 | 203 | } |