Skip to content

Commit ab840a9

Browse files
committed
chore(examples): enhance scan management
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 38b5c5f commit ab840a9

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

examples/Central/LedControl/LedControl.ino

+30-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ void setup() {
4242
Serial.println("Bluetooth® Low Energy Central - LED control");
4343

4444
// start scanning for peripherals
45-
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
45+
int ret = 1;
46+
do
47+
{
48+
ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
49+
if (ret == 0)
50+
{
51+
BLE.end();
52+
BLE.begin();
53+
}
54+
} while(ret == 0);
4655
}
4756

4857
void loop() {
@@ -64,12 +73,30 @@ void loop() {
6473
}
6574

6675
// stop scanning
67-
BLE.stopScan();
76+
int ret = 1;
77+
do
78+
{
79+
ret = BLE.stopScan();
80+
if (ret == 0)
81+
{
82+
BLE.end();
83+
BLE.begin();
84+
}
85+
} while(ret == 0);
6886

6987
controlLed(peripheral);
7088

7189
// peripheral disconnected, start scanning again
72-
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
90+
ret = 1;
91+
do
92+
{
93+
ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
94+
if (ret == 0)
95+
{
96+
BLE.end();
97+
BLE.begin();
98+
}
99+
} while(ret == 0);
73100
}
74101
}
75102

examples/Central/PeripheralExplorer/PeripheralExplorer.ino

+20-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ void setup() {
2929
Serial.println("Bluetooth® Low Energy Central - Peripheral Explorer");
3030

3131
// start scanning for peripherals
32-
BLE.scan();
32+
int ret = 1;
33+
do
34+
{
35+
ret = BLE.scan();
36+
if (ret == 0)
37+
{
38+
BLE.end();
39+
BLE.begin();
40+
}
41+
} while(ret == 0);
3342
}
3443

3544
void loop() {
@@ -49,7 +58,16 @@ void loop() {
4958
// see if peripheral is a LED
5059
if (peripheral.localName() == "LED") {
5160
// stop scanning
52-
BLE.stopScan();
61+
int ret = 1;
62+
do
63+
{
64+
ret = BLE.stopScan();
65+
if (ret == 0)
66+
{
67+
BLE.end();
68+
BLE.begin();
69+
}
70+
} while(ret == 0);
5371

5472
explorerPeripheral(peripheral);
5573

examples/Central/Scan/Scan.ino

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ void setup() {
2525

2626
Serial.println("Bluetooth® Low Energy Central scan");
2727

28-
// start scanning for peripheral
29-
BLE.scan();
28+
// start scanning for peripherals
29+
int ret = 1;
30+
do
31+
{
32+
ret = BLE.scan();
33+
if (ret == 0)
34+
{
35+
BLE.end();
36+
BLE.begin();
37+
}
38+
} while(ret == 0);
3039
}
3140

3241
void loop() {

examples/Central/ScanCallback/ScanCallback.ino

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ void setup() {
3131
BLE.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler);
3232

3333
// start scanning for peripherals with duplicates
34-
BLE.scan(true);
34+
int ret = 1;
35+
do
36+
{
37+
ret = BLE.scan(true);
38+
if (ret == 0)
39+
{
40+
BLE.end();
41+
BLE.begin();
42+
}
43+
} while(ret == 0);
3544
}
3645

3746
void loop() {

examples/Central/SensorTagButton/SensorTagButton.ino

+31-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ void setup() {
3030
Serial.println("Bluetooth® Low Energy Central - SensorTag button");
3131
Serial.println("Make sure to turn on the device.");
3232

33-
// start scanning for peripheral
34-
BLE.scan();
33+
// start scanning for peripherals
34+
int ret = 1;
35+
do
36+
{
37+
ret = BLE.scan();
38+
if (ret == 0)
39+
{
40+
BLE.end();
41+
BLE.begin();
42+
}
43+
} while(ret == 0);
3544
}
3645

3746
void loop() {
@@ -52,12 +61,30 @@ void loop() {
5261
// "CC2650 SensorTag"
5362
if (peripheral.localName() == "CC2650 SensorTag") {
5463
// stop scanning
55-
BLE.stopScan();
64+
int ret = 1;
65+
do
66+
{
67+
ret = BLE.stopScan();
68+
if (ret == 0)
69+
{
70+
BLE.end();
71+
BLE.begin();
72+
}
73+
} while(ret == 0);
5674

5775
monitorSensorTagButtons(peripheral);
5876

5977
// peripheral disconnected, start scanning again
60-
BLE.scan();
78+
ret = 1;
79+
do
80+
{
81+
ret = BLE.scan();
82+
if (ret == 0)
83+
{
84+
BLE.end();
85+
BLE.begin();
86+
}
87+
} while(ret == 0);
6188
}
6289
}
6390
}

0 commit comments

Comments
 (0)