High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Wed Jan 08 11:13:14 2014 +0000
Revision:
24:12eb3f19e9a4
Parent:
23:f19c60478e1b
Child:
25:7cf2a38ea175
Code cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:ace2e8d3ce79 1 #include "mbed.h"
ktownsend 0:ace2e8d3ce79 2 #include "uuid.h"
ktownsend 0:ace2e8d3ce79 3 #include "hw/nrf51822.h"
ktownsend 0:ace2e8d3ce79 4
ktownsend 0:ace2e8d3ce79 5 DigitalOut myled ( LED1 );
ktownsend 0:ace2e8d3ce79 6
ktownsend 16:542a9db01350 7 /* Radio HW */
ktownsend 0:ace2e8d3ce79 8 nRF51822 radio;
ktownsend 0:ace2e8d3ce79 9
ktownsend 5:7635f81a8e09 10 void startBeacon(void)
ktownsend 5:7635f81a8e09 11 {
ktownsend 23:f19c60478e1b 12 ble_error_t error;
ktownsend 16:542a9db01350 13 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED );
ktownsend 16:542a9db01350 14 GapAdvertisingData advData;
ktownsend 16:542a9db01350 15 GapAdvertisingData scanResponse;
ktownsend 16:542a9db01350 16
ktownsend 16:542a9db01350 17 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 16:542a9db01350 18
ktownsend 5:7635f81a8e09 19 /* iBeacon includes the FLAG and MSD fields */
ktownsend 16:542a9db01350 20 error = advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ktownsend 16:542a9db01350 21 error = advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25);
ktownsend 16:542a9db01350 22
ktownsend 11:200931be5617 23 error = radio.reset();
ktownsend 11:200931be5617 24 error = radio.setAdvertising(advParams, advData, scanResponse);
ktownsend 11:200931be5617 25 error = radio.start();
ktownsend 23:f19c60478e1b 26
ktownsend 23:f19c60478e1b 27 /* Hang around here for a while */
ktownsend 23:f19c60478e1b 28 while(1)
ktownsend 23:f19c60478e1b 29 {
ktownsend 23:f19c60478e1b 30 }
ktownsend 23:f19c60478e1b 31 }
ktownsend 23:f19c60478e1b 32
ktownsend 23:f19c60478e1b 33 void startBatteryService(void)
ktownsend 23:f19c60478e1b 34 {
ktownsend 23:f19c60478e1b 35 ble_error_t error;
ktownsend 23:f19c60478e1b 36 GattService battService ( 0x180F );
ktownsend 24:12eb3f19e9a4 37 GattCharacteristic battLevel ( 0x2A19, 1, 1, BLE_GATT_CHAR_PROPERTIES_NOTIFY | BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 24:12eb3f19e9a4 38
ktownsend 24:12eb3f19e9a4 39 /* Make sure we get a clean start */
ktownsend 23:f19c60478e1b 40 error = radio.reset();
ktownsend 24:12eb3f19e9a4 41
ktownsend 24:12eb3f19e9a4 42 /* Add the characteristic to our service */
ktownsend 23:f19c60478e1b 43 error = battService.addCharacteristic(battLevel);
ktownsend 24:12eb3f19e9a4 44
ktownsend 24:12eb3f19e9a4 45 /* Pass the service into the radio */
ktownsend 23:f19c60478e1b 46 error = radio.addService(battService);
ktownsend 24:12eb3f19e9a4 47
ktownsend 24:12eb3f19e9a4 48 /* Configure the radio and start advertising with default values */
ktownsend 24:12eb3f19e9a4 49 /* Make sure you've added all of your services before calling this function! */
ktownsend 23:f19c60478e1b 50 error = radio.start();
ktownsend 23:f19c60478e1b 51
ktownsend 24:12eb3f19e9a4 52 /* Now that we're live, update the battery level characteristic */
ktownsend 23:f19c60478e1b 53 uint8_t batt = 72;
ktownsend 23:f19c60478e1b 54 error = radio.writeCharacteristic(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
ktownsend 23:f19c60478e1b 55
ktownsend 23:f19c60478e1b 56 /* Hang around here for a while */
ktownsend 23:f19c60478e1b 57 while(1)
ktownsend 23:f19c60478e1b 58 {
ktownsend 23:f19c60478e1b 59 }
ktownsend 5:7635f81a8e09 60 }
ktownsend 5:7635f81a8e09 61
ktownsend 15:327d7329072c 62 int main()
ktownsend 15:327d7329072c 63 {
ktownsend 15:327d7329072c 64 /* Give the radio some time to boot up and settle */
ktownsend 15:327d7329072c 65 wait(2);
ktownsend 15:327d7329072c 66
ktownsend 23:f19c60478e1b 67 // startBeacon();
ktownsend 23:f19c60478e1b 68 startBatteryService();
ktownsend 15:327d7329072c 69
ktownsend 15:327d7329072c 70 while(1);
ktownsend 15:327d7329072c 71 }