BLE Central cant find the characteristic

Hello there,

I am using two Arduino Nano 33 IOT.

I am having alot of trouble with with BLE lately. For some reason I am not able to retrieve the characteristic "SpielerNameBLE" from the peripheral with the central.

When I am using my phone I have no problems manipulating the value of the characteristic.

If anybody knows what might be the problem I would appreciate every response!

The central code:

#include <ArduinoBLE.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);


  // initialize the Bluetooth® Low Energy hardware
  BLE.begin();

  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");

  VerbindungHerstellen();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void VerbindungHerstellen() {
  // start scanning for Trichter

  while (true) {
  
    BLEDevice Trichter = BLE.available();

    if (Trichter) {

      //Daten über die Verbindung
      Serial.print("Found ");
      Serial.print(Trichter.address());
      Serial.print(" '");
      Serial.print(Trichter.localName());
      Serial.print("' ");
      Serial.print(Trichter.advertisedServiceUuid());
      Serial.println();


      // stop scanning
      BLE.stopScan();

      DatenVerkehr(Trichter);

      // peripheral disconnected, start scanning again
      BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
    }
  }
}

//schickt Daten an den Trichter
void DatenVerkehr(BLEDevice Trichter) {

  //Gerät wird verbunden
  Serial.println("In der Methode Datenverkehr");
  //es wird überprüft, ob der Controller mit dem Trichter verbunden ist

  if (Trichter.connect()) {

    Serial.println("Connected");

    //characteristics vom Trichter rüberziehen

    BLECharacteristic SpielerNameBLE = Trichter.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");

    if (SpielerNameBLE) {
      Serial.println("Chararcteristic gefunden");
      char value[20] = "Hannes Laing";
    
      SpielerNameBLE.writeValue(&value, sizeof(value));
    } else {
      Serial.print("Charakteristik nicht gefunden");
    }
  }
  //Wenn nicht dann wird das Programm zurück in die Verbindungsschleife geschickt
  else {
    Serial.println("Verbindung fehlgeschlagen");
    return;
  }
}



The peripheral code:

#include <ArduinoBLE.h>

//Bluetooth service wird kreiert
BLEService TrichterService("19B10000-E8F2-537E-4F6C-D104768A1214");
//erstellt eine switch characteristic für SpielerName
BLEByteCharacteristic SpielerNameBLE("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
//erstellt eine characteristic für SpielerID

String SpielerName = "";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  BLE.begin();
  //Überprüuft ob Bluetoooth gestartet
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");
    while (true);
  }


  //angezeigter Name
  BLE.setLocalName("TrichterTimer");
  //Die UUID die benutzt wird um die Datei zu erkennen
  BLE.setAdvertisedService(TrichterService);

  //characteristics hinzufügen
  TrichterService.addCharacteristic(SpielerNameBLE);

  //Service hinzufügen
  BLE.addService(TrichterService);

  //ersten Werte hinzufügen
  SpielerNameBLE.setValue(0);

  //wird jetzt advertised
  VerbindungHerstellen();

  
}

void loop() {
  // put your main code here, to run repeatedly:
}


void VerbindungHerstellen() {

  BLE.advertise();
  while (true) {
    //Wenn das richtige Central gerät gefunden ist
    BLEDevice Controller = BLE.central();

    if (Controller.connected()) {
      Serial.println("Mit Controller verbunden");
      BLE.stopAdvertise();
      DatenVerkehr(Controller);
    }
    Serial.println("Disconnected");
  }
}

void DatenVerkehr(BLEDevice Controller) {
  //Wartet auf geschickte Daten solange er verbunden ist
  while (Controller.connected()) {
    //Serial.println("Aktiv verbunden");

    if (SpielerNameBLE.written()) {
      Serial.println("Written");
      char value = SpielerNameBLE.value();
      SpielerName += String(value);
      Serial.println(SpielerName);
    }
  }
}

The Serialmonitor of the Central:

22:45:41.915 -> Found 34:94:54:21:9e:da 'TrichterTimer' 19b10000-e8f2-537e-4f6c-d104768a1214

22:45:41.915 -> In der Methode Datenverkehr

22:45:42.119 -> Connected

22:45:42.119 -> Charakteristik nicht gefunden

Hi.
I compared your code with mine. My only remark is for the BLE.advertise() wrote on the loop().
I put it once in the code, just after declaring the service and adding characteristic to it.
I am pretty sure it is not needed to advertise more than once and could interfering with communication.

Hi thanks for the reply!

I tried that already didnt seam to change anything.
Still able to connect to the peripheral but not able to retrieve the characteristic :((

can you confirm you have different behavior with the 2 phone app you use?

I propose you get documentation from the non functionnal app, as it seems to be not related to arduino.
Pro tip: I use to read here and there that BLE library doesn't need to "pair" between periph and central

Yes when I am using my phone I have no problem seeing the characteristic and writing a new value

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