LED_Control_HM10.aia (157.0 KB)
#include <ArduinoBLE.h>
#include <String>
BLEService nanoService("0000ffe1-0000-1000-8000-00805f9b34fb"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
String test = "gaming";
BLEByteCharacteristic bleRW("0000ffe0-0000-1000-8000-00805f9b34fb", BLERead | BLEWrite);
BLEStringCharacteristic sdSend("0000ffe2-0000-1000-8000-00805f9b34fb", BLERead | BLEWrite , test.length());
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED's pin to output mode
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE Sense");
BLE.setAdvertisedService(nanoService);
// add the characteristic to the service
nanoService.addCharacteristic(bleRW);
nanoService.addCharacteristic(sdSend);
// add service
BLE.addService(nanoService);
// set the initial value for the characteristic:
bleRW.writeValue('0');
// start advertising
BLE.advertise();
Serial.println("Arduino BLE");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (bleRW.written()) {
switch (bleRW.value()) { // any value other than 0
case 1:
Serial.println("LED on");
digitalWrite(LED_BUILTIN, HIGH); // will turn the LED off
break;
case 2:
Serial.println(F("LED off"));
digitalWrite(LED_BUILTIN, LOW); // will turn the LED off
break;
case 3:
bleRW.writeValue((byte)0x05);
sdSend.writeValue((String)test);
Serial.println("Handshake initiated");.
break;
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
}
}
I am making a simple test app to send and receive BLE data between my Arduino Nano 33 BLE Sense and an Android phone.
One issue I am facing right now is that when I press disconnect on my app, the phone disconnects but the Arduino thinks it is still connected. This is irritating as I have to reupload the Arduino to reset the BLE (pressing the physical reset button doesn't seem to work sadly).
What am I missing?