Skip to content

Commit 5cb4df9

Browse files
committed
Add iBeacon example
1 parent 77f66f6 commit 5cb4df9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* iBeacon example
3+
*
4+
* This example demonstrates how to publish an Apple-compatible iBeacon
5+
*
6+
* Created: on May 26 2025
7+
* Author: lazd
8+
*/
9+
10+
#include <Arduino.h>
11+
#include <NimBLEDevice.h>
12+
#include <NimBLEBeacon.h>
13+
14+
// According to Apple, it's important to have a 100ms advertising time
15+
#define BEACON_ADVERTISING_TIME 160 // 100ms
16+
17+
// Hey, you! Replace this with your own unique UUID with something like https://www.uuidgenerator.net/
18+
const char* iBeaconUUID = "26D0814C-F81C-4B2D-AC57-032E2AFF8642";
19+
20+
void setup() {
21+
NimBLEDevice::init("NimBLEiBeacon");
22+
23+
// Create beacon object
24+
NimBLEBeacon beacon;
25+
beacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
26+
beacon.setMajor(1);
27+
beacon.setMinor(1);
28+
beacon.setSignalPower(0xC5); // Not required
29+
beacon.setProximityUUID(BLEUUID(iBeaconUUID)); // Unlike Bluedroid, you do not need to reverse endianness here
30+
31+
// Extract beacon data
32+
NimBLEBeacon::BeaconData beaconData = beacon.getData();
33+
34+
// Create advertisement data
35+
NimBLEAdvertisementData beaconAdvertisementData;
36+
beaconAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED
37+
beaconAdvertisementData.setManufacturerData(reinterpret_cast<const uint8_t*>(&beaconData), sizeof(NimBLEBeacon::BeaconData));
38+
39+
// Start advertising
40+
NimBLEAdvertising *advertising = NimBLEDevice::getAdvertising();
41+
advertising->setAdvertisingInterval(BEACON_ADVERTISING_TIME);
42+
advertising->setAdvertisementData(beaconAdvertisementData);
43+
advertising->start();
44+
}
45+
46+
void loop() {}

0 commit comments

Comments
 (0)