Seeed XIAO nRF522840 (not sense)
Seeed XIAO nRF52 library: 1.1.8 (not embed)
ArduinoBLE Library: 1.3.7
#include <ArduinoBLE.h>
BLEService LEDService("19b10000-e8f2-537e-4f6c-d104768a1214"); // Service UUID
BLEIntCharacteristic redCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite | BLENotify);
BLEIntCharacteristic greenCharacteristic("19b10002-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite | BLENotify);
BLEIntCharacteristic blueCharacteristic("19b10003-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite | BLENotify);
const int redPIN = LED_RED; // pin to use for the red LED (D11)
const int greenPIN = LED_GREEN; // pin to use for the green LED (D12)
const int bluePIN = LED_BLUE; // pin to use for the blue LED (D13)
void setup() {
pinMode(redPIN, OUTPUT); //set pins as outputs and set LED's HIGH (HIGH is off for this board)
digitalWrite(redPIN, HIGH);
pinMode(greenPIN, OUTPUT);
digitalWrite(greenPIN, HIGH);
pinMode(bluePIN, OUTPUT);
digitalWrite(bluePIN, HIGH);
if (!BLE.begin()) { // begin initialization
while (1); // wait until initialization complete
}
BLE.setLocalName("BLE_RGB"); // set advertised local name
BLE.setAdvertisedService(LEDService); // set advertised service UUID
LEDService.addCharacteristic(redCharacteristic); // add the red characteristic to the service
LEDService.addCharacteristic(greenCharacteristic); // add the green characteristic to the service
LEDService.addCharacteristic(blueCharacteristic); // add the blue characteristic to the service
BLE.addService(LEDService); // add service
BLE.advertise(); // start advertising
}
void loop() {
BLEDevice central = BLE.central(); // listen for BLE devices to connect:
if (central) { // if a central is connected to peripheral:
// put code here to preform 1 time when device is connected
while (central.connected()) { // while the central is still connected to peripheral
// if there is an update from Android App, change light ON (0) or OFF (1)
if (redCharacteristic.written()) digitalWrite(redPIN, redCharacteristic.value());
if (greenCharacteristic.written()) digitalWrite(greenPIN, greenCharacteristic.value());
if (blueCharacteristic.written()) digitalWrite(bluePIN, blueCharacteristic.value());
}
} //end of while loop
// you can put code here for what to do when not connected
} // end of loop