Skip to content

Commit a743a0c

Browse files
committed
Add BLE examples.
1 parent 624b8ec commit a743a0c

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Example of advertising service data.
3+
*
4+
* This example will start advertising service data for a custom UUID, the data value of which
5+
* will increment by one every 5 seconds.
6+
*
7+
* Note the configuration options in the build_opt.h file.
8+
* More BLE examples can be found in the NimBLE-Arduino examples: https://github.com/h2zero/NimBLE-Arduino.
9+
*
10+
* For further BLE documentation see: https://h2zero.github.io/NimBLE-Arduino
11+
*/
12+
13+
#include <Arduino.h>
14+
#include <NimBLEDevice.h>
15+
16+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
17+
18+
static NimBLEUUID dataUuid(SERVICE_UUID);
19+
static NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
20+
static uint32_t count = 0;
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
Serial.println("Starting Advertising!");
25+
26+
// Initialize the stack
27+
NimBLEDevice::init("NimBLE");
28+
}
29+
30+
void loop() {
31+
if (!pAdvertising->isAdvertising()) {
32+
// Update the advertised data
33+
pAdvertising->setServiceData(dataUuid, std::string((char*)&count, sizeof(count)));
34+
35+
// Start advertising the data
36+
pAdvertising->start(5);
37+
Serial.printf("Advertising count = %d\n", count);
38+
count++;
39+
}
40+
41+
// Short delay to allow the stack to reset states.
42+
delay(100);
43+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'-DCONFIG_MAIN_TASK_STACK_SIZE=512'
2+
'-DCONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE=1024'
3+
'-DCONFIG_BT_NIMBLE_ROLE_CENTRAL_DISABLED'
4+
'-DCONFIG_BT_NIMBLE_ROLE_OBSERVER_DISABLED'
5+
'-DCONFIG_BT_NIMBLE_ROLE_PERIPHERAL_DISABLED'
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Example of continuous scanning for BLE advertisements.
3+
* This example will scan for scanTime (seconds) and report all advertisements on the serial monitor.
4+
*
5+
* Note the configuration options in the build_opt.h file.
6+
* More BLE examples can be found in the NimBLE-Arduino examples: https://github.com/h2zero/NimBLE-Arduino.
7+
*
8+
* For further BLE documentation see: https://h2zero.github.io/NimBLE-Arduino
9+
*/
10+
11+
#include <Arduino.h>
12+
#include "NimBLEDevice.h"
13+
14+
NimBLEScan* pBLEScan;
15+
uint32_t scanTime = 30; // Scan duration in seconds (0 = forever)
16+
17+
// Callback class for received advertisements
18+
class MyAdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
19+
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
20+
Serial.printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
21+
}
22+
};
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
Serial.println("Scanning...");
27+
28+
// Initialize the BLE stack
29+
NimBLEDevice::init("");
30+
31+
// Create new scan instance
32+
pBLEScan = NimBLEDevice::getScan();
33+
34+
// Set the callback for when devices are discovered, no duplicates.
35+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false);
36+
37+
// Set active scanning, this will get scan response data from the advertiser.
38+
pBLEScan->setActiveScan(true);
39+
40+
// Set how often the scan occurs/switches channels; in milliseconds,
41+
pBLEScan->setInterval(97);
42+
43+
// How long to scan during the interval; in milliseconds.
44+
pBLEScan->setWindow(37);
45+
46+
// Do not store the scan results, use callback only.
47+
pBLEScan->setMaxResults(0);
48+
}
49+
50+
void loop() {
51+
// When the scan stops, restart it. This will cause duplicate devices to be reported again.
52+
if(pBLEScan->isScanning() == false) {
53+
// Start scan with: duration = scanTime (seconds), no scan ended callback, not a continuation of a previous scan.
54+
pBLEScan->start(scanTime, nullptr, false);
55+
}
56+
57+
// Short delay to allow the stack to reset states.
58+
delay(100);
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'-DCONFIG_MAIN_TASK_STACK_SIZE=512'
2+
'-DCONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE=1024'
3+
'-DCONFIG_BT_NIMBLE_ROLE_PERIPHERAL_DISABLED'
4+
'-DCONFIG_BT_NIMBLE_ROLE_BROADCASTER_DISABLED'

0 commit comments

Comments
 (0)