Please Help me this function doesn't fire from the periperical?

I connected perfectly by when I write a value to my service and Characteristic in the arduino nano the function switchCharacteristic.written() never fire.

void loopToConfigLedAndBle() {

// listen for BLE peripherals to connect:

BLEDevice central = BLE.central();

// if a central is connected to peripheral:

if (central) {

Serial.print("Connected to central: ");

// print the central's MAC address:

Serial.println(central.address());

if(central.hasLocalName()){

  Serial.print("Connected to central: ");

  Serial.println(central.localName());

  

}

// while the central is still connected to peripheral:

while (central.connected()) {

  // if the remote device wrote to the characteristic,

  // use the value to control the LED:

  if (switchCharacteristic.written()) {

    int value  = switchCharacteristic.value();

    Serial.print("value: "+value);

    if (value ==1) {   // any value other than 0

      Serial.println("LED on");

      digitalWrite(ledPin, HIGH);         // will turn the LED on

    } else {                              // a 0 value

      Serial.println(F("LED off"));

      digitalWrite(ledPin, LOW);          // will turn the LED off

    }

  }else{

     //Serial.println("is connected:"+central.address());

  }

  //delay(1000);

}

// when the central disconnects, print it out:

Serial.print(F("Disconnected from central: "));

Serial.println(central.address());

}

}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

excuse me. I 'm new here...

void loopToConfigLedAndBle() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    if(central.hasLocalName()){
      Serial.print("Connected to central: ");
      Serial.println(central.localName());
      
    }

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        int value  = switchCharacteristic.value();
        Serial.print("value: "+value);
        if (value ==1) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }else{
         //Serial.println("is connected:"+central.address());
      }
      //delay(1000);
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

And the rest of your code ?

#include <ArduinoBLE.h>

BLEService ledService("77B10000-E8F2-537F-4F6C-D104768A1777"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("77B10001-E8F2-537F-4F6C-D104768A1777", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setupToConfigLedAndBle() {
  
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  BLE.setLocalName("xxxxxx");
  BLE.setDeviceName("xxxxxx");

  String address    = BLE.address();


  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("xxxxxx visible...");

}

Please post your complete program in one posting.

Your code looks similar to the LED peripheral example code of the ArduinoBLE.h library.

What is the central/server? What is sending the value to the peripheral. Is it a phone? Android or iphone? What app?

To start with, I would certainly get the library example code working with a phone running nrfConnect.

yes the central is and android app.

#include <ArduinoBLE.h>

BLEService ledService("77B10000-E8F2-537F-4F6C-D104768A1777"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("77B10001-E8F2-537F-4F6C-D104768A1777", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  BLE.setLocalName("xxxxxx");
  BLE.setDeviceName("xxxxxx");

  String address    = BLE.address();


  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("xxxxxx visible...");

}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    if(central.hasLocalName()){
      Serial.print("Connected to central: ");
      Serial.println(central.localName());
      
    }

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        int value  = switchCharacteristic.value();
        Serial.print("value: "+value);
        if (value ==1) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }else{
         //Serial.println("is connected:"+central.address());
      }
      //delay(1000);
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

please get me this libray please...

Please explain more.
Do you have ArduinoBLE.h on your computer? It is available through the library manager.
The LED library example can be found in the ide with File>Examples>ArduinoBLE>Peripheral>LED

Do you have nRFConnect on your phone?

Thank you. I solved the issue. bluetooth lowenergy - Android BLE Characteristic setValue Unable To Write Correct Data - Stack Overflow

I check this Thread And I check some missing line in may android app code.

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