High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
main.cpp@15:327d7329072c, 2013-12-18 (annotated)
- Committer:
- ktownsend
- Date:
- Wed Dec 18 12:07:38 2013 +0000
- Revision:
- 15:327d7329072c
- Parent:
- 11:200931be5617
- Child:
- 16:542a9db01350
Cleanup for GAP publication (iBeacon working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:ace2e8d3ce79 | 1 | #include "mbed.h" |
ktownsend | 0:ace2e8d3ce79 | 2 | |
ktownsend | 0:ace2e8d3ce79 | 3 | #include "uuid.h" |
ktownsend | 2:ffc5216bd2cc | 4 | #include "GattService.h" |
ktownsend | 2:ffc5216bd2cc | 5 | #include "GattCharacteristic.h" |
ktownsend | 0:ace2e8d3ce79 | 6 | #include "hw/nrf51822.h" |
ktownsend | 0:ace2e8d3ce79 | 7 | |
ktownsend | 0:ace2e8d3ce79 | 8 | DigitalOut myled ( LED1 ); |
ktownsend | 0:ace2e8d3ce79 | 9 | |
ktownsend | 0:ace2e8d3ce79 | 10 | /* Radio HW abstraction layer */ |
ktownsend | 0:ace2e8d3ce79 | 11 | nRF51822 radio; |
ktownsend | 0:ace2e8d3ce79 | 12 | |
ktownsend | 0:ace2e8d3ce79 | 13 | /* Heart Rate Monitor Service */ |
ktownsend | 0:ace2e8d3ce79 | 14 | /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
ktownsend | 2:ffc5216bd2cc | 15 | GattService hrmService ( 0x180D ); |
ktownsend | 3:46de446e82ed | 16 | GattCharacteristic hrmRate ( 0x2A37, 2, 3, BLE_GATT_CHAR_PROPERTIES_NOTIFY ); |
ktownsend | 3:46de446e82ed | 17 | GattCharacteristic hrmLocation ( 0x2A39, 1, 1, BLE_GATT_CHAR_PROPERTIES_READ ); |
ktownsend | 0:ace2e8d3ce79 | 18 | |
ktownsend | 5:7635f81a8e09 | 19 | /* GAP Advertising Example (iBeacon) */ |
ktownsend | 6:425638944835 | 20 | GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED ); |
ktownsend | 5:7635f81a8e09 | 21 | GapAdvertisingData advData; |
ktownsend | 7:5e1f0d7f7c7d | 22 | GapAdvertisingData scanResponse; |
ktownsend | 5:7635f81a8e09 | 23 | |
ktownsend | 5:7635f81a8e09 | 24 | uint8_t iBeaconPayload[25] = { 0x4C, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 0x00, 0x00, 0x00, 0x00, 0xC8 }; |
ktownsend | 5:7635f81a8e09 | 25 | |
ktownsend | 5:7635f81a8e09 | 26 | void startBeacon(void) |
ktownsend | 5:7635f81a8e09 | 27 | { |
ktownsend | 11:200931be5617 | 28 | ble_error_t error; |
ktownsend | 11:200931be5617 | 29 | |
ktownsend | 5:7635f81a8e09 | 30 | /* iBeacon includes the FLAG and MSD fields */ |
ktownsend | 5:7635f81a8e09 | 31 | advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
ktownsend | 5:7635f81a8e09 | 32 | advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25); |
ktownsend | 5:7635f81a8e09 | 33 | |
ktownsend | 11:200931be5617 | 34 | error = radio.reset(); |
ktownsend | 11:200931be5617 | 35 | error = radio.setAdvertising(advParams, advData, scanResponse); |
ktownsend | 11:200931be5617 | 36 | error = radio.start(); |
ktownsend | 5:7635f81a8e09 | 37 | } |
ktownsend | 5:7635f81a8e09 | 38 | |
ktownsend | 15:327d7329072c | 39 | void startHRM(void) |
ktownsend | 0:ace2e8d3ce79 | 40 | { |
ktownsend | 15:327d7329072c | 41 | uint8_t hrmCounter = 100; |
ktownsend | 0:ace2e8d3ce79 | 42 | |
ktownsend | 0:ace2e8d3ce79 | 43 | /* Add the heart rate and sensor location chars to the HRM service */ |
ktownsend | 0:ace2e8d3ce79 | 44 | hrmService.addCharacteristic(hrmRate); |
ktownsend | 0:ace2e8d3ce79 | 45 | hrmService.addCharacteristic(hrmLocation); |
ktownsend | 15:327d7329072c | 46 | |
ktownsend | 0:ace2e8d3ce79 | 47 | /* Reset the BLE hardware to make sure we get a clean start */ |
ktownsend | 0:ace2e8d3ce79 | 48 | wait(2); |
ktownsend | 0:ace2e8d3ce79 | 49 | radio.reset(); |
ktownsend | 0:ace2e8d3ce79 | 50 | |
ktownsend | 0:ace2e8d3ce79 | 51 | /* Add the services to the radio HAL */ |
ktownsend | 0:ace2e8d3ce79 | 52 | radio.addService(hrmService); |
ktownsend | 0:ace2e8d3ce79 | 53 | |
ktownsend | 0:ace2e8d3ce79 | 54 | /* Start the services so that we can start pushing data out */ |
ktownsend | 0:ace2e8d3ce79 | 55 | radio.start(); |
ktownsend | 15:327d7329072c | 56 | |
ktownsend | 0:ace2e8d3ce79 | 57 | /* Set the heart rate monitor location (one time only) */ |
ktownsend | 0:ace2e8d3ce79 | 58 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ |
ktownsend | 0:ace2e8d3ce79 | 59 | uint8_t location = 0x01; /* Chest */ |
ktownsend | 2:ffc5216bd2cc | 60 | radio.writeCharacteristic(hrmService, hrmLocation, (uint8_t*)&location, sizeof(location)); |
ktownsend | 0:ace2e8d3ce79 | 61 | |
ktownsend | 0:ace2e8d3ce79 | 62 | while(1) |
ktownsend | 0:ace2e8d3ce79 | 63 | { |
ktownsend | 0:ace2e8d3ce79 | 64 | myled = 1; |
ktownsend | 0:ace2e8d3ce79 | 65 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 66 | myled = 0; |
ktownsend | 0:ace2e8d3ce79 | 67 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 68 | |
ktownsend | 0:ace2e8d3ce79 | 69 | /* Update the HRM measurement */ |
ktownsend | 0:ace2e8d3ce79 | 70 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
ktownsend | 0:ace2e8d3ce79 | 71 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
ktownsend | 0:ace2e8d3ce79 | 72 | hrmCounter++; |
ktownsend | 0:ace2e8d3ce79 | 73 | if (hrmCounter == 175) hrmCounter = 100; |
ktownsend | 0:ace2e8d3ce79 | 74 | uint8_t bpm[2] = { 0x00, hrmCounter }; |
ktownsend | 2:ffc5216bd2cc | 75 | radio.writeCharacteristic(hrmService, hrmRate, bpm, 2); |
ktownsend | 0:ace2e8d3ce79 | 76 | } |
ktownsend | 0:ace2e8d3ce79 | 77 | } |
ktownsend | 15:327d7329072c | 78 | |
ktownsend | 15:327d7329072c | 79 | int main() |
ktownsend | 15:327d7329072c | 80 | { |
ktownsend | 15:327d7329072c | 81 | /* Give the radio some time to boot up and settle */ |
ktownsend | 15:327d7329072c | 82 | wait(2); |
ktownsend | 15:327d7329072c | 83 | |
ktownsend | 15:327d7329072c | 84 | /* Only use one of these two options! */ |
ktownsend | 15:327d7329072c | 85 | startBeacon(); |
ktownsend | 15:327d7329072c | 86 | // startHRM(); |
ktownsend | 15:327d7329072c | 87 | |
ktownsend | 15:327d7329072c | 88 | while(1); |
ktownsend | 15:327d7329072c | 89 | } |