Arduino
This sample code shows how to use a switch component in an Arduino project.
#include <bluefruit.h>
// Custom 128-bit UUIDs (change these to whatever you want)
BLEService customService(BLEUuid("12345678-1234-5678-1234-56789abcdef0"));
BLECharacteristic customChar(BLEUuid("12345678-1234-5678-1234-56789abcdef1"));
uint8_t value = 0;
// Called when the central writes to our characteristic
void writeCallback(uint16_t conn_hdl, BLECharacteristic* chr,
uint8_t* data, uint16_t len) {
if (len < 1) return;
value = data[0]; // This is coming from the UI switch (0–1)
Serial.print("New value from central: ");
Serial.println(value);
}
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); } // for nRF52840 with native USB
Serial.println("Simple custom BLE switch example");
Bluefruit.begin();
Bluefruit.setName("Bleboard");
customService.begin();
customChar.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE);
customChar.setPermission(SECMODE_OPEN, SECMODE_OPEN);
customChar.setFixedLen(1);
customChar.setWriteCallback(writeCallback);
customChar.begin();
customChar.write8(value);
Bluefruit.Advertising.addService(customService);
Bluefruit.Advertising.start(0);
Serial.println("Advertising...");
}
void loop() {
// Value is driven by central writes.
delay(250);
}Last updated on