BLE 33 Peripheral not updating

Hello team - I have a fairly basic BLE 33 Peripheral sketch. It compiles and uploads without issue.

Where it fails (or my logic or ability fails) is in reading correct values from the defined Service Characteristic, or recognising Write updates occurring from any Central app.

The attached shows pre-loading the only Characteristic with 'A' however when reading the characteristic values "1" is returned. What have I done incorrectly?

When testing with the LightBlue app (installed on iPhone) I can clearly see 'A' loaded to the relevant characteristic (UUID commencing with 19B10001) and if I writeValue() to the same characteristic with a central app I can see the characteristic updated, however no change to "1" being returned from the readValue() statement.

Any insight or assistance will be very much appreciated.

#include <ArduinoBLE.h>

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

BLEService appService(deviceServiceUuid); 
BLEByteCharacteristic appCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);

byte uuval = 0;

void setup() {
  Serial.begin(9600);
  
  if (!BLE.begin()) {
    Serial.println("- Starting BLE module failed!");
    while (1);
  }

  BLE.setLocalName("Nano 33 BLE");
  BLE.setAdvertisedService(appService);
  appService.addCharacteristic(appCharacteristic);
  BLE.addService(appService);
  appCharacteristic.writeValue('A');   // this works, visible in LightBlue
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(500);

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      if (appCharacteristic.valueUpdated()){   // never being triggered because it sees an unchangeing "1" not the value visible via LightBlue
         appCharacteristic.readValue(uuval);
         Received(uuval);
       }
       Serial.print("characteristic value:");
       Serial.println(appCharacteristic.readValue(uuval)); // shows "1" not A. Doesn't show update when sending new data from Central
    }
    Serial.println("* Disconnected to central device!");
  }
}

void Received(byte VAL) {
  Serial.println("- Received New Value");
  if(VAL == 0x41)
    Serial.println("A");
  else if(VAL == 0x42)
    Serial.println("B");
  else if(VAL == 0x43)
    Serial.println("C");
  else if(VAL == 0x01)
    Serial.println("1");
  else if(VAL == 0x02)
    Serial.println("2");
  
}

Welcome to the forum.

You must use bleCharacteristic.written() instead of valueUpdated.

https://www.arduino.cc/en/Reference/ArduinoBLEBLECharacteristicwritten

valueUpdated is for the central devices to check whether the characteristic has been updated by the peripheral. See examples in both links

https://www.arduino.cc/en/Reference/ArduinoBLEBLECharacteristicvalueUpdated

Thank you so much Klaus, this was very helpful.

In fact I was fully resolved within a couple of minutes following your advice and reviewing the reference links.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.