@@ -37,9 +37,7 @@ For this example we will keep it simple and use a 16 bit value: ABCD.
37
37
```
38
38
#include "NimBLEDevice.h"
39
39
40
- // void setup() in Arduino
41
- void app_main(void)
42
- {
40
+ extern "C" void app_main(void) {
43
41
NimBLEDevice::init("NimBLE");
44
42
45
43
NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -79,9 +77,7 @@ The function call will simply be `pService->createCharacteristic("1234");`
79
77
```
80
78
#include "NimBLEDevice.h"
81
79
82
- // void setup() in Arduino
83
- void app_main(void)
84
- {
80
+ extern "C" void app_main(void) {
85
81
NimBLEDevice::init("NimBLE");
86
82
87
83
NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -99,12 +95,13 @@ There are many different types you can send as parameters for the value but for
99
95
` pCharacteristic->setValue("Hello BLE"); `
100
96
101
97
Next 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 .
103
99
104
100
** The code for this will be:**
105
101
```
106
102
NimBLEAdvertising *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
108
105
pAdvertising->start(); // start advertising
109
106
```
110
107
That'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
113
110
```
114
111
#include "NimBLEDevice.h"
115
112
116
- // void setup() in Arduino
117
- void app_main(void)
118
- {
113
+ extern "C" void app_main(void) {
119
114
NimBLEDevice::init("NimBLE");
120
115
121
116
NimBLEServer *pServer = NimBLEDevice::createServer();
@@ -126,7 +121,8 @@ void app_main(void)
126
121
pCharacteristic->setValue("Hello BLE");
127
122
128
123
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
130
126
pAdvertising->start();
131
127
}
132
128
```
@@ -144,7 +140,7 @@ After initializing the NimBLE stack we create a scan instance by calling `NimBLE
144
140
145
141
Once we have created the scan we can start looking for advertising servers.
146
142
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,
148
144
passing 0 will scan forever.
149
145
150
146
In 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
154
150
```
155
151
#include "NimBLEDevice.h"
156
152
157
- // void setup() in Arduino
158
- void app_main(void)
159
- {
153
+ extern "C" void app_main(void) {
160
154
NimBLEDevice::init("");
161
155
162
156
NimBLEScan *pScan = NimBLEDevice::getScan();
@@ -168,7 +162,7 @@ void app_main(void)
168
162
Now that we have scanned we need to check the results for any advertisers we are interested in connecting to.
169
163
170
164
To 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.
172
166
173
167
We will check each device found for the ` ABCD ` service by calling ` NimBLEAdvertisedDevice::isAdvertisingService ` .
174
168
This 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
177
171
```
178
172
NimBLEUUID serviceUuid("ABCD");
179
173
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);
182
176
183
- if (device. isAdvertisingService(serviceUuid)) {
184
- // create a client and connect
177
+ if (device-> isAdvertisingService(serviceUuid)) {
178
+ // create a client and connect
185
179
}
186
180
}
187
181
```
@@ -198,16 +192,16 @@ This takes a pointer to the `NimBLEAdvertisedDevice` and returns `true` if succe
198
192
```
199
193
NimBLEUUID serviceUuid("ABCD");
200
194
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);
203
197
204
- if (device. isAdvertisingService(serviceUuid)) {
198
+ if (device-> isAdvertisingService(serviceUuid)) {
205
199
NimBLEClient *pClient = NimBLEDevice::createClient();
206
200
207
- if(pClient->connect(&device)) {
208
- //success
201
+ if (pClient->connect(&device)) {
202
+ //success
209
203
} else {
210
- // failed to connect
204
+ // failed to connect
211
205
}
212
206
}
213
207
}
@@ -229,11 +223,15 @@ Finally we will read the characteristic value with `NimBLERemoteCharacteristic::
229
223
```
230
224
NimBLEUUID serviceUuid("ABCD");
231
225
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);
234
228
235
- if (device. isAdvertisingService(serviceUuid)) {
229
+ if (device-> isAdvertisingService(serviceUuid)) {
236
230
NimBLEClient *pClient = NimBLEDevice::createClient();
231
+
232
+ if (!pClient) { // Make sure the client was created
233
+ break;
234
+ }
237
235
238
236
if (pClient->connect(&device)) {
239
237
NimBLERemoteService *pService = pClient->getService(serviceUuid);
@@ -247,7 +245,7 @@ for(int i = 0; i < results.getCount(); i++) {
247
245
}
248
246
}
249
247
} else {
250
- // failed to connect
248
+ // failed to connect
251
249
}
252
250
}
253
251
}
@@ -262,12 +260,16 @@ This is done by calling `NimBLEDevice::deleteClient`.
262
260
```
263
261
NimBLEUUID serviceUuid("ABCD");
264
262
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);
267
265
268
- if (device. isAdvertisingService(serviceUuid)) {
266
+ if (device-> isAdvertisingService(serviceUuid)) {
269
267
NimBLEClient *pClient = NimBLEDevice::createClient();
270
-
268
+
269
+ if (!pClient) { // Make sure the client was created
270
+ break;
271
+ }
272
+
271
273
if (pClient->connect(&device)) {
272
274
NimBLERemoteService *pService = pClient->getService(serviceUuid);
273
275
@@ -280,7 +282,7 @@ for(int i = 0; i < results.getCount(); i++) {
280
282
}
281
283
}
282
284
} else {
283
- // failed to connect
285
+ // failed to connect
284
286
}
285
287
286
288
NimBLEDevice::deleteClient(pClient);
@@ -294,37 +296,39 @@ Note that there is no need to disconnect as that will be done when deleting the
294
296
```
295
297
#include "NimBLEDevice.h"
296
298
297
- // void setup() in Arduino
298
- void app_main(void)
299
- {
299
+ extern "C" void app_main(void) {
300
300
NimBLEDevice::init("");
301
-
301
+
302
302
NimBLEScan *pScan = NimBLEDevice::getScan();
303
- NimBLEScanResults results = pScan->start (10 * 1000);
304
-
303
+ NimBLEScanResults results = pScan->getResults (10 * 1000);
304
+
305
305
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)) {
311
311
NimBLEClient *pClient = NimBLEDevice::createClient();
312
-
312
+
313
+ if (!pClient) { // Make sure the client was created
314
+ break;
315
+ }
316
+
313
317
if (pClient->connect(&device)) {
314
318
NimBLERemoteService *pService = pClient->getService(serviceUuid);
315
-
319
+
316
320
if (pService != nullptr) {
317
321
NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("1234");
318
-
322
+
319
323
if (pCharacteristic != nullptr) {
320
324
std::string value = pCharacteristic->readValue();
321
325
// print or do whatever you need with the value
322
326
}
323
327
}
324
328
} else {
325
- // failed to connect
329
+ // failed to connect
326
330
}
327
-
331
+
328
332
NimBLEDevice::deleteClient(pClient);
329
333
}
330
334
}
@@ -334,4 +338,3 @@ void app_main(void)
334
338
335
339
For more advanced features and options please see the client examples in the examples folder.
336
340
<br />
337
-
0 commit comments