he LED example demonstrates the use of a read-write characteristic to control a LED through a phone app. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_LED

To help you create your own BLE services, we have created this service template. The LED example demonstrates the use of a read-write characteristic to control a LED through a phone app.

The template covers:

  • Setting up advertising and connection states.
  • Assigning UUIDs to the service and its characteristic.
  • Creating an input characteristic: read-write, boolean. This characteristic offers control of the LED.
  • Constructing a service class and adding it to the BLE stack.

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.

  • NOTE:* If you have more than a single mbed board (e.g. nrf51dk or mkit) you can run the BLE_LED and BLE_LEDBlinker at the same time. For more information please refer to the BLE_LEDBlinker demo.

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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/start_scan.png

figure 1 How to start scan using nRF Master Control Panel 4.0.5

  • Find your device; it should be named `LED`.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/scan_results.png

figure 2 Scan results using nRF Master Control Panel 4.0.5

  • Establish a connection with your device.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/connection.png

figure 3 How to establish a connection using Master Control Panel 4.0.5

  • Discover the services and the characteristics on the device. The *LED service* has the UUID `0xA000` and includes the *LED state characteristic* which has the UUID `0xA001`. Depending on your scanner, non standard 16-bit UUID's can be displayed as 128-bit UUID's. If it is the case the following format will be used: `0000XXXX-0000-1000-8000-00805F9B34FB` where `XXXX` is the hexadecimal representation of the 16-bit UUID value.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/discovery.png

figure 4 Representation of the Led service using Master Control Panel 4.0.5

  • Open the write pannel of the *LED state* characteristic.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/write_characteristic.png

figure 5 How to read and write a characteristic value using Master Control Panel 4.0.5

  • The characteristic accept a 1 byte value:

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/write_pannel.png

figure 6 Write characteristic pannel using Master Control Panel 4.0.5

`0x00`: LED ON

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/LED_ON.png

figure 6 Write characteristic pannel to set the LED on using Master Control Panel 4.0.5

`0x01`: LED OFF

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-LED/raw-file/c6a8f2b3efb6/img/LED_OFF.png

figure 6 Write characteristic pannel to set the LED off using Master Control Panel 4.0.5

  • Toggle the LED characteristic value and see the LED turn ON or turn OFF according to the value you set.

If you can see the characteristic, and the LED is turned on/off as you toggle its value, the application is working properly.

Committer:
mbed_official
Date:
Thu Aug 15 17:01:56 2019 +0100
Revision:
79:4c62b7c6081a
Parent:
74:51fde11a771f
Merge pull request #252 from donatieng/mbed_os_update

