Skip to content

Commit 19baf30

Browse files
committed
Update examples and change code based on code review
1. Update example sketches; 2. Check the NULL pointers; 3. Add the writeValue to the template; 4. Delete reference in callback parameters;
1 parent c8c36a3 commit 19baf30

File tree

17 files changed

+767
-1
lines changed

17 files changed

+767
-1
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Arduino BLE Central LED Control example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
21+
#include <ArduinoBLE.h>
22+
23+
// variables for button
24+
const int buttonPin = 2;
25+
int oldButtonState = LOW;
26+
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
31+
// configure the button pin as input
32+
pinMode(buttonPin, INPUT);
33+
34+
// initialize the BLE hardware
35+
BLE.begin();
36+
37+
Serial.println("BLE Central - LED control");
38+
39+
// start scanning for peripherals
40+
BLE.startScanning();
41+
}
42+
43+
void loop() {
44+
// check if a peripheral has been discovered
45+
BLEDevice peripheral = BLE.available();
46+
47+
if (peripheral) {
48+
// discovered a peripheral, print out address, local name, and advertised service
49+
Serial.print("Found ");
50+
Serial.print(peripheral.address());
51+
Serial.print(" '");
52+
Serial.print(peripheral.localName());
53+
Serial.print("' ");
54+
Serial.print(peripheral.advertisedServiceUuid());
55+
Serial.println();
56+
57+
// see if peripheral is advertising the LED service
58+
if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") {
59+
// stop scanning
60+
BLE.stopScanning();
61+
62+
controlLed(peripheral);
63+
64+
// peripheral disconnected, start scanning again
65+
BLE.startScanning();
66+
}
67+
}
68+
}
69+
70+
void controlLed(BLEDevice peripheral) {
71+
// connect to the peripheral
72+
Serial.println("Connecting ...");
73+
74+
if (peripheral.connect()) {
75+
Serial.println("Connected");
76+
} else {
77+
Serial.println("Failed to connect!");
78+
return;
79+
}
80+
81+
// discover peripheral attributes
82+
Serial.println("Discovering attributes ...");
83+
if (peripheral.discoverAttributes()) {
84+
Serial.println("Attributes discovered");
85+
} else {
86+
Serial.println("Attribute discovery failed!");
87+
peripheral.disconnect();
88+
return;
89+
}
90+
91+
// retrieve the LED characteristic
92+
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
93+
94+
if (!ledCharacteristic) {
95+
Serial.println("Peripheral does not have LED characteristic!");
96+
peripheral.disconnect();
97+
return;
98+
} else if (!ledCharacteristic.canWrite()) {
99+
Serial.println("Peripheral does not have a writable LED characteristic!");
100+
peripheral.disconnect();
101+
return;
102+
}
103+
104+
while (peripheral.connected()) {
105+
// while the peripheral is connection
106+
107+
// read the button pin
108+
int buttonState = digitalRead(buttonPin);
109+
110+
if (oldButtonState != buttonState) {
111+
// button changed
112+
oldButtonState = buttonState;
113+
114+
if (buttonState) {
115+
Serial.println("button pressed");
116+
117+
// button is pressed, write 0x01 to turn the LED on
118+
ledCharacteristic.writeByte(0x01);
119+
} else {
120+
Serial.println("button released");
121+
122+
// button is released, write 0x00 to turn the LED of
123+
ledCharacteristic.writeByte(0x00);
124+
}
125+
}
126+
}
127+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
Arduino BLE Central peripheral explorer example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <ArduinoBLE.h>
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
25+
// initialize the BLE hardware
26+
BLE.begin();
27+
28+
Serial.println("BLE Central - Peripheral Explorer");
29+
30+
// start scanning for peripherals
31+
BLE.startScanning();
32+
}
33+
34+
void loop() {
35+
// check if a peripheral has been discovered
36+
BLEDevice peripheral = BLE.available();
37+
38+
if (peripheral) {
39+
// discovered a peripheral, print out address, local name, and advertised service
40+
Serial.print("Found ");
41+
Serial.print(peripheral.address());
42+
Serial.print(" '");
43+
Serial.print(peripheral.localName());
44+
Serial.print("' ");
45+
Serial.print(peripheral.advertisedServiceUuid());
46+
Serial.println();
47+
48+
// see if peripheral is a SensorTag
49+
if (peripheral.localName() == "SensorTag") {
50+
// stop scanning
51+
BLE.stopScanning();
52+
53+
explorerPeripheral(peripheral);
54+
55+
// peripheral disconnected, we are done
56+
while (1) {
57+
// do nothing
58+
}
59+
}
60+
}
61+
}
62+
63+
void explorerPeripheral(BLEDevice peripheral) {
64+
// connect to the peripheral
65+
Serial.println("Connecting ...");
66+
67+
if (peripheral.connect()) {
68+
Serial.println("Connected");
69+
} else {
70+
Serial.println("Failed to connect!");
71+
return;
72+
}
73+
74+
// discover peripheral attributes
75+
Serial.println("Discovering attributes ...");
76+
if (peripheral.discoverAttributes()) {
77+
Serial.println("Attributes discovered");
78+
} else {
79+
Serial.println("Attribute discovery failed!");
80+
peripheral.disconnect();
81+
return;
82+
}
83+
84+
// read and print device name of peripheral
85+
Serial.println();
86+
Serial.print("Device name: ");
87+
Serial.println(peripheral.deviceName());
88+
89+
// read and print appearance of peripheral
90+
Serial.print("Appearance: ");
91+
Serial.println(peripheral.appearance());
92+
Serial.println();
93+
94+
// loop the services of the peripheral and explore each
95+
for (int i = 0; i < peripheral.serviceCount(); i++) {
96+
BLEService service = peripheral.service(i);
97+
98+
exploreService(service);
99+
}
100+
101+
Serial.println();
102+
103+
// we are done exploring, disconnect
104+
Serial.println("Disconnecting ...");
105+
peripheral.disconnect();
106+
Serial.println("Disconnected");
107+
}
108+
109+
void exploreService(BLEService service) {
110+
// print the UUID of the service
111+
Serial.print("Service ");
112+
Serial.println(service.uuid());
113+
114+
// loop the characteristics of the service and explore each
115+
for (int i = 0; i < service.characteristicCount(); i++) {
116+
BLECharacteristic characteristic = service.characteristic(i);
117+
118+
exploreCharacteristic(characteristic);
119+
}
120+
}
121+
122+
void exploreCharacteristic(BLECharacteristic characteristic) {
123+
// print the UUID and properies of the characteristic
124+
Serial.print("\tCharacteristic ");
125+
Serial.print(characteristic.uuid());
126+
Serial.print(", properties 0x");
127+
Serial.print(characteristic.properties());
128+
129+
// check if the characteristic is readable
130+
if (characteristic.canRead()) {
131+
// read the characteristic value
132+
characteristic.read();
133+
134+
// print out the value of the characteristic
135+
Serial.print(", value 0x");
136+
printData(characteristic.value(), characteristic.valueLength());
137+
}
138+
139+
Serial.println();
140+
141+
// loop the descriptors of the characteristic and explore each
142+
for (int i = 0; i < characteristic.descriptorCount(); i++) {
143+
BLEDescriptor descriptor = characteristic.descriptor(i);
144+
145+
exploreDescriptor(descriptor);
146+
}
147+
}
148+
149+
void exploreDescriptor(BLEDescriptor descriptor) {
150+
// print the UUID of the descriptor
151+
Serial.print("\t\tDescriptor ");
152+
Serial.print(descriptor.uuid());
153+
154+
// read the descriptor value
155+
descriptor.read();
156+
157+
// print out the value of the descriptor
158+
Serial.print(", value 0x");
159+
printData(descriptor.value(), descriptor.valueLength());
160+
161+
Serial.println();
162+
}
163+
164+
void printData(const unsigned char data[], int length) {
165+
for (int i = 0; i < length; i++) {
166+
unsigned char b = data[i];
167+
168+
if (b < 16) {
169+
Serial.print("0");
170+
}
171+
172+
Serial.print(b, HEX);
173+
}
174+
}
175+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Arduino BLE Central scan example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <ArduinoBLE.h>
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
25+
// initialize the BLE hardware
26+
BLE.begin();
27+
28+
Serial.println("BLE Central scan");
29+
30+
// start scanning for peripheral
31+
BLE.startScanning();
32+
}
33+
34+
void loop() {
35+
// check if a peripheral has been discovered
36+
BLEDevice peripheral = BLE.available();
37+
38+
if (peripheral) {
39+
// discovered a peripheral
40+
Serial.println("Discovered a peripheral");
41+
Serial.println("-----------------------");
42+
43+
// print address
44+
Serial.print("Address: ");
45+
Serial.println(peripheral.address());
46+
47+
// print the local name, if present
48+
if (peripheral.hasLocalName()) {
49+
Serial.print("Local Name: ");
50+
Serial.println(peripheral.localName());
51+
}
52+
53+
// print the advertised service UUID's, if present
54+
if (peripheral.hasAdvertisedServiceUuid()) {
55+
Serial.print("Service UUID's: ");
56+
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
57+
Serial.print(peripheral.advertisedServiceUuid(i));
58+
Serial.print(" ");
59+
}
60+
Serial.println();
61+
}
62+
63+
// print the RSSI
64+
Serial.print("RSSI: ");
65+
Serial.println(peripheral.rssi());
66+
67+
Serial.println();
68+
}
69+
}
70+

0 commit comments

Comments
 (0)