Hi everyone,
I'm trying to connect two Nano 33 BLEs over bluetooth and read information from the peripheral device, but I'm having some issues. I can reliably get the devices to connect to one another and correctly tell me each other's local names, but when I try and read the value of a characteristic from the peripheral device I seem to get nothing.
Here is the code I'm running on the central device:
#include <ArduinoBLE.h>
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
void setup() {
Serial.begin(9600);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Remi Smartwatch");
BLE.setDeviceName("Remi Smartwatch");
BLE.advertise();
Serial.println("Remi Smartwatch Bluetooth Initialized");
Serial.println(" ");
}
void loop() {
// look for the "phone"
Serial.println("Looking for Peripheral Device");
BLE.scanForUuid(deviceServiceUuid);
BLEDevice phone = BLE.available();
// if a peripheral is found begin communicating
if (phone) {
// confirm phone device details
Serial.println("* Phone found!");
Serial.print("* Phone MAC address: ");
Serial.println(phone.address());
Serial.print("* Phone name: ");
Serial.println(phone.localName());
Serial.print("* Advertised service UUID: ");
Serial.println(phone.advertisedServiceUuid());
Serial.println(" ");
BLE.stopScan();
// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (phone.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
phone.disconnect();
//return;
}
while (phone.connected()) {
// Request information from the peripheral
BLECharacteristic medName = phone.characteristic(deviceServiceCharacteristicUuid);
byte med = 0;
medName.readValue(med);
Serial.println(med);
delay(100);
}
}
}
...and here is the code I'm running on the peripheral device:
#include <ArduinoBLE.h>
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
BLEService medService(deviceServiceUuid);
BLEByteCharacteristic medName(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("Remi Phone App");
BLE.setDeviceName("Remi Phone App");
BLE.setAdvertisedService(medService);
BLE.addService(medService);
medName.writeValue(1);
BLE.advertise();
Serial.println("Remi Phone App Bluetooth Initialized");
Serial.println(" ");
}
void loop() {
// put your main code here, to run repeatedly:
BLEDevice remiWatch = BLE.central();
if (remiWatch) {
Serial.println("* Connected to Remi Smartwatch!");
Serial.println("* Device MAC Address: ");
Serial.println(remiWatch.address());
Serial.println(" ");
while (remiWatch.connected()) {
medName.writeValue(0);
delay(1000);
medName.writeValue(1);
delay(1000);
}
}
}