|
1 |
| -/* |
2 |
| - Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 |
3 |
| - which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp |
4 |
| -*/ |
5 |
| - |
6 |
| -/* |
7 |
| - Create a BLE server that will send periodic iBeacon frames. |
8 |
| - The design of creating the BLE server is: |
9 |
| - 1. Create a BLE Server |
10 |
| - 2. Create advertising data |
11 |
| - 3. Start advertising. |
12 |
| - 4. wait |
13 |
| - 5. Stop advertising. |
14 |
| -*/ |
15 |
| -#include <BLEDevice.h> |
16 |
| -#include <BLEServer.h> |
17 |
| -#include <BLEUtils.h> |
18 |
| -#include <BLE2902.h> |
19 |
| -#include <BLEBeacon.h> |
20 |
| - |
21 |
| -#define DEVICE_NAME "ESP32" |
22 |
| -#define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E" |
23 |
| -#define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1" |
24 |
| -#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D" |
25 |
| -#define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5" |
26 |
| - |
27 |
| -BLEServer* pServer; |
28 |
| -BLECharacteristic* pCharacteristic; |
29 |
| -bool deviceConnected = false; |
30 |
| -uint8_t value = 0; |
31 |
| - |
32 |
| -class MyServerCallbacks : public BLEServerCallbacks { |
33 |
| - void onConnect(BLEServer* pServer) { |
34 |
| - deviceConnected = true; |
35 |
| - Serial.println("deviceConnected = true"); |
36 |
| - }; |
37 |
| - |
38 |
| - void onDisconnect(BLEServer* pServer) { |
39 |
| - deviceConnected = false; |
40 |
| - Serial.println("deviceConnected = false"); |
41 |
| - |
42 |
| - // Restart advertising to be visible and connectable again |
43 |
| - BLEAdvertising* pAdvertising; |
44 |
| - pAdvertising = pServer->getAdvertising(); |
45 |
| - pAdvertising->start(); |
46 |
| - Serial.println("iBeacon advertising restarted"); |
47 |
| - } |
48 |
| -}; |
49 |
| - |
50 |
| -class MyCallbacks : public BLECharacteristicCallbacks { |
51 |
| - void onWrite(BLECharacteristic* pCharacteristic) { |
52 |
| - String rxValue = pCharacteristic->getValue(); |
53 |
| - |
54 |
| - if (rxValue.length() > 0) { |
55 |
| - Serial.println("*********"); |
56 |
| - Serial.print("Received Value: "); |
57 |
| - for (int i = 0; i < rxValue.length(); i++) { |
58 |
| - Serial.print(rxValue[i]); |
59 |
| - } |
60 |
| - Serial.println(); |
61 |
| - Serial.println("*********"); |
62 |
| - } |
63 |
| - } |
64 |
| -}; |
65 |
| - |
66 |
| - |
67 |
| -void init_service() { |
68 |
| - BLEAdvertising* pAdvertising; |
69 |
| - pAdvertising = pServer->getAdvertising(); |
70 |
| - pAdvertising->stop(); |
71 |
| - |
72 |
| - // Create the BLE Service |
73 |
| - BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID)); |
74 |
| - |
75 |
| - // Create a BLE Characteristic |
76 |
| - pCharacteristic = pService->createCharacteristic( |
77 |
| - CHARACTERISTIC_UUID, |
78 |
| - BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY); |
79 |
| - pCharacteristic->setCallbacks(new MyCallbacks()); |
80 |
| - pCharacteristic->addDescriptor(new BLE2902()); |
81 |
| - |
82 |
| - pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); |
83 |
| - |
84 |
| - // Start the service |
85 |
| - pService->start(); |
86 |
| - |
87 |
| - pAdvertising->start(); |
88 |
| -} |
89 |
| - |
90 |
| -void init_beacon() { |
91 |
| - BLEAdvertising* pAdvertising; |
92 |
| - pAdvertising = pServer->getAdvertising(); |
93 |
| - pAdvertising->stop(); |
94 |
| - // iBeacon |
95 |
| - BLEBeacon myBeacon; |
96 |
| - myBeacon.setManufacturerId(0x4c00); |
97 |
| - myBeacon.setMajor(5); |
98 |
| - myBeacon.setMinor(88); |
99 |
| - myBeacon.setSignalPower(0xc5); |
100 |
| - myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV)); |
101 |
| - |
102 |
| - BLEAdvertisementData advertisementData; |
103 |
| - advertisementData.setFlags(0x1A); |
104 |
| - advertisementData.setManufacturerData(myBeacon.getData()); |
105 |
| - pAdvertising->setAdvertisementData(advertisementData); |
106 |
| - |
107 |
| - pAdvertising->start(); |
108 |
| -} |
109 |
| - |
110 |
| -void setup() { |
111 |
| - Serial.begin(115200); |
112 |
| - Serial.println(); |
113 |
| - Serial.println("Initializing..."); |
114 |
| - Serial.flush(); |
115 |
| - |
116 |
| - BLEDevice::init(DEVICE_NAME); |
117 |
| - pServer = BLEDevice::createServer(); |
118 |
| - pServer->setCallbacks(new MyServerCallbacks()); |
119 |
| - |
120 |
| - init_service(); |
121 |
| - init_beacon(); |
122 |
| - |
123 |
| - Serial.println("iBeacon + service defined and advertising!"); |
124 |
| -} |
125 |
| - |
126 |
| -void loop() { |
127 |
| - if (deviceConnected) { |
128 |
| - Serial.printf("*** NOTIFY: %d ***\n", value); |
129 |
| - pCharacteristic->setValue(&value, 1); |
130 |
| - pCharacteristic->notify(); |
131 |
| - value++; |
132 |
| - } |
133 |
| - delay(2000); |
134 |
| -} |
| 1 | +/* |
| 2 | + Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434 |
| 3 | + which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp |
| 4 | +*/ |
| 5 | + |
| 6 | +/* |
| 7 | + Create a BLE server that will send periodic iBeacon frames. |
| 8 | + The design of creating the BLE server is: |
| 9 | + 1. Create a BLE Server |
| 10 | + 2. Create advertising data |
| 11 | + 3. Start advertising. |
| 12 | + 4. wait |
| 13 | + 5. Stop advertising. |
| 14 | +*/ |
| 15 | +#include <BLEDevice.h> |
| 16 | +#include <BLEServer.h> |
| 17 | +#include <BLEUtils.h> |
| 18 | +#include <BLE2902.h> |
| 19 | +#include <BLEBeacon.h> |
| 20 | + |
| 21 | +#define DEVICE_NAME "ESP32" |
| 22 | +#define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E" |
| 23 | +#define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1" |
| 24 | +#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D" |
| 25 | +#define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5" |
| 26 | + |
| 27 | +BLEServer* pServer; |
| 28 | +BLECharacteristic* pCharacteristic; |
| 29 | +bool deviceConnected = false; |
| 30 | +uint8_t value = 0; |
| 31 | + |
| 32 | +class MyServerCallbacks : public BLEServerCallbacks { |
| 33 | + void onConnect(BLEServer* pServer) { |
| 34 | + deviceConnected = true; |
| 35 | + Serial.println("deviceConnected = true"); |
| 36 | + }; |
| 37 | + |
| 38 | + void onDisconnect(BLEServer* pServer) { |
| 39 | + deviceConnected = false; |
| 40 | + Serial.println("deviceConnected = false"); |
| 41 | + |
| 42 | + // Restart advertising to be visible and connectable again |
| 43 | + BLEAdvertising* pAdvertising; |
| 44 | + pAdvertising = pServer->getAdvertising(); |
| 45 | + pAdvertising->start(); |
| 46 | + Serial.println("iBeacon advertising restarted"); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class MyCallbacks : public BLECharacteristicCallbacks { |
| 51 | + void onWrite(BLECharacteristic* pCharacteristic) { |
| 52 | + String rxValue = pCharacteristic->getValue(); |
| 53 | + |
| 54 | + if (rxValue.length() > 0) { |
| 55 | + Serial.println("*********"); |
| 56 | + Serial.print("Received Value: "); |
| 57 | + for (int i = 0; i < rxValue.length(); i++) { |
| 58 | + Serial.print(rxValue[i]); |
| 59 | + } |
| 60 | + Serial.println(); |
| 61 | + Serial.println("*********"); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +void init_service() { |
| 68 | + BLEAdvertising* pAdvertising; |
| 69 | + pAdvertising = pServer->getAdvertising(); |
| 70 | + pAdvertising->stop(); |
| 71 | + |
| 72 | + // Create the BLE Service |
| 73 | + BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID)); |
| 74 | + |
| 75 | + // Create a BLE Characteristic |
| 76 | + pCharacteristic = pService->createCharacteristic( |
| 77 | + CHARACTERISTIC_UUID, |
| 78 | + BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY); |
| 79 | + pCharacteristic->setCallbacks(new MyCallbacks()); |
| 80 | + pCharacteristic->addDescriptor(new BLE2902()); |
| 81 | + |
| 82 | + pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); |
| 83 | + |
| 84 | + // Start the service |
| 85 | + pService->start(); |
| 86 | + |
| 87 | + pAdvertising->start(); |
| 88 | +} |
| 89 | + |
| 90 | +void init_beacon() { |
| 91 | + BLEAdvertising* pAdvertising; |
| 92 | + pAdvertising = pServer->getAdvertising(); |
| 93 | + pAdvertising->stop(); |
| 94 | + // iBeacon |
| 95 | + BLEBeacon myBeacon; |
| 96 | + myBeacon.setManufacturerId(0x4c00); |
| 97 | + myBeacon.setMajor(5); |
| 98 | + myBeacon.setMinor(88); |
| 99 | + myBeacon.setSignalPower(0xc5); |
| 100 | + myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV)); |
| 101 | + |
| 102 | + BLEAdvertisementData advertisementData; |
| 103 | + advertisementData.setFlags(0x1A); |
| 104 | + advertisementData.setManufacturerData(myBeacon.getData()); |
| 105 | + pAdvertising->setAdvertisementData(advertisementData); |
| 106 | + |
| 107 | + pAdvertising->start(); |
| 108 | +} |
| 109 | + |
| 110 | +void setup() { |
| 111 | + Serial.begin(115200); |
| 112 | + Serial.println(); |
| 113 | + Serial.println("Initializing..."); |
| 114 | + Serial.flush(); |
| 115 | + |
| 116 | + BLEDevice::init(DEVICE_NAME); |
| 117 | + pServer = BLEDevice::createServer(); |
| 118 | + pServer->setCallbacks(new MyServerCallbacks()); |
| 119 | + |
| 120 | + init_service(); |
| 121 | + init_beacon(); |
| 122 | + |
| 123 | + Serial.println("iBeacon + service defined and advertising!"); |
| 124 | +} |
| 125 | + |
| 126 | +void loop() { |
| 127 | + if (deviceConnected) { |
| 128 | + Serial.printf("*** NOTIFY: %d ***\n", value); |
| 129 | + pCharacteristic->setValue(&value, 1); |
| 130 | + pCharacteristic->notify(); |
| 131 | + value++; |
| 132 | + } |
| 133 | + delay(2000); |
| 134 | +} |
0 commit comments