Cannot read BLE characteristic on Arduino Uno R4 WiFi

I am running the BLE BatteryMonitor example sketch on a Nano 33 BLE Sense and can read the Service 1801 and Characteristic 2A19 on nRF Connect app on my Android phone. I am trying to read the same Service and Characteristic on my Uno R4 WiFi with the following sketch.

#include <ArduinoBLE.h>

 void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  //while(!Serial);
  digitalWrite(LED_BUILTIN, LOW);
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1)
      ;
  }
  Serial.println("BLE Central for Temperature Battery Server");
  BLE.scanForUuid("180F");
}
 
 void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();
  if (peripheral) {
    digitalWrite(LED_BUILTIN, HIGH);
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();
    BLE.stopScan();
  }
 
 peripheral.connect();
  while (peripheral) {
    peripheral.discoverAttributes();
    BLEService service = peripheral.service("180F");
    Serial.print("Service ");
    Serial.print(service.uuid());
    BLECharacteristic characteristicBatt = service.characteristic("2A19");
    readCharacteristicBattValue(characteristicBatt);
  }
}

void readCharacteristicBattValue(BLECharacteristic characteristicBatt) {
  // print the UUID and properties of the characteristic
  Serial.print("\t ");
  Serial.print(characteristicBatt.uuid());
  // check if the characteristicTemp is readable
  if (characteristicBatt.canRead()) {
    //read the characteristicTemp value
    int16_t battery = 0;
    characteristicBatt.readValue(&battery, 2);
    Serial.print(", battery = ");
    Serial.print(battery / 100);
    Serial.println("%");
    delay(10000);
  }
}

However the output is always 0% as though the sketch is not reading the characteristic correctly. The monitor output is:

BLE Central for Temperature Battery Server
Found ba:8e:08:04:a3:4d 'BatteryMonitor' 180f
Service 180f 2a19, battery = 0%
Service 180f 2a19, battery = 0%

What am I doing wrong?

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