Go Down

Topic: Characteristics and Attributes of Peripheral Device not Discovered (Read 149 times) previous topic - next topic

CadetheDude34

Hello! I am working on a project to connect two Arduino Nano 33s to each other using the built in BLE modules. The Central board is able to connect to the peripheral board however it is not able to discover the attributes or characteristics of the peripheral board. I have tested the peripheral with a BLE scanner app and the app was able to detect the service and the attributes. Any help of guidance would be greatly appreciated.

Peripheral
Code: [Select]
#include <ArduinoBLE.h>
BLEService bandService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedCharCharacteristic bandChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  if (!BLE.begin())
  {
     while (1);
  }
 
  BLE.setLocalName("BandShake_0002");
  BLE.setAdvertisedService(bandService);
  bandService.addCharacteristic(bandChar);
  BLE.addService(bandService);
  bandChar.writeValue(8);
 
  BLE.advertise();
}

void loop()
{
  BLEDevice central = BLE.central();
  if (central)
   {
      digitalWrite(LED_BUILTIN, HIGH);
 
      while (central.connected()) {
          bandChar.writeValue(2);
          delay(1000);
      }
   }
   digitalWrite(LED_BUILTIN, LOW);
   delay(100);
}



Central
Code: [Select]
#include <ArduinoBLE.h>
BLEService bandService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedCharCharacteristic bandChar("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);


void setup() {
  if (!BLE.begin())
  {
    Serial.println("failed to start");
     while (1);
  }
//  BLE.setLocalName("BandShake_0002");
//  BLE.setAdvertisedService(bandService);
//  bandService.addCharacteristic(bandChar);
//  BLE.addService(bandService);
//  bandChar.writeValue(0);
//  BLE.advertise();

 
  BLE.scanForUuid("19B10000-E8F2-537E-4F6C-D104768A1214");
}
void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // 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();

    // stop scanning
    BLE.stopScan();

    controlBand(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19B10000-E8F2-537E-4F6C-D104768A1214");
  }
}

void controlBand(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

    // discover peripheral attributes
    Serial.println("Discovering attributes ...");
    if (peripheral.discoverAttributes()) {
      Serial.println("Attributes discovered");
    } else {
      Serial.println("Attribute discovery failed!");
      peripheral.disconnect();
      return;
    }

    BLECharacteristic bandCharVal = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
   
    if (bandCharVal) {
      Serial.println("Band");
    }
    else{
      Serial.println("No Band");
      peripheral.disconnect();
      return;
    }
}

Go Up