Bluetooth Connection Issues

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);
    }
  }
}
1 Like

To add detail about my problem, the serial monitor output of my central device keeps repeating these lines:

Looking for Peripheral Device
Looking for Peripheral Device
Looking for Peripheral Device

  • Phone found!
  • Phone MAC address: 27:63:7b:ba:4a:25
  • Phone name: Remi Phone App
  • Advertised service UUID: 19b10000-e8f2-537e-4f6c-d104768a1214

Discovering attributes ...
Attribute discovery failed!
Looking for Peripheral Device
Looking for Peripheral Device

and so on...

I got my central device to discover the attributes of my peripheral device by adding the line

phone.connect();

after I create the phone BLEDevice object.

However I still can't seem to get the value of the medName characteristic from the peripheral device. My code is set up so that the value of medName on the peripheral changes every second, and if my central device code is working properly I should be able to see it change on the central device serial monitor. The central device serial monitor is outputting zeros, so my central device isn't actually reading the value of the medName characteristic from the peripheral.

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