Example program for the Eddystone Beacon service.
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_EddystoneBeacon by
This example demonstrates how to set up and initialize a Eddystone Beacon. For more details on the Eddystone specification please see the Eddystone Github Page.
The Basics
An Eddystone Beacon is a Bluetooth Low Energy beacon, that means it does all of its data transfer in GAP advertising packets. Eddystone beacons, unlike other beacons, broadcast multiple types of data. Currently Eddystone beacons have 3 frame types (UID, URL, TLM) that will get swapped out periodically. This swapping of frame data allows Eddystone beacons to send many different types of data, thus increasing their usefulness over traditional beacons. Note that the UID frame type provides the same 16Bytes of UUID namespace that iBeacons do and the URL frame type provides the same functionality as a URIBeacon.
For more details see the Eddystone Specification.
Smartphone Apps
nRF Master Control Panel - this program recognizes Eddystone beacons and will display the data for all frame types.
Walkthrough of Physical Web application
Technical Details
The Eddystone Specification looks like the following image. Please note that this may change over time and for up to date information the official spec should be referenced.
The Eddystone Frames get swapped in and out depending on what frames you have enabled. The only required frame type is the TLM frame, all others are optional and you can have any number enabled. To disable the UID or URL frames give their values a 'NULL' in the Eddystone constructor. The Eddystone spec recommends broadcasting 10 frames a second.
Note
- The current Eddystone mbed example does not allow for combining the eddystone service with other services, this will be changes in a future update.
- There is an Eddystone Config service that allows for updating beacons in the field. We are working on an example for this, so keep your eyes pealed for a future update.
Diff: ZipBeaconConfigService.h
- Revision:
- 17:0458759c40e4
- Parent:
- 16:a7d07ea94b31
- Child:
- 18:91c36071aafb
--- a/ZipBeaconConfigService.h Wed Jul 22 18:01:20 2015 +0000 +++ b/ZipBeaconConfigService.h Wed Jul 22 21:28:09 2015 +0000 @@ -39,7 +39,7 @@ static const uint8_t BEACON_EDDYSTONE[] = {0xAA, 0xFE}; //Debug is disabled by default -#if 0 +#if 1 #define DBG(x, ...) printf("[EddyStone: DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); #define WARN(x, ...) printf("[EddyStone: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); #define ERR(x, ...) printf("[EddyStone: ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); @@ -242,9 +242,7 @@ * @return number of bytes used. negative number indicates error message. */ int constructUIDFrame(uint8_t * Data, uint8_t maxSize) { - if(maxSize < FRAME_SIZE_UID) { - return -1; // not enough space to encode UIDframe in advertising packet. - } + int index = 0; Data[index++] = FRAME_TYPE_UID; // 1B Type Data[index++] = defaultUidPower; // 1B Power @ 0meter @@ -258,6 +256,7 @@ Data[index++] = (uint8_t)(uidRFU >> 8); Data[index++] = (uint8_t)uidRFU; } + DBG("construcUIDFrame %d, %d",maxSize,index); return index; } @@ -283,15 +282,13 @@ * @return number of bytes used. negative number indicates error message. */ int constructURLFrame(uint8_t * Data, uint8_t maxSize) { - if(maxSize < (2 + defaultUriDataLength)) { - return -1; // not enough space to encode URL frame in advertising packet. - } int index = 0; Data[index++] = FRAME_TYPE_URL; // 1B Type Data[index++] = params.txPowerMode; // 1B TX Power for(int x = 0; x < defaultUriDataLength; x++) { // 18B of URL Prefix + encoded URL Data[index++] = defaultUriData[x]; } + DBG("constructURLFrame: %d, %d",maxSize,index); return index; } @@ -318,9 +315,6 @@ * @return number of bytes used. negative number indicates error message. */ int constructTLMFrame(uint8_t * Data, uint8_t maxSize) { - if(maxSize < FRAME_SIZE_TLM) { // error, not enough space to add TLM frame. 14B, every time - return -1; - } int index = 0; Data[index++] = FRAME_TYPE_TLM; // Eddystone frame type = Telemetry Data[index++] = TlmVersion; // TLM Version Number @@ -336,7 +330,7 @@ Data[index++] = (uint8_t)(TlmTimeSinceBoot>>8); // Time Since Boot [1] Data[index++] = (uint8_t)(TlmTimeSinceBoot>>16); // Time Since Boot [2] Data[index++] = (uint8_t)(TlmTimeSinceBoot>>24); // Time Since Boot [3] - + DBG("constructURLFrame: %d, %d",maxSize,index); return index; } @@ -394,22 +388,17 @@ */ bool updateAdvPacket(uint8_t serviceData[], unsigned serviceDataLen) { // Fields from the Service - DBG("Updating Adv Data: %d", serviceDataLen); - + DBG("Updating AdvFrame: %d", serviceDataLen); + printf("\r\n"); + for(int x = 0; x<serviceDataLen; x++) { + printf("%2.2x:",serviceData[x]); + } + printf("\r\n"); ble.clearAdvertisingPayload(); ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE)); ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen); - // attach callback to count number of sent packets - -// printf("\r\n"); -// for(int x = 0; x<serviceDataLen; x++) { -// printf("%2.2x:",serviceData[x]); -// } -// printf("\r\n"); - - ble.gap().startAdvertising(); return true; } @@ -419,26 +408,25 @@ * */ void radioNotificationCallback(bool radioActive) { - DBG("RadioNotificationCallback : %d, %d",radioActive,frameIndex); + DBG("RadioNotificationCallback : %d, %d, %d",radioActive,frameIndex,TlmPduCount); // Update Time and PDUCount // True just before an frame is sent, fale just after a frame is sent - if(radioActive) { + + TlmPduCount++; + frameIndex = frameIndex % EDDYSTONE_MAX_FRAMETYPE; + uint8_t serviceData[SERVICE_DATA_MAX]; + unsigned serviceDataLen = 0; + //hard code in the eddystone UUID + serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; + serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; + if(!radioActive) { // Do Nothing } else { - static bool switchFlag; - TlmPduCount++; - frameIndex = frameIndex % EDDYSTONE_MAX_FRAMETYPE; - uint8_t serviceData[SERVICE_DATA_MAX]; - unsigned serviceDataLen = 0; - //hard code in the eddystone UUID - serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; - serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; - // state machine to control which packet is being sent switch(frameIndex) { case 0: - DBG("\tconstructing TLM Frame: "); - serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); + serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,20); + DBG("\tconstructing TLM Frame: len= %d",serviceDataLen); updateAdvPacket(serviceData,serviceDataLen); frameIndex++; switchFlag = true; @@ -446,8 +434,8 @@ case 1: // switch out packets if(switchFlag) { - DBG("\tconstructing URL Frame: "); - serviceDataLen += constructURLFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); + serviceDataLen += constructURLFrame(serviceData+serviceDataLen,20); + DBG("\tconstructing URL Frame: len = %d ",serviceDataLen); updateAdvPacket(serviceData,serviceDataLen); switchFlag = false; } else { @@ -460,8 +448,8 @@ case 2: // switch out packets if(switchFlag) { - DBG("\tconstructing UID Frame: "); - serviceDataLen += constructUIDFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); + serviceDataLen += constructUIDFrame(serviceData+serviceDataLen,20); + DBG("\tconstructing UID Frame: len=%d",serviceDataLen); updateAdvPacket(serviceData,serviceDataLen); switchFlag = false; } else { @@ -491,8 +479,10 @@ uint8_t flags = params.flags; // Initialize Frame transition - frameIndex = 0; + frameIndex = 2; uidRFU = 0; + switchFlag = true; + /* Reinitialize the BLE stack. This will clear away the existing services and advertising state. */ ble.shutdown(); @@ -500,30 +490,22 @@ ble.setTxPower(params.advPowerLevels[params.txPowerMode]); ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(beaconPeriod); - ble.gap().onRadioNotification(this,&ZipBeaconConfigService::radioNotificationCallback); extern void saveURIBeaconConfigParams(const Params_t *paramsP); /* forward declaration; necessary to avoid a circular dependency. */ saveURIBeaconConfigParams(¶ms); - setTLMFrameData(0x00,22,33,0,0); // Initialize TLM Data, for testing, remove for release - updateTlmPduCount(0); - updateTlmTimeSinceBoot(0); + setTLMFrameData(0x00,0x2222,0x3333,0x01,0x02); // Initialize TLM Data, for testing, remove for release + updateTlmPduCount(0x11); + updateTlmTimeSinceBoot(0x22); // Construct TLM Frame in initial advertising. serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0]; serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1]; - serviceDataLen += constructTLMFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); + serviceDataLen += constructURLFrame(serviceData+serviceDataLen,SERVICE_DATA_MAX); updateAdvPacket(serviceData, serviceDataLen); -// serviceData[serviceDataLen++] = FRAME_TYPE_URL | flags; -// serviceData[serviceDataLen++] = advPowerLevels[txPowerMode]; -// for (unsigned j = 0; j < uriDataLength; j++) { -// serviceData[serviceDataLen++] = uriData[j]; -// } - - - //callbackTick.attach(this,&ZipBeaconConfigService::radioNotificationCallback, 0.1); // switch services every 2 seconds + ble.gap().startAdvertising(); + ble.gap().onRadioNotification(this,&ZipBeaconConfigService::radioNotificationCallback); timeSinceBootTick.attach(this,&ZipBeaconConfigService::tsbCallback,0.1); // incriment the TimeSinceBoot ticker every 0.1s - //callbackTick.attach(stupidWrapperFn,2.0); } private: @@ -697,6 +679,7 @@ uint8_t lockedState; bool initSucceeded; uint8_t resetFlag; + bool switchFlag; // Private Variables for Telemetry Data uint8_t TlmVersion;