Reputation: 1
first post here! I'm trying to use an Arduino Nano RP2040 Connect as a Bluetooth Low Energy peripheral, currently having 1 service with 3 characteristic exposing:
The code is this:
#include <ArduinoBLE.h>
#define LED_RSSI 2
#define LED_STATUS 4
int oldSensorValue = 0;
int oldRssiValue = 0;
bool ledState = false;
long previousMillis = 0;
BLEService sensorService("18902a9a-1f4a-44fe-936f-14c8eea41800");
BLEIntCharacteristic sensorChar("18902a9a-1f4a-44fe-936f-14c8eea41801", BLERead | BLENotify);
BLEIntCharacteristic rssiChar("18902a9a-1f4a-44fe-936f-14c8eea41802", BLERead | BLENotify);
BLEBoolCharacteristic ledChar("18902a9a-1f4a-44fe-936f-14c8eea41803", BLERead | BLEWrite | BLENotify);
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_RSSI, OUTPUT);
pinMode(LED_STATUS, OUTPUT);
digitalWrite(LED_STATUS, ledState);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1)
;
}
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
BLE.setLocalName("Sensor Monitor");
//BLE.setDeviceName(name); //defaults “Arduino”
BLE.setAppearance(0x015); //set appearance as "Sensor" 0x015
sensorService.addCharacteristic(sensorChar);
sensorService.addCharacteristic(rssiChar);
sensorService.addCharacteristic(ledChar);
BLE.addService(sensorService);
BLE.setAdvertisedService(sensorService); // add the service UUID
sensorChar.writeValue(oldSensorValue);
rssiChar.writeValue(oldRssiValue);
ledChar.writeValue(ledState);
ledChar.setEventHandler(BLEWritten, onUpdateLed);
sensorChar.setEventHandler(BLESubscribed, onSubscribedChar);
rssiChar.setEventHandler(BLESubscribed, onSubscribedChar);
ledChar.setEventHandler(BLESubscribed, onSubscribedChar);
sensorChar.setEventHandler(BLEUnsubscribed, onUnsubscribedChar);
rssiChar.setEventHandler(BLEUnsubscribed, onUnsubscribedChar);
ledChar.setEventHandler(BLEUnsubscribed, onUnsubscribedChar);
startAdvertise();
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
while (central.connected()) {
long currentMillis = millis();
long timer2 = millis();
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
updateSensorValue();
updateRSSI();
}
}
}
}
void startAdvertise() {
if (!BLE.advertise()) {
Serial.println("Bluetooth® device failed to advertise.");
while (1) {
;
}
} else {
Serial.println("Bluetooth® device active, waiting for connections...");
}
}
void updateSensorValue() {
int sensorValue = analogRead(A0);
if (sensorValue != oldSensorValue) {
// Serial.print("sensor Level % is now: ");
// Serial.println(sensorValue);
sensorChar.writeValue(sensorValue);
oldSensorValue = sensorValue;
}
}
void updateRSSI() {
int newRssiValue = BLE.rssi();
if (newRssiValue != 127)
analogWrite(LED_RSSI, map(abs(newRssiValue), 0, 128, 255, 0));
else
analogWrite(LED_RSSI, 0);
if (newRssiValue != oldRssiValue) {
// Serial.print("RSSI is now: ");
// Serial.println(newRssiValue);
rssiChar.writeValue(newRssiValue);
oldRssiValue = newRssiValue;
}
}
void onUpdateLed(BLEDevice central, BLECharacteristic characteristic) {
Serial.println("Central '" + central.address() + "' wrote '" + ledChar.value() + "' to characteristic '" + characteristic.uuid() + "'");
ledState = ledChar.value();
digitalWrite(LED_STATUS, ledState);
}
void onSubscribedChar(BLEDevice central, BLECharacteristic characteristic) {
Serial.println("Central '" + central.address() + "' subscribed to characteristic '" + characteristic.uuid() + "'");
}
void onUnsubscribedChar(BLEDevice central, BLECharacteristic characteristic) {
Serial.println("Central '" + central.address() + "' unsubscribed to characteristic '" + characteristic.uuid() + "'");
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.println("Central '" + central.address() + "' connected");
digitalWrite(LED_BUILTIN, HIGH);
BLE.stopAdvertise();
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.println("Central '" + central.address() + "' disconnected");
digitalWrite(LED_BUILTIN, LOW);
analogWrite(LED_RSSI, 0);
startAdvertise();
}
I have tried its functionality with nRF Connect and LightBlue on Android, trying to read, subscribe and write characteristics, everything seems to work as intended.
The problem I'm facing is that whatever I do with the UUIDs on the Arduino side I keep seeing the same ones on the apps mentioned above. On nRF Connect there's the possibility to "Refresh services" and that does fixes temporarely the issue but if I disconnect from the peripheral and reconnect the issue appears again. Tried cleaning data and cache of both apps, of bluetooth system app, without success.
Its not only those 2 apps that do this behaviour:I'm trying to interface the Arduino with a Unity app ran on Android and it fails to subscribe to the characteristics with the "new" UUIDs but somehow succeed if I set the Arduino with the UUIDs my phone is stuck seeing.
A completely different phone sees updates UUIDs, everytime, but I need to use my phone for developing.
Is there something wrong with my phone behaviour? It's a Motorola Moto G52. How can I fix this? What am I doing wrong? Any help appreciated
Upvotes: 0
Views: 210
Reputation: 18472
Apparently the client uses GATT caching. This way it saves some round trips for service discovery for every connection start.
A client can cache the database structure (only) in one of the following cases:
The devices are bonded. The server uses the Service Changed characteristic to inform the client when something changes with the db structure, by sending an indication containing the range of the handles that have changed.
Both the client and server support the relatively new Database Hash characteristic, which contains a hash of the db structure which the server stores in a characteristic which the client reads on every reconnection. If it has changed since the last time, the client must rediscover.
The server never changes the db structure during its lifetime. This is indicated to the client by not having the Service Changed characteristic.
If none of the above cases apply, then the client is buggy and behaves incorrectly.
Upvotes: 0