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@2:e532bbe3fd8e, 2016-07-28 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Thu Jul 28 23:20:45 2016 +0100
- Revision:
- 2:e532bbe3fd8e
- Parent:
- 1:7f4e41bacb3c
- Child:
- 3:f0ed4199b362
Sync with mbed-os-5.1.0-rc3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
2:e532bbe3fd8e | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
2:e532bbe3fd8e | 2 | * Copyright (c) 2006-2013 ARM Limited |
Vincent Coubard |
2:e532bbe3fd8e | 3 | * |
Vincent Coubard |
2:e532bbe3fd8e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
2:e532bbe3fd8e | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
2:e532bbe3fd8e | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
2:e532bbe3fd8e | 7 | * |
Vincent Coubard |
2:e532bbe3fd8e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
2:e532bbe3fd8e | 9 | * |
Vincent Coubard |
2:e532bbe3fd8e | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
2:e532bbe3fd8e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
2:e532bbe3fd8e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
2:e532bbe3fd8e | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
2:e532bbe3fd8e | 14 | * limitations under the License. |
Vincent Coubard |
2:e532bbe3fd8e | 15 | */ |
Vincent Coubard |
2:e532bbe3fd8e | 16 | |
Vincent Coubard |
2:e532bbe3fd8e | 17 | #include <mbed-events/events.h> |
Vincent Coubard |
2:e532bbe3fd8e | 18 | #include <mbed.h> |
Vincent Coubard |
2:e532bbe3fd8e | 19 | #include "ble/BLE.h" |
Vincent Coubard |
2:e532bbe3fd8e | 20 | |
Vincent Coubard |
2:e532bbe3fd8e | 21 | DigitalOut led1(LED1, 1); |
Vincent Coubard |
2:e532bbe3fd8e | 22 | InterruptIn button(BUTTON1); |
Vincent Coubard |
2:e532bbe3fd8e | 23 | uint8_t cnt; |
Vincent Coubard |
2:e532bbe3fd8e | 24 | |
Vincent Coubard |
2:e532bbe3fd8e | 25 | // Change your device name below |
Vincent Coubard |
2:e532bbe3fd8e | 26 | const char DEVICE_NAME[] = "GAPButton"; |
Vincent Coubard |
2:e532bbe3fd8e | 27 | |
Vincent Coubard |
2:e532bbe3fd8e | 28 | /* We can arbiturarily choose the GAPButton service UUID to be 0xAA00 |
Vincent Coubard |
2:e532bbe3fd8e | 29 | * as long as it does not overlap with the UUIDs defined here: |
Vincent Coubard |
2:e532bbe3fd8e | 30 | * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx */ |
Vincent Coubard |
2:e532bbe3fd8e | 31 | #define GAPButtonUUID 0xAA00 |
Vincent Coubard |
2:e532bbe3fd8e | 32 | const uint16_t uuid16_list[] = {GAPButtonUUID}; |
Vincent Coubard |
2:e532bbe3fd8e | 33 | |
Vincent Coubard |
2:e532bbe3fd8e | 34 | static EventQueue eventQueue( |
Vincent Coubard |
2:e532bbe3fd8e | 35 | /* event count */ 16 * /* event size */ 32 |
Vincent Coubard |
2:e532bbe3fd8e | 36 | ); |
Vincent Coubard |
2:e532bbe3fd8e | 37 | |
Vincent Coubard |
2:e532bbe3fd8e | 38 | void print_error(ble_error_t error, const char* msg) |
Vincent Coubard |
2:e532bbe3fd8e | 39 | { |
Vincent Coubard |
2:e532bbe3fd8e | 40 | printf("%s: ", msg); |
Vincent Coubard |
2:e532bbe3fd8e | 41 | switch(error) { |
Vincent Coubard |
2:e532bbe3fd8e | 42 | case BLE_ERROR_NONE: |
Vincent Coubard |
2:e532bbe3fd8e | 43 | printf("BLE_ERROR_NONE: No error"); |
Vincent Coubard |
2:e532bbe3fd8e | 44 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 45 | case BLE_ERROR_BUFFER_OVERFLOW: |
Vincent Coubard |
2:e532bbe3fd8e | 46 | printf("BLE_ERROR_BUFFER_OVERFLOW: The requested action would cause a buffer overflow and has been aborted"); |
Vincent Coubard |
2:e532bbe3fd8e | 47 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 48 | case BLE_ERROR_NOT_IMPLEMENTED: |
Vincent Coubard |
2:e532bbe3fd8e | 49 | printf("BLE_ERROR_NOT_IMPLEMENTED: Requested a feature that isn't yet implement or isn't supported by the target HW"); |
Vincent Coubard |
2:e532bbe3fd8e | 50 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 51 | case BLE_ERROR_PARAM_OUT_OF_RANGE: |
Vincent Coubard |
2:e532bbe3fd8e | 52 | printf("BLE_ERROR_PARAM_OUT_OF_RANGE: One of the supplied parameters is outside the valid range"); |
Vincent Coubard |
2:e532bbe3fd8e | 53 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 54 | case BLE_ERROR_INVALID_PARAM: |
Vincent Coubard |
2:e532bbe3fd8e | 55 | printf("BLE_ERROR_INVALID_PARAM: One of the supplied parameters is invalid"); |
Vincent Coubard |
2:e532bbe3fd8e | 56 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 57 | case BLE_STACK_BUSY: |
Vincent Coubard |
2:e532bbe3fd8e | 58 | printf("BLE_STACK_BUSY: The stack is busy"); |
Vincent Coubard |
2:e532bbe3fd8e | 59 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 60 | case BLE_ERROR_INVALID_STATE: |
Vincent Coubard |
2:e532bbe3fd8e | 61 | printf("BLE_ERROR_INVALID_STATE: Invalid state"); |
Vincent Coubard |
2:e532bbe3fd8e | 62 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 63 | case BLE_ERROR_NO_MEM: |
Vincent Coubard |
2:e532bbe3fd8e | 64 | printf("BLE_ERROR_NO_MEM: Out of Memory"); |
Vincent Coubard |
2:e532bbe3fd8e | 65 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 66 | case BLE_ERROR_OPERATION_NOT_PERMITTED: |
Vincent Coubard |
2:e532bbe3fd8e | 67 | printf("BLE_ERROR_OPERATION_NOT_PERMITTED"); |
Vincent Coubard |
2:e532bbe3fd8e | 68 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 69 | case BLE_ERROR_INITIALIZATION_INCOMPLETE: |
Vincent Coubard |
2:e532bbe3fd8e | 70 | printf("BLE_ERROR_INITIALIZATION_INCOMPLETE"); |
Vincent Coubard |
2:e532bbe3fd8e | 71 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 72 | case BLE_ERROR_ALREADY_INITIALIZED: |
Vincent Coubard |
2:e532bbe3fd8e | 73 | printf("BLE_ERROR_ALREADY_INITIALIZED"); |
Vincent Coubard |
2:e532bbe3fd8e | 74 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 75 | case BLE_ERROR_UNSPECIFIED: |
Vincent Coubard |
2:e532bbe3fd8e | 76 | printf("BLE_ERROR_UNSPECIFIED: Unknown error"); |
Vincent Coubard |
2:e532bbe3fd8e | 77 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 78 | case BLE_ERROR_INTERNAL_STACK_FAILURE: |
Vincent Coubard |
2:e532bbe3fd8e | 79 | printf("BLE_ERROR_INTERNAL_STACK_FAILURE: internal stack faillure"); |
Vincent Coubard |
2:e532bbe3fd8e | 80 | break; |
Vincent Coubard |
2:e532bbe3fd8e | 81 | } |
Vincent Coubard |
2:e532bbe3fd8e | 82 | printf("\r\n"); |
Vincent Coubard |
2:e532bbe3fd8e | 83 | } |
Vincent Coubard |
2:e532bbe3fd8e | 84 | |
Vincent Coubard |
2:e532bbe3fd8e | 85 | void updatePayload(void) |
Vincent Coubard |
2:e532bbe3fd8e | 86 | { |
Vincent Coubard |
2:e532bbe3fd8e | 87 | // Update the count in the SERVICE_DATA field of the advertising payload |
Vincent Coubard |
2:e532bbe3fd8e | 88 | uint8_t service_data[3]; |
Vincent Coubard |
2:e532bbe3fd8e | 89 | service_data[0] = GAPButtonUUID & 0xff; |
Vincent Coubard |
2:e532bbe3fd8e | 90 | service_data[1] = GAPButtonUUID >> 8; |
Vincent Coubard |
2:e532bbe3fd8e | 91 | service_data[2] = cnt; // Put the button click count in the third byte |
Vincent Coubard |
2:e532bbe3fd8e | 92 | ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
Vincent Coubard |
2:e532bbe3fd8e | 93 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 94 | print_error(err, "Updating payload failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 95 | } |
Vincent Coubard |
2:e532bbe3fd8e | 96 | } |
Vincent Coubard |
2:e532bbe3fd8e | 97 | |
Vincent Coubard |
2:e532bbe3fd8e | 98 | void buttonPressedCallback(void) |
Vincent Coubard |
2:e532bbe3fd8e | 99 | { |
Vincent Coubard |
2:e532bbe3fd8e | 100 | ++cnt; |
Vincent Coubard |
2:e532bbe3fd8e | 101 | |
Vincent Coubard |
2:e532bbe3fd8e | 102 | // Calling BLE api in interrupt context may cause race conditions |
Vincent Coubard |
2:e532bbe3fd8e | 103 | // Using mbed-events to schedule calls to BLE api for safety |
Vincent Coubard |
2:e532bbe3fd8e | 104 | eventQueue.post(updatePayload); |
Vincent Coubard |
2:e532bbe3fd8e | 105 | } |
Vincent Coubard |
2:e532bbe3fd8e | 106 | |
Vincent Coubard |
2:e532bbe3fd8e | 107 | void blinkCallback(void) |
Vincent Coubard |
2:e532bbe3fd8e | 108 | { |
Vincent Coubard |
2:e532bbe3fd8e | 109 | led1 = !led1; |
Vincent Coubard |
2:e532bbe3fd8e | 110 | } |
Vincent Coubard |
2:e532bbe3fd8e | 111 | |
Vincent Coubard |
2:e532bbe3fd8e | 112 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) |
Vincent Coubard |
2:e532bbe3fd8e | 113 | { |
Vincent Coubard |
2:e532bbe3fd8e | 114 | BLE& ble = context->ble; |
Vincent Coubard |
2:e532bbe3fd8e | 115 | ble_error_t err = context->error; |
Vincent Coubard |
2:e532bbe3fd8e | 116 | |
Vincent Coubard |
2:e532bbe3fd8e | 117 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 118 | print_error(err, "BLE initialisation failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 119 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 120 | } |
Vincent Coubard |
2:e532bbe3fd8e | 121 | |
Vincent Coubard |
2:e532bbe3fd8e | 122 | // Set up the advertising flags. Note: not all combination of flags are valid |
Vincent Coubard |
2:e532bbe3fd8e | 123 | // BREDR_NOT_SUPPORTED: Device does not support Basic Rate or Enchanced Data Rate, It is Low Energy only. |
Vincent Coubard |
2:e532bbe3fd8e | 124 | // LE_GENERAL_DISCOVERABLE: Peripheral device is discoverable at any moment |
Vincent Coubard |
2:e532bbe3fd8e | 125 | err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Vincent Coubard |
2:e532bbe3fd8e | 126 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 127 | print_error(err, "Setting GAP flags failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 128 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 129 | } |
Vincent Coubard |
2:e532bbe3fd8e | 130 | |
Vincent Coubard |
2:e532bbe3fd8e | 131 | // Put the device name in the advertising payload |
Vincent Coubard |
2:e532bbe3fd8e | 132 | err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Vincent Coubard |
2:e532bbe3fd8e | 133 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 134 | print_error(err, "Setting device name failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 135 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 136 | } |
Vincent Coubard |
2:e532bbe3fd8e | 137 | |
Vincent Coubard |
2:e532bbe3fd8e | 138 | err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Vincent Coubard |
2:e532bbe3fd8e | 139 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 140 | print_error(err, "Setting service UUID failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 141 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 142 | } |
Vincent Coubard |
2:e532bbe3fd8e | 143 | |
Vincent Coubard |
2:e532bbe3fd8e | 144 | // The Service Data data type consists of a service UUID with the data associated with that service. |
Vincent Coubard |
2:e532bbe3fd8e | 145 | // We will encode the number of button clicks in the Service Data field |
Vincent Coubard |
2:e532bbe3fd8e | 146 | // First two bytes of SERVICE_DATA field should contain the UUID of the service |
Vincent Coubard |
2:e532bbe3fd8e | 147 | uint8_t service_data[3]; |
Vincent Coubard |
2:e532bbe3fd8e | 148 | service_data[0] = GAPButtonUUID & 0xff; |
Vincent Coubard |
2:e532bbe3fd8e | 149 | service_data[1] = GAPButtonUUID >> 8; |
Vincent Coubard |
2:e532bbe3fd8e | 150 | service_data[2] = cnt; // Put the button click count in the third byte |
Vincent Coubard |
2:e532bbe3fd8e | 151 | err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
Vincent Coubard |
2:e532bbe3fd8e | 152 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 153 | print_error(err, "Setting service data failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 154 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 155 | } |
Vincent Coubard |
2:e532bbe3fd8e | 156 | |
Vincent Coubard |
2:e532bbe3fd8e | 157 | // It is not connectable as we are just boardcasting |
Vincent Coubard |
2:e532bbe3fd8e | 158 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
Vincent Coubard |
2:e532bbe3fd8e | 159 | |
Vincent Coubard |
2:e532bbe3fd8e | 160 | // Send out the advertising payload every 1000ms |
Vincent Coubard |
2:e532bbe3fd8e | 161 | ble.gap().setAdvertisingInterval(1000); |
Vincent Coubard |
2:e532bbe3fd8e | 162 | |
Vincent Coubard |
2:e532bbe3fd8e | 163 | err = ble.gap().startAdvertising(); |
Vincent Coubard |
2:e532bbe3fd8e | 164 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 165 | print_error(err, "Sart advertising failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 166 | return; |
Vincent Coubard |
2:e532bbe3fd8e | 167 | } |
Vincent Coubard |
2:e532bbe3fd8e | 168 | } |
Vincent Coubard |
2:e532bbe3fd8e | 169 | |
Vincent Coubard |
2:e532bbe3fd8e | 170 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Vincent Coubard |
2:e532bbe3fd8e | 171 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
2:e532bbe3fd8e | 172 | eventQueue.post(Callback<void()>(&ble, &BLE::processEvents)); |
Vincent Coubard |
2:e532bbe3fd8e | 173 | } |
Vincent Coubard |
2:e532bbe3fd8e | 174 | |
Vincent Coubard |
2:e532bbe3fd8e | 175 | int main() |
Vincent Coubard |
2:e532bbe3fd8e | 176 | { |
Vincent Coubard |
2:e532bbe3fd8e | 177 | cnt = 0; |
Vincent Coubard |
2:e532bbe3fd8e | 178 | |
Vincent Coubard |
2:e532bbe3fd8e | 179 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
2:e532bbe3fd8e | 180 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Vincent Coubard |
2:e532bbe3fd8e | 181 | ble_error_t err = ble.init(bleInitComplete); |
Vincent Coubard |
2:e532bbe3fd8e | 182 | if (err != BLE_ERROR_NONE) { |
Vincent Coubard |
2:e532bbe3fd8e | 183 | print_error(err, "BLE initialisation failed"); |
Vincent Coubard |
2:e532bbe3fd8e | 184 | return 0; |
Vincent Coubard |
2:e532bbe3fd8e | 185 | } |
Vincent Coubard |
2:e532bbe3fd8e | 186 | |
Vincent Coubard |
2:e532bbe3fd8e | 187 | // Blink LED every 500 ms to indicate system aliveness |
Vincent Coubard |
2:e532bbe3fd8e | 188 | eventQueue.post_every(500, blinkCallback); |
Vincent Coubard |
2:e532bbe3fd8e | 189 | |
Vincent Coubard |
2:e532bbe3fd8e | 190 | // Register function to be called when button is released |
Vincent Coubard |
2:e532bbe3fd8e | 191 | button.rise(buttonPressedCallback); |
Vincent Coubard |
2:e532bbe3fd8e | 192 | |
Vincent Coubard |
2:e532bbe3fd8e | 193 | while (true) { |
Vincent Coubard |
2:e532bbe3fd8e | 194 | eventQueue.dispatch(); |
Vincent Coubard |
2:e532bbe3fd8e | 195 | } |
Vincent Coubard |
2:e532bbe3fd8e | 196 | |
Vincent Coubard |
2:e532bbe3fd8e | 197 | return 0; |
Vincent Coubard |
2:e532bbe3fd8e | 198 | } |