Demo program for the SimpleBLE library - Library that exposes BLE characteristics as ordinary variables.

Dependencies:   SimpleBLE mbed nRF51822

This is an example program for SimpleBLE, a library targeted at workshops and hackathons that makes building Bluetooth Low Energy devices a lot simpler.

For more information and the API, see the SimpleBLE homepage or read the announcement blog post.

For the mbed OS 5 version (with nRF52-DK support), see SimpleBLE-Example-mbedos5.

Committer:
janjongboom
Date:
Wed May 11 13:01:37 2016 +0000
Revision:
0:ba1c49874d3c
Child:
2:12a235e7691a
SimpleBLE Example initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
janjongboom 0:ba1c49874d3c 1 /**
janjongboom 0:ba1c49874d3c 2 * This is an example program that's using the SimpleBLE library
janjongboom 0:ba1c49874d3c 3 * to expose some variables to the outside world using BLE.
janjongboom 0:ba1c49874d3c 4 * Written by Jan Jongboom (jan.jongboom@arm.com)
janjongboom 0:ba1c49874d3c 5 */
janjongboom 0:ba1c49874d3c 6
janjongboom 0:ba1c49874d3c 7 #include "mbed.h"
janjongboom 0:ba1c49874d3c 8 #include "SimpleBLE.h"
janjongboom 0:ba1c49874d3c 9
janjongboom 0:ba1c49874d3c 10 // The first thing we need to do is create a SimpleBLE instance:
janjongboom 0:ba1c49874d3c 11 // * first argument is the device name
janjongboom 0:ba1c49874d3c 12 // * second is the advertisement interval in ms. (default 1000 ms.)
janjongboom 0:ba1c49874d3c 13 SimpleBLE ble("DEVICE_NAME", 1000);
janjongboom 0:ba1c49874d3c 14
janjongboom 0:ba1c49874d3c 15 // Now we can declare some variables that we want to expose.
janjongboom 0:ba1c49874d3c 16 // After you created the variable you can use it like any other var,
janjongboom 0:ba1c49874d3c 17 // but it's value will be automatically updated over Bluetooth!
janjongboom 0:ba1c49874d3c 18
janjongboom 0:ba1c49874d3c 19 // F.e. here we declare service 0x180d (heartrate), char 0x2a37 (curr. value) as uint8_t
janjongboom 0:ba1c49874d3c 20 SimpleChar<uint8_t> heartrate = ble.readOnly<uint8_t>(0x180D, 0x2A37, true /* notify */, 100 /* default value */);
janjongboom 0:ba1c49874d3c 21
janjongboom 0:ba1c49874d3c 22 // now we can use this variable everywhere in our code like a normal uint8_t
janjongboom 0:ba1c49874d3c 23 void updateHeartrate() {
janjongboom 0:ba1c49874d3c 24 // we just loop between 100 and 180
janjongboom 0:ba1c49874d3c 25 heartrate = heartrate + 1;
janjongboom 0:ba1c49874d3c 26 if (heartrate > 180) {
janjongboom 0:ba1c49874d3c 27 heartrate = 100;
janjongboom 0:ba1c49874d3c 28 }
janjongboom 0:ba1c49874d3c 29 }
janjongboom 0:ba1c49874d3c 30
janjongboom 0:ba1c49874d3c 31 // And here we create a custom service (0x9310) and char (0x9311) with a callback
janjongboom 0:ba1c49874d3c 32 void callback(uint32_t newValue) {
janjongboom 0:ba1c49874d3c 33 // whenever someone updates this var over Bluetooth, this function will be called
janjongboom 0:ba1c49874d3c 34 printf("My value was updated to %d\n", newValue);
janjongboom 0:ba1c49874d3c 35 }
janjongboom 0:ba1c49874d3c 36 // FYI, you can also use UUIDs here instead of short services :-)
janjongboom 0:ba1c49874d3c 37 SimpleChar<uint32_t> writeMe = ble.readWrite<uint32_t>(0x9310, 0x9311, &callback);
janjongboom 0:ba1c49874d3c 38
janjongboom 0:ba1c49874d3c 39 int main(int, char**) {
janjongboom 0:ba1c49874d3c 40 // update the heart rate every second
janjongboom 0:ba1c49874d3c 41 Ticker t;
janjongboom 0:ba1c49874d3c 42 t.attach(updateHeartrate, 1.0f);
janjongboom 0:ba1c49874d3c 43
janjongboom 0:ba1c49874d3c 44 // here's how we kick off our loop
janjongboom 0:ba1c49874d3c 45 ble.start();
janjongboom 0:ba1c49874d3c 46 while (1) {
janjongboom 0:ba1c49874d3c 47 ble.waitForEvent();
janjongboom 0:ba1c49874d3c 48 }
janjongboom 0:ba1c49874d3c 49
janjongboom 0:ba1c49874d3c 50 }