Ultimately, I want to get two Nano 33 BLEs to act as peripherals that send their respective battery levels to a third Nano 33 BLE acting as the central. My first step though is to just get one peripheral to send its battery level to the central. For the peripheral, i used the Arduino BLE example BatteryMonitor.ino. I can see the battery levels in the Serial Monitor and I can connect my iPhone to the peripheral and I can see the battery levels there too. So all seems good on the peripheral side of things.
The Central, however, is where I'm running into an issue (or issues). I added the Central's code below.
The Serial Monitor on the Central side prints the following (minus the "-" marks):
BatteryMonitor
180f
0
Found BatteryMonitor Name
Batt Level 0
So, it appears to have found the Local Name and Service ID but the number of Characteristics appears to be zero and the Batt Level always shows "0" regardless of what is showing the on the peripheral side.
I fully and freely admit I'm probably doing a number of things wrong here but cannot/have not been able to sort out the issue(s). Once I figure this out, I will likely be back for advice on how to add another peripheral to the mix.
#include <ArduinoBLE.h>
// BLE Battery Service
BLEService batteryService("180F");
// BLE Battery Level Characteristic
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.scan();
}
void loop() {
// wait for a BLE central
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.println(peripheral.localName());
Serial.println(peripheral.advertisedServiceUuid());
Serial.println(peripheral.characteristicCount());
if (peripheral.localName() == "BatteryMonitor") {
Serial.println("Found BatteryMonitor Name");
peripheral.connect();
unsigned int batLevel = batteryLevelChar.read();
Serial.print("Batt Level ");
Serial.println(batLevel);
}
}
}