diff --git a/examples/ibeacon/ibeacon.ino b/examples/ibeacon/ibeacon.ino index 5ac1197..eb9d569 100644 --- a/examples/ibeacon/ibeacon.ino +++ b/examples/ibeacon/ibeacon.ino @@ -5,19 +5,17 @@ #error "This example only works with nRF51 boards" #endif -static BLEPeripheral blePeripheral(0, 0, 0); +iBeacon beacon; 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); + char* uuid = "a196c876-de8c-4c47-ab5a-d7afd5ae7127"; + unsigned short major = 0; + unsigned short minor = 0; + unsigned short measuredPower = -55; - blePeripheral.begin(); + beacon.begin(uuid, major, minor, measuredPower); } void loop() { - blePeripheral.poll(); + beacon.loop(); } diff --git a/iBeacon.cpp b/iBeacon.cpp index c1abf69..4cc0e9d 100644 --- a/iBeacon.cpp +++ b/iBeacon.cpp @@ -1,10 +1,16 @@ #if defined(NRF51) || defined(__RFduino__) -#include +#include "BLEUuid.h" #include "iBeacon.h" -void iBeacon::setData(BLEPeripheral& peripheral, const char* uuidString, uint16_t major, uint16_t minor, int8_t measuredPower) { +iBeacon::iBeacon() : + _blePeripheral(0, 0, 0) +{ + this->_blePeripheral.setConnectable(false); +} + +void iBeacon::begin(const char* uuidString, unsigned short major, unsigned short minor, char measuredPower) { unsigned char manufacturerData[MAX_UUID_LENGTH + 9]; // 4 bytes of header and 5 bytes of trailer. BLEUuid uuid(uuidString); int i = 0; @@ -12,7 +18,7 @@ void iBeacon::setData(BLEPeripheral& peripheral, const char* uuidString, uint16_ // 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; @@ -27,7 +33,13 @@ void iBeacon::setData(BLEPeripheral& peripheral, const char* uuidString, uint16_ manufacturerData[i++] = minor; manufacturerData[i++] = measuredPower; - peripheral.setManufacturerData(manufacturerData, i); + this->_blePeripheral.setManufacturerData(manufacturerData, i); + + this->_blePeripheral.begin(); +} + +void iBeacon::loop() { + this->_blePeripheral.poll(); } #endif diff --git a/iBeacon.h b/iBeacon.h index e0ffb68..d28b199 100644 --- a/iBeacon.h +++ b/iBeacon.h @@ -8,7 +8,13 @@ class iBeacon { public: - static void setData(BLEPeripheral& peripheral, const char* uuidString, uint16_t major, uint16_t minor, int8_t measuredPower); + iBeacon(); + + void begin(const char* uuidString, unsigned short major, unsigned short minor, char measuredPower); + void loop(); + + private: + BLEPeripheral _blePeripheral; }; #endif diff --git a/keywords.txt b/keywords.txt index aae2464..0b3cfa5 100644 --- a/keywords.txt +++ b/keywords.txt @@ -43,6 +43,7 @@ BLEKeyboard KEYWORD1 BLEMultimedia KEYWORD1 BLESystemControl KEYWORD1 URIBeacon KEYWORD1 +iBeacon KEYWORD1 ####################################### # Methods and Functions (KEYWORD2)