Skip to content

Commit 2986119

Browse files
committed
Add enhanced advertising examples
While the old way of preparing advertising data is still supported, an alternative enhanced method to build advertising packets is now available. It consists in defining and configuring an object of type 'BLEAdvertisingData' to build the desired packet. When it has been configured, by setting the appropriate parameters, it can be used to populate the advertising data packet or the scan response data packet. Following this way, the user can decide in which packet each parameter should be put. Also, it is now possible to configure an advertising packet (advertising or scan response) by passing a raw data packet. If an advertising packet has a raw data parameter set, all its other parameters will be ignored. Also, advertising parameters such as manufacturer data, service data or raw data should have a GLOBAL scope, because they are passed as pointers and are not copied internally.
1 parent fd5bc06 commit 2986119

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <ArduinoBLE.h>
2+
3+
BLEService myService("fff0");
4+
BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast);
5+
6+
// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
7+
const uint8_t manufactData[4] = {0x01, 0x02, 0x03, 0x04};
8+
const uint8_t serviceData[3] = {0x00, 0x01, 0x02};
9+
10+
void setup() {
11+
Serial.begin(9600);
12+
while (!Serial);
13+
14+
if (!BLE.begin()) {
15+
Serial.println("failed to initialize BLE!");
16+
while (1);
17+
}
18+
19+
myService.addCharacteristic(myCharacteristic);
20+
BLE.addService(myService);
21+
22+
// Build scan response data packet
23+
BLEAdvertisingData scanData;
24+
// Set parameters for scan response packet
25+
scanData.setLocalName("Test enhanced advertising");
26+
// Copy set parameters in the actual scan response packet
27+
BLE.setScanResponseData(scanData);
28+
29+
// Build advertising data packet
30+
BLEAdvertisingData advData;
31+
// Set parameters for advertising packet
32+
advData.setManufacturerData(0x004C, manufactData, sizeof(manufactData));
33+
advData.setAdvertisedService(myService);
34+
advData.setAdvertisedServiceData(0xfff0, serviceData, sizeof(serviceData));
35+
// Copy set parameters in the actual advertising packet
36+
BLE.setAdvertisingData(advData);
37+
38+
BLE.advertise();
39+
Serial.println("advertising ...");
40+
}
41+
42+
void loop() {
43+
BLE.poll();
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <ArduinoBLE.h>
2+
3+
BLEService myService("fff0");
4+
BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast);
5+
6+
// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
7+
const uint8_t completeRawAdvertisingData[] = {0x02,0x01,0x06,0x09,0xff,0x01,0x01,0x00,0x01,0x02,0x03,0x04,0x05};
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
while (!Serial);
12+
13+
if (!BLE.begin()) {
14+
Serial.println("failed to initialize BLE!");
15+
while (1);
16+
}
17+
18+
myService.addCharacteristic(myCharacteristic);
19+
BLE.addService(myService);
20+
21+
// Build advertising data packet
22+
BLEAdvertisingData advData;
23+
// If a packet has a raw data parameter, then all the other parameters of the packet will be ignored
24+
advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));
25+
// Copy set parameters in the actual advertising packet
26+
BLE.setAdvertisingData(advData);
27+
28+
// Build scan response data packet
29+
BLEAdvertisingData scanData;
30+
scanData.setLocalName("Test advertising raw data");
31+
// Copy set parameters in the actual scan response packet
32+
BLE.setScanResponseData(scanData);
33+
34+
BLE.advertise();
35+
36+
Serial.println("advertising ...");
37+
}
38+
39+
void loop() {
40+
BLE.poll();
41+
}

0 commit comments

Comments
 (0)