Skip to content

Commit 9e15c78

Browse files
committed
Add BLE Central support
1 parent 6769d47 commit 9e15c78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4012
-199
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Enables BLE connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev.2, Arduino Nano 33 IoT, and Arduino Nano 33 BLE.
66

7-
This library currently supports creating a BLE peripheral.
7+
This library supports creating a BLE peripheral and BLE central mode.
88

99
For the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev.2, and Arduino Nano 33 IoT boards, it requires the NINA module to be running [Arduino NINA-W102 firmware](https://github.com/arduino/nina-fw) v1.2.0 or later.
1010

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
LED Control
3+
4+
This example scans for BLE peripherals until one with the advertised service
5+
"19b10000-e8f2-537e-4f6c-d104768a1214" UUID is found. Once discovered and connected,
6+
it will remotely control the BLE Peripheral's LED, when the button is pressed or released.
7+
8+
The circuit:
9+
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
10+
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
11+
- Button with pull-up resistor connected to pin 2.
12+
13+
You can use it with another board that is compatible with this library and the
14+
Peripherals -> LED example.
15+
16+
This example code is in the public domain.
17+
*/
18+
19+
#include <ArduinoBLE.h>
20+
21+
// variables for button
22+
const int buttonPin = 2;
23+
int oldButtonState = LOW;
24+
25+
void setup() {
26+
Serial.begin(9600);
27+
while (!Serial);
28+
29+
// configure the button pin as input
30+
pinMode(buttonPin, INPUT);
31+
32+
// initialize the BLE hardware
33+
BLE.begin();
34+
35+
Serial.println("BLE Central - LED control");
36+
37+
// start scanning for peripherals
38+
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
39+
}
40+
41+
void loop() {
42+
// check if a peripheral has been discovered
43+
BLEDevice peripheral = BLE.available();
44+
45+
if (peripheral) {
46+
// discovered a peripheral, print out address, local name, and advertised service
47+
Serial.print("Found ");
48+
Serial.print(peripheral.address());
49+
Serial.print(" '");
50+
Serial.print(peripheral.localName());
51+
Serial.print("' ");
52+
Serial.print(peripheral.advertisedServiceUuid());
53+
Serial.println();
54+
55+
if (peripheral.localName() != "LED") {
56+
return;
57+
}
58+
59+
// stop scanning
60+
BLE.stopScan();
61+
62+
controlLed(peripheral);
63+
64+
// peripheral disconnected, start scanning again
65+
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
66+
}
67+
}
68+
69+
void controlLed(BLEDevice peripheral) {
70+
// connect to the peripheral
71+
Serial.println("Connecting ...");
72+
73+
if (peripheral.connect()) {
74+
Serial.println("Connected");
75+
} else {
76+
Serial.println("Failed to connect!");
77+
return;
78+
}
79+
80+
// discover peripheral attributes
81+
Serial.println("Discovering attributes ...");
82+
if (peripheral.discoverAttributes()) {
83+
Serial.println("Attributes discovered");
84+
} else {
85+
Serial.println("Attribute discovery failed!");
86+
peripheral.disconnect();
87+
return;
88+
}
89+
90+
// retrieve the LED characteristic
91+
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
92+
93+
if (!ledCharacteristic) {
94+
Serial.println("Peripheral does not have LED characteristic!");
95+
peripheral.disconnect();
96+
return;
97+
} else if (!ledCharacteristic.canWrite()) {
98+
Serial.println("Peripheral does not have a writable LED characteristic!");
99+
peripheral.disconnect();
100+
return;
101+
}
102+
103+
while (peripheral.connected()) {
104+
// while the peripheral is connected
105+
106+
// read the button pin
107+
int buttonState = digitalRead(buttonPin);
108+
109+
if (oldButtonState != buttonState) {
110+
// button changed
111+
oldButtonState = buttonState;
112+
113+
if (buttonState) {
114+
Serial.println("button pressed");
115+
116+
// button is pressed, write 0x01 to turn the LED on
117+
ledCharacteristic.writeValue((byte)0x01);
118+
} else {
119+
Serial.println("button released");
120+
121+
// button is released, write 0x00 to turn the LED off
122+
ledCharacteristic.writeValue((byte)0x00);
123+
}
124+
}
125+
}
126+
127+
Serial.println("Peripheral disconnected");
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
Peripheral Explorer
3+
4+
This example scans for BLE peripherals until one with a particular name ("LED")
5+
is found. Then connects, and discovers + prints all the peripheral's attributes.
6+
7+
The circuit:
8+
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
9+
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
10+
11+
You can use it with another board that is compatible with this library and the
12+
Peripherals -> LED example.
13+
14+
This example code is in the public domain.
15+
*/
16+
17+
#include <ArduinoBLE.h>
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
while (!Serial);
22+
23+
// begin initialization
24+
if (!BLE.begin()) {
25+
Serial.println("starting BLE failed!");
26+
27+
while (1);
28+
}
29+
30+
Serial.println("BLE Central - Peripheral Explorer");
31+
32+
// start scanning for peripherals
33+
BLE.scan();
34+
}
35+
36+
void loop() {
37+
// check if a peripheral has been discovered
38+
BLEDevice peripheral = BLE.available();
39+
40+
if (peripheral) {
41+
// discovered a peripheral, print out address, local name, and advertised service
42+
Serial.print("Found ");
43+
Serial.print(peripheral.address());
44+
Serial.print(" '");
45+
Serial.print(peripheral.localName());
46+
Serial.print("' ");
47+
Serial.print(peripheral.advertisedServiceUuid());
48+
Serial.println();
49+
50+
// see if peripheral is a LED
51+
if (peripheral.localName() == "LED") {
52+
// stop scanning
53+
BLE.stopScan();
54+
55+
explorerPeripheral(peripheral);
56+
57+
// peripheral disconnected, we are done
58+
while (1) {
59+
// do nothing
60+
}
61+
}
62+
}
63+
}
64+
65+
void explorerPeripheral(BLEDevice peripheral) {
66+
// connect to the peripheral
67+
Serial.println("Connecting ...");
68+
69+
if (peripheral.connect()) {
70+
Serial.println("Connected");
71+
} else {
72+
Serial.println("Failed to connect!");
73+
return;
74+
}
75+
76+
// discover peripheral attributes
77+
Serial.println("Discovering attributes ...");
78+
if (peripheral.discoverAttributes()) {
79+
Serial.println("Attributes discovered");
80+
} else {
81+
Serial.println("Attribute discovery failed!");
82+
peripheral.disconnect();
83+
return;
84+
}
85+
86+
// read and print device name of peripheral
87+
Serial.println();
88+
Serial.print("Device name: ");
89+
Serial.println(peripheral.deviceName());
90+
Serial.print("Appearance: 0x");
91+
Serial.println(peripheral.appearance(), HEX);
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(), HEX);
128+
129+
// check if the characteristic is readable
130+
if (characteristic.canRead()) {
131+
// read the characteristic value
132+
characteristic.read();
133+
134+
if (characteristic.valueLength() > 0) {
135+
// print out the value of the characteristic
136+
Serial.print(", value 0x");
137+
printData(characteristic.value(), characteristic.valueLength());
138+
}
139+
}
140+
Serial.println();
141+
142+
// loop the descriptors of the characteristic and explore each
143+
for (int i = 0; i < characteristic.descriptorCount(); i++) {
144+
BLEDescriptor descriptor = characteristic.descriptor(i);
145+
146+
exploreDescriptor(descriptor);
147+
}
148+
}
149+
150+
void exploreDescriptor(BLEDescriptor descriptor) {
151+
// print the UUID of the descriptor
152+
Serial.print("\t\tDescriptor ");
153+
Serial.print(descriptor.uuid());
154+
155+
// read the descriptor value
156+
descriptor.read();
157+
158+
// print out the value of the descriptor
159+
Serial.print(", value 0x");
160+
printData(descriptor.value(), descriptor.valueLength());
161+
162+
Serial.println();
163+
}
164+
165+
void printData(const unsigned char data[], int length) {
166+
for (int i = 0; i < length; i++) {
167+
unsigned char b = data[i];
168+
169+
if (b < 16) {
170+
Serial.print("0");
171+
}
172+
173+
Serial.print(b, HEX);
174+
}
175+
}

