mbed-x / Mbed OS SimpleBLE-Example-mbedos5

Dependencies:   SimpleBLE-mbed-os-5

Fork of SimpleBLE-Example by mbed-x

Files at this revision

API Documentation at this revision

Comitter:
janjongboom
Date:
Wed May 11 13:01:37 2016 +0000
Child:
1:2d61e4e5b18d
Commit message:
SimpleBLE Example initial commit

Changed in this revision

SimpleBLE.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleBLE.lib	Wed May 11 13:01:37 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/janjongboom/code/SimpleBLE/#2ecd71f6ab04
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 11 13:01:37 2016 +0000
@@ -0,0 +1,50 @@
+/**
+ * This is an example program that's using the SimpleBLE library
+ * to expose some variables to the outside world using BLE.
+ * Written by Jan Jongboom (jan.jongboom@arm.com)
+ */
+
+#include "mbed.h"
+#include "SimpleBLE.h"
+
+// The first thing we need to do is create a SimpleBLE instance:
+// * first argument is the device name
+// * second is the advertisement interval in ms. (default 1000 ms.)
+SimpleBLE ble("DEVICE_NAME", 1000);
+
+// Now we can declare some variables that we want to expose.
+// After you created the variable you can use it like any other var,
+// but it's value will be automatically updated over Bluetooth!
+
+// F.e. here we declare service 0x180d (heartrate), char 0x2a37 (curr. value) as uint8_t
+SimpleChar<uint8_t> heartrate = ble.readOnly<uint8_t>(0x180D, 0x2A37, true /* notify */, 100 /* default value */);
+
+// now we can use this variable everywhere in our code like a normal uint8_t
+void updateHeartrate() {
+    // we just loop between 100 and 180
+    heartrate = heartrate + 1;
+    if (heartrate > 180) {
+        heartrate = 100;
+    }
+}
+
+// And here we create a custom service (0x9310) and char (0x9311) with a callback
+void callback(uint32_t newValue) {
+    // whenever someone updates this var over Bluetooth, this function will be called
+    printf("My value was updated to %d\n", newValue);
+}
+// FYI, you can also use UUIDs here instead of short services :-)
+SimpleChar<uint32_t> writeMe = ble.readWrite<uint32_t>(0x9310, 0x9311, &callback);
+
+int main(int, char**) {
+    // update the heart rate every second
+    Ticker t;
+    t.attach(updateHeartrate, 1.0f);
+    
+    // here's how we kick off our loop
+    ble.start();
+    while (1) {
+        ble.waitForEvent();
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed May 11 13:01:37 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nRF51822.lib	Wed May 11 13:01:37 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#f7faad332abc