@@ -37,9 +37,7 @@ For this example we will keep it simple and use a 16 bit value: ABCD.
3737```
3838#include "NimBLEDevice.h"
3939
40- // void setup() in Arduino
41- void app_main(void)
42- {
40+ extern "C" void app_main(void) {
4341 NimBLEDevice::init("NimBLE");
4442
4543 NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -79,9 +77,7 @@ The function call will simply be `pService->createCharacteristic("1234");`
7977```
8078#include "NimBLEDevice.h"
8179
82- // void setup() in Arduino
83- void app_main(void)
84- {
80+ extern "C" void app_main(void) {
8581 NimBLEDevice::init("NimBLE");
8682
8783 NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -99,12 +95,13 @@ There are many different types you can send as parameters for the value but for
9995` pCharacteristic->setValue("Hello BLE"); `
10096
10197Next we need to advertise for connections.
102- To do this we create an instance of ` NimBLEAdvertising ` add our service to it (optional) and start advertisng .
98+ To do this we create an instance of ` NimBLEAdvertising ` add our service to it (optional) and start advertising .
10399
104100** The code for this will be:**
105101```
106102NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); // create advertising instance
107- pAdvertising->addServiceUUID("ABCD"); // tell advertising the UUID of our service
103+ pAdvertising->addServiceUUID("ABCD"); // advertise the UUID of our service
104+ pAdvertising->setName("NimBLE"); // advertise the device name
108105pAdvertising->start(); // start advertising
109106```
110107That's it, this will be enough to create a BLE server with a service and a characteristic and advertise for client connections.
@@ -113,9 +110,7 @@ That's it, this will be enough to create a BLE server with a service and a chara
113110```
114111#include "NimBLEDevice.h"
115112
116- // void setup() in Arduino
117- void app_main(void)
118- {
113+ extern "C" void app_main(void) {
119114 NimBLEDevice::init("NimBLE");
120115
121116 NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -126,7 +121,8 @@ void app_main(void)
126121 pCharacteristic->setValue("Hello BLE");
127122
128123 NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
129- pAdvertising->addServiceUUID("ABCD");
124+ pAdvertising->addServiceUUID("ABCD"); // advertise the UUID of our service
125+ pAdvertising->setName("NimBLE"); // advertise the device name
130126 pAdvertising->start();
131127}
132128```
@@ -144,7 +140,7 @@ After initializing the NimBLE stack we create a scan instance by calling `NimBLE
144140
145141Once we have created the scan we can start looking for advertising servers.
146142
147- To do this we call ` NimBLEScan::start (duration) ` , the duration parameter is a uint32_t that specifies the number of milliseconds to scan for,
143+ To do this we call ` NimBLEScan::getResults (duration) ` , the duration parameter is a uint32_t that specifies the number of milliseconds to scan for,
148144passing 0 will scan forever.
149145
150146In this example we will scan for 10 seconds. This is a blocking function (a non blocking overload is also available).
@@ -154,9 +150,7 @@ This call returns an instance of `NimBLEScanResults` when the scan completes whi
154150```
155151#include "NimBLEDevice.h"
156152
157- // void setup() in Arduino
158- void app_main(void)
159- {
153+ extern "C" void app_main(void) {
160154 NimBLEDevice::init("");
161155
162156 NimBLEScan *pScan = NimBLEDevice::getScan();
@@ -168,7 +162,7 @@ void app_main(void)
168162Now that we have scanned we need to check the results for any advertisers we are interested in connecting to.
169163
170164To do this we iterate through the results and check if any of the devices found are advertising the service we want ` ABCD ` .
171- Each result in ` NimBLEScanResults ` is a ` NimBLEAdvertisedDevice ` instance that we can access data from.
165+ Each result in ` NimBLEScanResults ` is a ` const NimBLEAdvertisedDevice* ` that we can access data from.
172166
173167We will check each device found for the ` ABCD ` service by calling ` NimBLEAdvertisedDevice::isAdvertisingService ` .
174168This takes an instance of ` NimBLEUUID ` as a parameter so we will need to create one.
@@ -177,11 +171,11 @@ This takes an instance of `NimBLEUUID` as a parameter so we will need to create
177171```
178172NimBLEUUID serviceUuid("ABCD");
179173
180- for(int i = 0; i < results.getCount(); i++) {
181- NimBLEAdvertisedDevice device = results.getDevice(i);
174+ for (int i = 0; i < results.getCount(); i++) {
175+ const NimBLEAdvertisedDevice * device = results.getDevice(i);
182176
183- if (device. isAdvertisingService(serviceUuid)) {
184- // create a client and connect
177+ if (device-> isAdvertisingService(serviceUuid)) {
178+ // create a client and connect
185179 }
186180}
187181```
@@ -198,16 +192,16 @@ This takes a pointer to the `NimBLEAdvertisedDevice` and returns `true` if succe
198192```
199193NimBLEUUID serviceUuid("ABCD");
200194
201- for(int i = 0; i < results.getCount(); i++) {
202- NimBLEAdvertisedDevice device = results.getDevice(i);
195+ for (int i = 0; i < results.getCount(); i++) {
196+ const NimBLEAdvertisedDevice * device = results.getDevice(i);
203197
204- if (device. isAdvertisingService(serviceUuid)) {
198+ if (device-> isAdvertisingService(serviceUuid)) {
205199 NimBLEClient *pClient = NimBLEDevice::createClient();
206200
207- if(pClient->connect(&device)) {
208- //success
201+ if (pClient->connect(&device)) {
202+ //success
209203 } else {
210- // failed to connect
204+ // failed to connect
211205 }
212206 }
213207}
@@ -229,11 +223,15 @@ Finally we will read the characteristic value with `NimBLERemoteCharacteristic::
229223```
230224NimBLEUUID serviceUuid("ABCD");
231225
232- for(int i = 0; i < results.getCount(); i++) {
233- NimBLEAdvertisedDevice device = results.getDevice(i);
226+ for (int i = 0; i < results.getCount(); i++) {
227+ const NimBLEAdvertisedDevice * device = results.getDevice(i);
234228
235- if (device. isAdvertisingService(serviceUuid)) {
229+ if (device-> isAdvertisingService(serviceUuid)) {
236230 NimBLEClient *pClient = NimBLEDevice::createClient();
231+
232+ if (!pClient) { // Make sure the client was created
233+ break;
234+ }
237235
238236 if (pClient->connect(&device)) {
239237 NimBLERemoteService *pService = pClient->getService(serviceUuid);
@@ -247,7 +245,7 @@ for(int i = 0; i < results.getCount(); i++) {
247245 }
248246 }
249247 } else {
250- // failed to connect
248+ // failed to connect
251249 }
252250 }
253251}
@@ -262,12 +260,16 @@ This is done by calling `NimBLEDevice::deleteClient`.
262260```
263261NimBLEUUID serviceUuid("ABCD");
264262
265- for(int i = 0; i < results.getCount(); i++) {
266- NimBLEAdvertisedDevice device = results.getDevice(i);
263+ for (int i = 0; i < results.getCount(); i++) {
264+ const NimBLEAdvertisedDevice * device = results.getDevice(i);
267265
268- if (device. isAdvertisingService(serviceUuid)) {
266+ if (device-> isAdvertisingService(serviceUuid)) {
269267 NimBLEClient *pClient = NimBLEDevice::createClient();
270-
268+
269+ if (!pClient) { // Make sure the client was created
270+ break;
271+ }
272+
271273 if (pClient->connect(&device)) {
272274 NimBLERemoteService *pService = pClient->getService(serviceUuid);
273275
@@ -280,7 +282,7 @@ for(int i = 0; i < results.getCount(); i++) {
280282 }
281283 }
282284 } else {
283- // failed to connect
285+ // failed to connect
284286 }
285287
286288 NimBLEDevice::deleteClient(pClient);
@@ -294,37 +296,39 @@ Note that there is no need to disconnect as that will be done when deleting the
294296```
295297#include "NimBLEDevice.h"
296298
297- // void setup() in Arduino
298- void app_main(void)
299- {
299+ extern "C" void app_main(void) {
300300 NimBLEDevice::init("");
301-
301+
302302 NimBLEScan *pScan = NimBLEDevice::getScan();
303- NimBLEScanResults results = pScan->start (10 * 1000);
304-
303+ NimBLEScanResults results = pScan->getResults (10 * 1000);
304+
305305 NimBLEUUID serviceUuid("ABCD");
306-
307- for(int i = 0; i < results.getCount(); i++) {
308- NimBLEAdvertisedDevice device = results.getDevice(i);
309-
310- if (device. isAdvertisingService(serviceUuid)) {
306+
307+ for (int i = 0; i < results.getCount(); i++) {
308+ const NimBLEAdvertisedDevice * device = results.getDevice(i);
309+
310+ if (device-> isAdvertisingService(serviceUuid)) {
311311 NimBLEClient *pClient = NimBLEDevice::createClient();
312-
312+
313+ if (!pClient) { // Make sure the client was created
314+ break;
315+ }
316+
313317 if (pClient->connect(&device)) {
314318 NimBLERemoteService *pService = pClient->getService(serviceUuid);
315-
319+
316320 if (pService != nullptr) {
317321 NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("1234");
318-
322+
319323 if (pCharacteristic != nullptr) {
320324 std::string value = pCharacteristic->readValue();
321325 // print or do whatever you need with the value
322326 }
323327 }
324328 } else {
325- // failed to connect
329+ // failed to connect
326330 }
327-
331+
328332 NimBLEDevice::deleteClient(pClient);
329333 }
330334 }
@@ -334,4 +338,3 @@ void app_main(void)
334338
335339For more advanced features and options please see the client examples in the examples folder.
336340<br />
337-
0 commit comments