examples/Central/Scan/Scan.ino

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Scan
3+
4+
This example scans for BLE peripherals and prints out their advertising details:
5+
address, local name, adverised service UUID's.
6+
7+
The circuit:
8+
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
9+
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <ArduinoBLE.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
// begin initialization
21+
if (!BLE.begin()) {
22+
Serial.println("starting BLE failed!");
23+
24+
while (1);
25+
}
26+
27+
Serial.println("BLE Central scan");
28+
29+
// start scanning for peripheral
30+
BLE.scan();
31+
}
32+
33+
void loop() {
34+
// check if a peripheral has been discovered
35+
BLEDevice peripheral = BLE.available();
36+
37+
if (peripheral) {
38+
// discovered a peripheral
39+
Serial.println("Discovered a peripheral");
40+
Serial.println("-----------------------");
41+
42+
// print address
43+
Serial.print("Address: ");
44+
Serial.println(peripheral.address());
45+
46+
// print the local name, if present
47+
if (peripheral.hasLocalName()) {
48+
Serial.print("Local Name: ");
49+
Serial.println(peripheral.localName());
50+
}
51+
52+
// print the advertised service UUIDs, if present
53+
if (peripheral.hasAdvertisedServiceUuid()) {
54+
Serial.print("Service UUIDs: ");
55+
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
56+
Serial.print(peripheral.advertisedServiceUuid(i));
57+
Serial.print(" ");
58+
}
59+
Serial.println();
60+
}
61+
62+
// print the RSSI
63+
Serial.print("RSSI: ");
64+
Serial.println(peripheral.rssi());
65+
66+
Serial.println();
67+
}
68+
}

0 commit comments

Comments
 (0)