#include <ArduinoBLE.h>
#define MY_UUID(val) ("D65D0396-" val "-4381-9985-653653CE831F")
char bleName[] = "BLE_events";
BLEService myService(MY_UUID("0000"));
BLEByteCharacteristic thirdChar(MY_UUID("0003"), BLERead | BLEWrite | BLENotify);
BLECharacteristic fourthChar(MY_UUID("0004"), BLERead | BLEWrite, 40 | BLENotify);
void setup() {
Serial.begin(9600);
if (!Serial) delay(3000);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) {
Serial.println("starting BLE failed");
while (true);
}
BLE.setLocalName(bleName);
Serial.println(bleName);
BLE.setAdvertisedService(myService);
myService.addCharacteristic(thirdChar);
myService.addCharacteristic(fourthChar);
BLE.addService(myService);
thirdChar.setValue(0);
fourthChar.setValue(0);
BLE.setEventHandler(BLEConnected, connectHandler);
BLE.setEventHandler(BLEDisconnected, disconnectHandler);
thirdChar.setEventHandler(BLEUpdated, characteristicUpdated);
thirdChar.setEventHandler(BLESubscribed, characteristicSubscribed);
thirdChar.setEventHandler(BLEUnsubscribed, characteristicUnsubscribed);
fourthChar.setEventHandler(BLEUpdated, characteristicUpdated);
fourthChar.setEventHandler(BLESubscribed, characteristicSubscribed);
fourthChar.setEventHandler(BLEUnsubscribed, characteristicUnsubscribed);
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
BLE.poll();
}
void connectHandler(BLEDevice central) {
Serial.print("Connected event, central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
}
void disconnectHandler(BLEDevice central) {
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, LOW);
}
void characteristicSubscribed(BLEDevice central, BLECharacteristic thisChar) {
Serial.print("Characteristic subscribed. UUID: ");
Serial.println(thisChar.uuid());
}
void characteristicUnsubscribed(BLEDevice central, BLECharacteristic thisChar) {
Serial.print("Characteristic unsubscribed. UUID: ");
Serial.println(thisChar.uuid());
}
void characteristicUpdated(BLEDevice central, BLECharacteristic thisChar) {
byte incoming[40];
byte outgoing[50] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190.200, 210, 220, 230.240, 250, 255};
Serial.print("Characteristic updated. UUID: ");
Serial.print(thisChar.uuid());
Serial.print(" value: ");
Serial.println();
thisChar.readValue(incoming, 40);
Serial.println("incoming: ");
Serial.print("HEX: ");
for (int i = 0; i < 40; i++) {
Serial.print(incoming[i], HEX);
Serial.print("-");
}
Serial.println();
Serial.print("DEC: ");
for (int i = 0; i < 40; i++) {
Serial.print(incoming[i], DEC);
Serial.print("-");
}
Serial.println();
thisChar.writeValue(outgoing, 50);
Serial.println("outgoing: ");
Serial.print("HEX: ");
for (int i = 0; i < 40; i++) {
Serial.print(outgoing[i], HEX);
Serial.print("-");
}
Serial.println();
Serial.print("DEC: ");
for (int i = 0; i < 40; i++) {
Serial.print(outgoing[i], DEC);
Serial.print("-");
}
Serial.println();
}