Skip to content

Commit

Permalink
- Eddystone Beacon API
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed Jul 15, 2015
1 parent 890c37f commit 1a48882
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
119 changes: 119 additions & 0 deletions EddystoneBeacon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "EddystoneBeacon.h"

#define MAX_SERVICE_DATA_SIZE 20

#define FLAGS_UID 0x00
#define FLAGS_URL 0x10
#define FLAGS_TLM 0x20

static const char* EDDYSTONE_URL_BEACON_PREFIX_SUBSTITUTIONS[] = {
"http://www.",
"https://www.",
"http://",
"https://",
"urn:uuid:"
};

static const char* EDDYSTONE_URL_BEACON_SUFFIX_SUBSTITUTIONS[] = {
".com/",
".org/",
".edu/",
".net/",
".info/",
".biz/",
".gov/",
".com",
".org",
".edu",
".net",
".info",
".biz",
".gov"
};

EddystoneBeacon::EddystoneBeacon(unsigned char req, unsigned char rdy, unsigned char rst) :
BLEPeripheral(req, rdy, rst),
_bleService("feaa"),
_bleCharacteristic("feab", BLERead | BLEBroadcast, MAX_SERVICE_DATA_SIZE)
{
this->setAdvertisedServiceUuid(this->_bleService.uuid());

this->setConnectable(false);

this->addAttribute(this->_bleService);
this->addAttribute(this->_bleCharacteristic);
}

void EddystoneBeacon::begin(unsigned char power, const BLEUuid& uid) {
unsigned char serviceData[MAX_SERVICE_DATA_SIZE];

this->_power = power;

memset(serviceData, 0x00, sizeof(serviceData));
serviceData[0] = FLAGS_UID;
serviceData[1] = this->_power;

const unsigned char* uidData = uid.data();
for (int i = 0; i < 16; i++) {
serviceData[i + 2] = uidData[15 - i];
}

serviceData[18] = 0x00; // Reserved for future use, must be: 0x00
serviceData[19] = 0x00; // Reserved for future use, must be: 0x00

this->_bleCharacteristic.setValue(serviceData, sizeof(serviceData));

BLEPeripheral::begin();

this->_bleCharacteristic.broadcast();
}

void EddystoneBeacon::begin(unsigned char power, const char* uri) {
this->_power = power;
this->setURI(uri);

BLEPeripheral::begin();

this->_bleCharacteristic.broadcast();
}

void EddystoneBeacon::setURI(const char* uri) {
unsigned char serviceData[MAX_SERVICE_DATA_SIZE];

serviceData[0] = FLAGS_URL;
serviceData[1] = this->_power;
unsigned char compressedURIlength = this->compressURI(uri, (char *)&serviceData[2], sizeof(serviceData) - 2);

this->_bleCharacteristic.setValue(serviceData, 2 + compressedURIlength);
}

unsigned char EddystoneBeacon::compressURI(const char* uri, char *compressedUri, unsigned char compressedUriSize) {
String uriString = uri;

// replace prefixes
for (unsigned int i = 0; i < (sizeof(EDDYSTONE_URL_BEACON_PREFIX_SUBSTITUTIONS) / sizeof(char *)); i++) {
String replacement = " ";
replacement[0] = (char)(i | 0x80); // set high bit, String.replace does not like '\0' replacement

uriString.replace(EDDYSTONE_URL_BEACON_PREFIX_SUBSTITUTIONS[i], replacement);
}

// replace suffixes
for (unsigned int i = 0; i < (sizeof(EDDYSTONE_URL_BEACON_SUFFIX_SUBSTITUTIONS) / sizeof(char *)); i++) {
String replacement = " ";
replacement[0] = (char)(i | 0x80); // set high bit, String.replace does not like '\0' replacement

uriString.replace(EDDYSTONE_URL_BEACON_SUFFIX_SUBSTITUTIONS[i], replacement);
}

unsigned char i = 0;
for (i = 0; i < uriString.length() && i < compressedUriSize; i++) {
compressedUri[i] = (uriString[i] & 0x7f); // assign byte after clearing hight bit
}

return i;
}

void EddystoneBeacon::loop() {
this->poll();
}
27 changes: 27 additions & 0 deletions EddystoneBeacon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _EDDYSTONE_BEACON_H_
#define _EDDYSTONE_BEACON_H_

#include "BLEPeripheral.h"
#include "BLEUuid.h"

class EddystoneBeacon : public BLEPeripheral
{
public:
EddystoneBeacon(unsigned char req, unsigned char rdy, unsigned char rst);

void begin(unsigned char power, const BLEUuid& uid);
void begin(unsigned char power, const char* uri);
void loop();

void setURI(const char* uri);

private:
unsigned char compressURI(const char* uri, char *compressedUri, unsigned char compressedUriSize);

unsigned char _power;

BLEService _bleService;
BLECharacteristic _bleCharacteristic;
};

#endif
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ BLEMultimedia KEYWORD1
BLESystemControl KEYWORD1
URIBeacon KEYWORD1
iBeacon KEYWORD1
EddystoneBeacon KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down

0 comments on commit 1a48882

Please sign in to comment.