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:
Tue Jan 07 19:58:06 2014 +0000
Revision:
23:f19c60478e1b
Parent:
16:542a9db01350
Child:
24:12eb3f19e9a4
Added GattService example

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 11:200931be5617 19
ktownsend 5:7635f81a8e09 20 /* iBeacon includes the FLAG and MSD fields */
ktownsend 16:542a9db01350 21 error = advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ktownsend 16:542a9db01350 22 error = advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25);
ktownsend 16:542a9db01350 23
ktownsend 11:200931be5617 24 error = radio.reset();
ktownsend 11:200931be5617 25 error = radio.setAdvertising(advParams, advData, scanResponse);
ktownsend 11:200931be5617 26 error = radio.start();
ktownsend 23:f19c60478e1b 27
ktownsend 23:f19c60478e1b 28 /* Hang around here for a while */
ktownsend 23:f19c60478e1b 29 while(1)
ktownsend 23:f19c60478e1b 30 {
ktownsend 23:f19c60478e1b 31 }
ktownsend 23:f19c60478e1b 32 }
ktownsend 23:f19c60478e1b 33
ktownsend 23:f19c60478e1b 34 void startBatteryService(void)
ktownsend 23:f19c60478e1b 35 {
ktownsend 23:f19c60478e1b 36 ble_error_t error;
ktownsend 23:f19c60478e1b 37 GattService battService ( 0x180F );
ktownsend 23:f19c60478e1b 38 GattCharacteristic battLevel ( 0x2A19, 1, 1, 0x10 | 0x02);
ktownsend 23:f19c60478e1b 39
ktownsend 23:f19c60478e1b 40 error = radio.reset();
ktownsend 23:f19c60478e1b 41 error = battService.addCharacteristic(battLevel);
ktownsend 23:f19c60478e1b 42 error = radio.addService(battService);
ktownsend 23:f19c60478e1b 43 error = radio.start();
ktownsend 23:f19c60478e1b 44
ktownsend 23:f19c60478e1b 45 uint8_t batt = 72;
ktownsend 23:f19c60478e1b 46 error = radio.writeCharacteristic(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
ktownsend 23:f19c60478e1b 47
ktownsend 23:f19c60478e1b 48 /* Hang around here for a while */
ktownsend 23:f19c60478e1b 49 while(1)
ktownsend 23:f19c60478e1b 50 {
ktownsend 23:f19c60478e1b 51 }
ktownsend 5:7635f81a8e09 52 }
ktownsend 5:7635f81a8e09 53
ktownsend 15:327d7329072c 54 int main()
ktownsend 15:327d7329072c 55 {
ktownsend 15:327d7329072c 56 /* Give the radio some time to boot up and settle */
ktownsend 15:327d7329072c 57 wait(2);
ktownsend 15:327d7329072c 58
ktownsend 23:f19c60478e1b 59 // startBeacon();
ktownsend 23:f19c60478e1b 60 startBatteryService();
ktownsend 15:327d7329072c 61
ktownsend 15:327d7329072c 62 while(1);
ktownsend 15:327d7329072c 63 }