![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A blue button is always a nice toy ...
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp
- Committer:
- hux
- Date:
- 2017-01-06
- Revision:
- 23:677689000369
- Parent:
- 22:d467526abc4a
- Child:
- 24:0f08f68579bd
File content as of revision 23:677689000369:
#include <mbed.h> #include "ble/BLE.h" #include "bricks.h" Serial out(USBTX, USBRX); // serial port to PC terminal //============================================================================== // Custom Service Definition //============================================================================== // occupation service Collection detection; Characteristic<Digital> presence (detection,0xA011, "rn", "Presence"); Characteristic<DateTime> timestamp(detection,0xA012, "r", "Timestamp"); Characteristic<Digital> test (detection,0xA013, "w", "Test"); Service service1(detection,0xA010); // our occupation service // address service Collection address; Characteristic<Digital> id (address, 0xA021, "ran", "ID"); Characteristic<Digital> name (address, 0xA022, "ran", "Name"); Characteristic<Digital> layout (address, 0xA023, "ran", "Layout"); Service service2(address,0xA020); // our occupation service //============================================================================== // Some Callbacks //============================================================================== void print(Blob &blue, GattCharacteristic &chr, const char *name) { Buffer data; uint8_t *p = data; blue.get(chr,data); out.printf("%s: %02x-%02x-%02x-%02x-%02x-",name,(int)(p[0]),(int)(p[1]),(int)(p[2]),(int)(p[3]),(int)(p[4])); out.printf("%02x-%02x-%02x-%02x-%02x\r\n",(int)(p[5]),(int)(p[6]),(int)(p[7]),(int)(p[8]),(int)(p[9])); } void onWritten(Blob &o) // Handle writes to writeCharacteristic { const GattWriteCallbackParams *p = o.pWritten; // Check to see what characteristic was written, by handle if(p->handle == test.getValueHandle()) { Digital value; get(o,test,value); set(o,presence,value); print(o,test,"test"); blink("x x x xxx xxx xxx x ",CONNECTED); } } void onConnect(Blob &blue) // Connection Callback { blink("x x x ",CONNECTED); // indicate advertising } void onDisconnect(Blob &blue) // Disconnection Callback { blue.start(); // start advertising on client disconnect blink(ADVERTISE); // indicate advertising } void onError(Blob &blue) // Error Reporting Callback { (void) blue; // Avoid compiler warnings blink(FAULT); // indicate advertising } void onSetup(Blob &blue) // Immediately After Initializing BLE { blue.config(service1); // configure custom service 1 blue.config(service1); // configure custom service 2 blue.name("T07N#8 Easy GATT"); // setup advertising name blue.device("nRF51-DK"); // setup device name blue.data("ABCDEF"); // setup advertising data blue.onConnect(onConnect); // setup connection callback blue.onDisconnect(onDisconnect); // setup disconnection callback blue.onWritten(onWritten); // setup data written callback blue.advertise("C:ng",100); // start advertising @ 100 msec interval blink(ADVERTISE); // show that board is advertising } //============================================================================== // Main Program //============================================================================== int main(void) { out.printf(" hello\r\n"); verbose(10); // print all verbose messages blink(IDLE); // idle blinking - just started! Blob blue; // declare a blob (BLE OBject) blue.init(onSetup,onError); // init BLE base layer, always do first while (true) // Infinite loop waiting for BLE events blue.sleep(); // low power waiting for BLE events }