-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nano33BLE_peripheral.ino
62 lines (48 loc) · 1.7 KB
/
Nano33BLE_peripheral.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
This program uses the ArduinoBLE library to set up an Arduino Nano 33 BLE
as a peripheral device and specifies a service and a characteristic.
The circuit:
- Arduino Nano 33 BLE.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
const char* deviceServiceUuid = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const char* deviceServiceCharacteristicUuid = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
BLEService ourService(deviceServiceUuid);
BLEByteCharacteristic ourCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("- Starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
BLE.setAdvertisedService(ourService);
ourService.addCharacteristic(ourCharacteristic);
BLE.addService(ourService);
ourCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Nano 33 BLE (Peripheral Device)");
Serial.println(" ");
}
void loop() {
BLEDevice central = BLE.central();
Serial.println("- Discovering central device...");
delay(500);
if (central) {
Serial.println("* Connected to the central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
while (central.connected()) {
int charac = 1;
Serial.print("* Writing value to our characteristic: ");
Serial.println(charac);
ourCharacteristic.writeValue((byte)charac);
Serial.println("* Writing value to our characteristic done!");
Serial.println(" ");
}
}
Serial.println("* Disconnected from the central device!");
}