Arduino
This sample code shows how to use a value 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-56789abcdef2"));
BLECharacteristic customChar(BLEUuid("12345678-1234-5678-1234-56789abcdef3"));
uint8_t value = 0;
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); } // for nRF52840 with native USB
Serial.println("Simple custom BLE example");
Bluefruit.begin();
Bluefruit.setName("Bleboard");
customService.begin();
customChar.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
customChar.setPermission(SECMODE_OPEN, SECMODE_OPEN);
customChar.setFixedLen(1);
customChar.begin();
// Set the initial characteristic value so reads return value
customChar.write8(value);
Bluefruit.Advertising.addService(customService);
Bluefruit.Advertising.start(0);
Serial.println("Advertising...");
}
void loop() {
static uint32_t lastNotifyMs = 0;
uint32_t now = millis();
// Notify once per second if a central is connected
if (Bluefruit.connected() && (now - lastNotifyMs) > 1000) {
lastNotifyMs = now;
value++;
// Keep it in range
if (value >= 100) {
value = 0;
}
// Update characteristic value
customChar.write8(value);
// Send notification (only delivered if central enabled notifications)
customChar.notify8(value);
Serial.print("Notified value: ");
Serial.println(value);
}
delay(1000);
}Last updated on