Update Master branch to use Mbed OS 5.13.1
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:864ddfb70a9c 1 /* mbed Microcontroller Library
mbed_official 2:864ddfb70a9c 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:864ddfb70a9c 3 *
mbed_official 2:864ddfb70a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:864ddfb70a9c 5 * you may not use this file except in compliance with the License.
mbed_official 2:864ddfb70a9c 6 * You may obtain a copy of the License at
mbed_official 2:864ddfb70a9c 7 *
mbed_official 2:864ddfb70a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:864ddfb70a9c 9 *
mbed_official 2:864ddfb70a9c 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:864ddfb70a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:864ddfb70a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:864ddfb70a9c 13 * See the License for the specific language governing permissions and
mbed_official 2:864ddfb70a9c 14 * limitations under the License.
mbed_official 2:864ddfb70a9c 15 */
mbed_official 2:864ddfb70a9c 16
mbed_official 11:7404978b24e7 17 #include <events/mbed_events.h>
mbed_official 2:864ddfb70a9c 18 #include <mbed.h>
mbed_official 2:864ddfb70a9c 19 #include "ble/BLE.h"
mbed_official 2:864ddfb70a9c 20 #include "LEDService.h"
mbed_official 74:51fde11a771f 21 #include "pretty_printer.h"
mbed_official 2:864ddfb70a9c 22
mbed_official 74:51fde11a771f 23 const static char DEVICE_NAME[] = "LED";
mbed_official 2:864ddfb70a9c 24
mbed_official 74:51fde11a771f 25 static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
mbed_official 2:864ddfb70a9c 26
mbed_official 74:51fde11a771f 27 class LEDDemo : ble::Gap::EventHandler {
mbed_official 74:51fde11a771f 28 public:
mbed_official 74:51fde11a771f 29 LEDDemo(BLE &ble, events::EventQueue &event_queue) :
mbed_official 74:51fde11a771f 30 _ble(ble),
mbed_official 74:51fde11a771f 31 _event_queue(event_queue),
mbed_official 74:51fde11a771f 32 _alive_led(LED1, 1),
mbed_official 74:51fde11a771f 33 _actuated_led(LED2, 0),
mbed_official 74:51fde11a771f 34 _led_uuid(LEDService::LED_SERVICE_UUID),
mbed_official 74:51fde11a771f 35 _led_service(NULL),
mbed_official 74:51fde11a771f 36 _adv_data_builder(_adv_buffer) { }
mbed_official 2:864ddfb70a9c 37
mbed_official 74:51fde11a771f 38 ~LEDDemo() {
mbed_official 74:51fde11a771f 39 delete _led_service;
mbed_official 2:864ddfb70a9c 40 }
mbed_official 2:864ddfb70a9c 41
mbed_official 74:51fde11a771f 42 void start() {
mbed_official 74:51fde11a771f 43 _ble.gap().setEventHandler(this);
mbed_official 74:51fde11a771f 44
mbed_official 74:51fde11a771f 45 _ble.init(this, &LEDDemo::on_init_complete);
mbed_official 74:51fde11a771f 46
mbed_official 74:51fde11a771f 47 _event_queue.call_every(500, this, &LEDDemo::blink);
mbed_official 74:51fde11a771f 48
mbed_official 74:51fde11a771f 49 _event_queue.dispatch_forever();
mbed_official 74:51fde11a771f 50 }
mbed_official 2:864ddfb70a9c 51
mbed_official 74:51fde11a771f 52 private:
mbed_official 74:51fde11a771f 53 /** Callback triggered when the ble initialization process has finished */
mbed_official 74:51fde11a771f 54 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
mbed_official 74:51fde11a771f 55 if (params->error != BLE_ERROR_NONE) {
mbed_official 74:51fde11a771f 56 printf("Ble initialization failed.");
mbed_official 74:51fde11a771f 57 return;
mbed_official 74:51fde11a771f 58 }
mbed_official 44:df8adb3bc797 59
mbed_official 74:51fde11a771f 60 _led_service = new LEDService(_ble, false);
mbed_official 74:51fde11a771f 61
mbed_official 74:51fde11a771f 62 _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written);
mbed_official 2:864ddfb70a9c 63
mbed_official 74:51fde11a771f 64 print_mac_address();
mbed_official 74:51fde11a771f 65
mbed_official 74:51fde11a771f 66 start_advertising();
mbed_official 2:864ddfb70a9c 67 }
mbed_official 2:864ddfb70a9c 68
mbed_official 74:51fde11a771f 69 void start_advertising() {
mbed_official 74:51fde11a771f 70 /* Create advertising parameters and payload */
mbed_official 74:51fde11a771f 71
mbed_official 74:51fde11a771f 72 ble::AdvertisingParameters adv_parameters(
mbed_official 74:51fde11a771f 73 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
mbed_official 74:51fde11a771f 74 ble::adv_interval_t(ble::millisecond_t(1000))
mbed_official 74:51fde11a771f 75 );
mbed_official 74:51fde11a771f 76
mbed_official 74:51fde11a771f 77 _adv_data_builder.setFlags();
mbed_official 74:51fde11a771f 78 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_led_uuid, 1));
mbed_official 74:51fde11a771f 79 _adv_data_builder.setName(DEVICE_NAME);
mbed_official 74:51fde11a771f 80
mbed_official 74:51fde11a771f 81 /* Setup advertising */
mbed_official 74:51fde11a771f 82
mbed_official 74:51fde11a771f 83 ble_error_t error = _ble.gap().setAdvertisingParameters(
mbed_official 74:51fde11a771f 84 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 85 adv_parameters
mbed_official 74:51fde11a771f 86 );
mbed_official 74:51fde11a771f 87
mbed_official 74:51fde11a771f 88 if (error) {
mbed_official 74:51fde11a771f 89 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
mbed_official 74:51fde11a771f 90 return;
mbed_official 74:51fde11a771f 91 }
mbed_official 74:51fde11a771f 92
mbed_official 74:51fde11a771f 93 error = _ble.gap().setAdvertisingPayload(
mbed_official 74:51fde11a771f 94 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 95 _adv_data_builder.getAdvertisingData()
mbed_official 74:51fde11a771f 96 );
mbed_official 74:51fde11a771f 97
mbed_official 74:51fde11a771f 98 if (error) {
mbed_official 74:51fde11a771f 99 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
mbed_official 74:51fde11a771f 100 return;
mbed_official 74:51fde11a771f 101 }
mbed_official 74:51fde11a771f 102
mbed_official 74:51fde11a771f 103 /* Start advertising */
mbed_official 74:51fde11a771f 104
mbed_official 74:51fde11a771f 105 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 106
mbed_official 74:51fde11a771f 107 if (error) {
mbed_official 74:51fde11a771f 108 printf("_ble.gap().startAdvertising() failed\r\n");
mbed_official 74:51fde11a771f 109 return;
mbed_official 74:51fde11a771f 110 }
mbed_official 2:864ddfb70a9c 111 }
mbed_official 2:864ddfb70a9c 112
mbed_official 74:51fde11a771f 113 /**
mbed_official 74:51fde11a771f 114 * This callback allows the LEDService to receive updates to the ledState Characteristic.
mbed_official 74:51fde11a771f 115 *
mbed_official 74:51fde11a771f 116 * @param[in] params Information about the characterisitc being updated.
mbed_official 74:51fde11a771f 117 */
mbed_official 74:51fde11a771f 118 void on_data_written(const GattWriteCallbackParams *params) {
mbed_official 74:51fde11a771f 119 if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) {
mbed_official 74:51fde11a771f 120 _actuated_led = *(params->data);
mbed_official 74:51fde11a771f 121 }
mbed_official 74:51fde11a771f 122 }
mbed_official 2:864ddfb70a9c 123
mbed_official 74:51fde11a771f 124 void blink() {
mbed_official 74:51fde11a771f 125 _alive_led = !_alive_led;
mbed_official 74:51fde11a771f 126 }
mbed_official 74:51fde11a771f 127
mbed_official 74:51fde11a771f 128 private:
mbed_official 74:51fde11a771f 129 /* Event handler */
mbed_official 2:864ddfb70a9c 130
mbed_official 74:51fde11a771f 131 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
mbed_official 74:51fde11a771f 132 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 133 }
mbed_official 74:51fde11a771f 134
mbed_official 74:51fde11a771f 135 private:
mbed_official 74:51fde11a771f 136 BLE &_ble;
mbed_official 74:51fde11a771f 137 events::EventQueue &_event_queue;
mbed_official 74:51fde11a771f 138 DigitalOut _alive_led;
mbed_official 74:51fde11a771f 139 DigitalOut _actuated_led;
mbed_official 44:df8adb3bc797 140
mbed_official 74:51fde11a771f 141 UUID _led_uuid;
mbed_official 74:51fde11a771f 142 LEDService *_led_service;
mbed_official 2:864ddfb70a9c 143
mbed_official 74:51fde11a771f 144 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
mbed_official 74:51fde11a771f 145 ble::AdvertisingDataBuilder _adv_data_builder;
mbed_official 74:51fde11a771f 146 };
mbed_official 74:51fde11a771f 147
mbed_official 74:51fde11a771f 148 /** Schedule processing of events from the BLE middleware in the event queue. */
mbed_official 74:51fde11a771f 149 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
mbed_official 74:51fde11a771f 150 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
mbed_official 2:864ddfb70a9c 151 }
mbed_official 2:864ddfb70a9c 152
mbed_official 2:864ddfb70a9c 153 int main()
mbed_official 2:864ddfb70a9c 154 {
mbed_official 74:51fde11a771f 155 BLE &ble = BLE::Instance();
mbed_official 74:51fde11a771f 156 ble.onEventsToProcess(schedule_ble_events);
mbed_official 2:864ddfb70a9c 157
mbed_official 74:51fde11a771f 158 LEDDemo demo(ble, event_queue);
mbed_official 74:51fde11a771f 159 demo.start();
mbed_official 2:864ddfb70a9c 160
mbed_official 2:864ddfb70a9c 161 return 0;
mbed_official 2:864ddfb70a9c 162 }
mbed_official 74:51fde11a771f 163