#include <ArduinoBLE.h>
const int VERSION = 0x00000001;
#define UUID(val) ("cfccb0fd-" val "-4da3-afaf-a4305a6b64b3")
#define DEBUG 1
BLEService service (UUID("0000"));
BLEUnsignedIntCharacteristic versionCharacteristic (UUID("0001"), BLERead);
BLEFloatCharacteristic testCharacteristic (UUID("0002"), BLENotify);
String name;
unsigned long lastNotify = 0;
void printSerialMsg(const char * msg) {
#ifdef DEBUG
if (Serial) {
Serial.println(msg);
}
#endif
}
void updateSubscribedCharacteristics() {
if (testCharacteristic.subscribed()) {
float test = 3.141;
testCharacteristic.writeValue(test);
}
}
void setup() {
#ifdef DEBUG
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
#endif
delay(5000);
if (!BLE.begin()) {
printSerialMsg("Failed to initialized BLE!");
}
String address = BLE.address();
#ifdef DEBUG
if (Serial) {
Serial.print("address = ");
Serial.println(address);
}
#endif
address.toUpperCase();
name = "Nano IoT - ";
name += address[address.length() - 5];
name += address[address.length() - 4];
name += address[address.length() - 2];
name += address[address.length() - 1];
#ifdef DEBUG
if (Serial) {
Serial.print("name = ");
Serial.println(name);
}
#endif
BLE.setLocalName(name.c_str());
BLE.setDeviceName(name.c_str());
BLE.setAdvertisedService(service);
service.addCharacteristic(versionCharacteristic);
service.addCharacteristic(testCharacteristic);
versionCharacteristic.setValue(VERSION);
BLE.addService(service);
BLE.advertise();
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
while (BLE.connected()) {
unsigned long now = millis();
if (abs((long) now - (long) lastNotify) >= 100) {
updateSubscribedCharacteristics();
lastNotify = now;
}
}
}
After compiling and uploading this, I was able to see the advertised services in a BLE scanner (used LightBlue) and could see the Nano IoT as a connected device in my settings.
I was not able to find any documentation on how a service has to be structured such that the App accepts it as a sensor. As you are providing this library for connecting Arduino devices to the App, i´m placing this issue here but if you think it would be more appropiate in the App-repository, feel free to transfer it.