Skip to content

Commit

Permalink
Merge pull request sandeepmistry#21 from george-hawkins/master
Browse files Browse the repository at this point in the history
Add new iBeacon class for nRF51 boards.
  • Loading branch information
sandeepmistry committed May 23, 2015
2 parents 6f2dcb9 + 6d906be commit c85ef60
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/ibeacon/ibeacon.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <BLEPeripheral.h>
#include <iBeacon.h>

#if !defined(NRF51) && !defined(__RFduino__)
#error "This example only works with nRF51 boards"
#endif

static BLEPeripheral blePeripheral(0, 0, 0);

void setup() {
char* uuid = "a196c876-de8c-4c47-ab5a-d7afd5ae7127";
uint16_t major = 0;
uint16_t minor = 0;
int8_t measuredPower = -55;

iBeacon::setData(blePeripheral, uuid, major, minor, measuredPower);

blePeripheral.begin();
}

void loop() {
blePeripheral.poll();
}
33 changes: 33 additions & 0 deletions iBeacon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if defined(NRF51) || defined(__RFduino__)

#include <BLEUuid.h>

#include "iBeacon.h"

void iBeacon::setData(BLEPeripheral& peripheral, const char* uuidString, uint16_t major, uint16_t minor, int8_t measuredPower) {
unsigned char manufacturerData[MAX_UUID_LENGTH + 9]; // 4 bytes of header and 5 bytes of trailer.
BLEUuid uuid(uuidString);
int i = 0;

// 0x004c = Apple, see https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers
manufacturerData[i++] = 0x4c; // Apple Company Identifier LE (16 bit)
manufacturerData[i++] = 0x00;

// See "Beacon type" in "Building Applications with IBeacon".
manufacturerData[i++] = 0x02;
manufacturerData[i++] = uuid.length() + 5;

for (int j = (uuid.length() - 1); j >= 0; j--) {
manufacturerData[i++] = uuid.data()[j];
}

manufacturerData[i++] = major >> 8;
manufacturerData[i++] = major;
manufacturerData[i++] = minor >> 8;
manufacturerData[i++] = minor;
manufacturerData[i++] = measuredPower;

peripheral.setManufacturerData(manufacturerData, i);
}

#endif
16 changes: 16 additions & 0 deletions iBeacon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _I_BEACON_H_
#define _I_BEACON_H_

#if defined(NRF51) || !defined(__RFduino__)

#include "BLEPeripheral.h"

class iBeacon
{
public:
static void setData(BLEPeripheral& peripheral, const char* uuidString, uint16_t major, uint16_t minor, int8_t measuredPower);
};

#endif

#endif

0 comments on commit c85ef60

Please sign in to comment.