1#include <ArduinoBLE.h>
2
3
4BLEService ledService("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
5BLEUnsignedCharCharacteristic ledCharacteristic("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", BLEWrite | BLEWriteWithoutResponse);
6
7void setup() {
8 Serial.begin(9600);
9
10
11 if (!BLE.begin()) {
12 Serial.println("Failed to start BLE!");
13 while (1)
14 ;
15 }
16
17
18 BLE.setLocalName("LEDPeripheral");
19 BLE.setAdvertisedService(ledService);
20
21
22 ledService.addCharacteristic(ledCharacteristic);
23
24
25 BLE.addService(ledService);
26
27
28 ledCharacteristic.writeValue(0);
29
30
31 BLE.advertise();
32 Serial.println("BLE LED Peripheral is now advertising...");
33}
34
35void loop() {
36
37 BLEDevice central = BLE.central();
38
39 if (central) {
40 Serial.print("Connected to: ");
41 Serial.println(central.address());
42
43
44 while (central.connected()) {
45
46 if (ledCharacteristic.written()) {
47
48 int ledValue = ledCharacteristic.value();
49 Serial.print("Received value: ");
50 Serial.println(ledValue);
51 }
52 }
53
54
55 Serial.print("Disconnected from: ");
56 Serial.println(central.address());
57 }
58}