I created a BLE app, with BLEextension. However it only goes as far as discovering and pairing with the device. When I press the button that is supposed to command the peripheral and write response as 1, to operate a function, the app forces close.
This is my code in arduino ide to my microcontroller Xiao ESP32s3
#include <ArduinoBLE.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
bool deviceConnected = false;
const int motorPin = 9;
BLEService serviceUUID("e472f814-0e36-463a-81aa-d8be9577ff48");
BLEIntCharacteristic characteristicUUID("e472f815-0e36-463a-81aa-d8be9577ff48", BLERead | BLENotify | BLEWrite);
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(38400);
while (!Serial);
// Set the motor pin as an output
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName ("BuzzLE");
BLE.setAdvertisedService(serviceUUID);
serviceUUID.addCharacteristic(characteristicUUID);
BLE.addService(serviceUUID);
characteristicUUID.setValue(0);
BLE.advertise();
Serial.println("Waiting for a central device to connect...");
}
void loop() {
// Handle BLE events
BLEDevice central = BLE.central();
// If a device is connected, check the characteristic value and vibrate accordingly
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
deviceConnected = true;
while (central.connected()) {
if (characteristicUUID.written()) {
uint8_t value = characteristicUUID.value()[0];
if (value == 1) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
deviceConnected = false;
digitalWrite(motorPin, LOW); // Stop vibrating when disconnected
} else {
digitalWrite(motorPin, LOW); // Stop vibrating when no central device is connected
}
}
Please help me, we're running out of time T.T