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 18:49:39 2014 +0000
Revision:
25:7cf2a38ea175
Parent:
24:12eb3f19e9a4
Minor cleanup (better comments, etc.)

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 16:542a9db01350 5 /* Radio HW */
ktownsend 0:ace2e8d3ce79 6 nRF51822 radio;
ktownsend 0:ace2e8d3ce79 7
ktownsend 5:7635f81a8e09 8 void startBeacon(void)
ktownsend 5:7635f81a8e09 9 {
ktownsend 16:542a9db01350 10 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED );
ktownsend 16:542a9db01350 11 GapAdvertisingData advData;
ktownsend 16:542a9db01350 12 GapAdvertisingData scanResponse;
ktownsend 16:542a9db01350 13
ktownsend 25:7cf2a38ea175 14 /* Define an iBeacon payload
ktownsend 25:7cf2a38ea175 15 --------------------------------------------------------------
ktownsend 25:7cf2a38ea175 16 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
ktownsend 25:7cf2a38ea175 17 Major/Minor = 0000 / 0000
ktownsend 25:7cf2a38ea175 18 Tx Power = C8
ktownsend 25:7cf2a38ea175 19 */
ktownsend 16:542a9db01350 20 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 21
ktownsend 5:7635f81a8e09 22 /* iBeacon includes the FLAG and MSD fields */
ktownsend 25:7cf2a38ea175 23 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ktownsend 25:7cf2a38ea175 24 advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, sizeof(iBeaconPayload));
ktownsend 16:542a9db01350 25
ktownsend 25:7cf2a38ea175 26 radio.reset();
ktownsend 25:7cf2a38ea175 27 radio.setAdvertising(advParams, advData, scanResponse);
ktownsend 25:7cf2a38ea175 28 radio.start();
ktownsend 23:f19c60478e1b 29 }
ktownsend 23:f19c60478e1b 30
ktownsend 23:f19c60478e1b 31 void startBatteryService(void)
ktownsend 23:f19c60478e1b 32 {
ktownsend 23:f19c60478e1b 33 GattService battService ( 0x180F );
ktownsend 24:12eb3f19e9a4 34 GattCharacteristic battLevel ( 0x2A19, 1, 1, BLE_GATT_CHAR_PROPERTIES_NOTIFY | BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 24:12eb3f19e9a4 35
ktownsend 24:12eb3f19e9a4 36 /* Make sure we get a clean start */
ktownsend 25:7cf2a38ea175 37 radio.reset();
ktownsend 24:12eb3f19e9a4 38
ktownsend 24:12eb3f19e9a4 39 /* Add the characteristic to our service */
ktownsend 25:7cf2a38ea175 40 battService.addCharacteristic(battLevel);
ktownsend 24:12eb3f19e9a4 41
ktownsend 24:12eb3f19e9a4 42 /* Pass the service into the radio */
ktownsend 25:7cf2a38ea175 43 radio.addService(battService);
ktownsend 24:12eb3f19e9a4 44
ktownsend 24:12eb3f19e9a4 45 /* Configure the radio and start advertising with default values */
ktownsend 24:12eb3f19e9a4 46 /* Make sure you've added all of your services before calling this function! */
ktownsend 25:7cf2a38ea175 47 radio.start();
ktownsend 23:f19c60478e1b 48
ktownsend 24:12eb3f19e9a4 49 /* Now that we're live, update the battery level characteristic */
ktownsend 23:f19c60478e1b 50 uint8_t batt = 72;
ktownsend 25:7cf2a38ea175 51 radio.writeCharacteristic(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
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 25:7cf2a38ea175 59 /* Select one of the options below depending on the demo you want to see */
ktownsend 23:f19c60478e1b 60 // startBeacon();
ktownsend 23:f19c60478e1b 61 startBatteryService();
ktownsend 15:327d7329072c 62
ktownsend 15:327d7329072c 63 while(1);
ktownsend 15:327d7329072c 64 }