arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
319 stars 207 forks source link

Peripheral device does not receive values ​​written by the central device #81

Open avilmaru opened 4 years ago

avilmaru commented 4 years ago

Central Device: Arduino 33 BLE Sense Peripheral device: Arduino MKR WiFi 1010.

Upgrading from version 1.1.0 to version 1.1.2 the peripheral device does not receive values ​​written by the central device. Before everything worked correctly.


Central device code

// ....

while (peripheral.connected()) {

    String command = calculoAngulos(); 

    if (command != "")
    {
      int n = command.length(); 
      // declaring character array 
      char char_array[n + 1]; 
      // copying the contents of the string to char array 
      strcpy(char_array, command.c_str()); 
      gestureCharacteristic.writeValue((const char*)char_array);
     delay(1);
    }

  }
// ... etc

Peripheral device code

// ....

 // while the central is still connected to peripheral:
    while (central.connected()) {

      // if the remote device wrote to the characteristic,
      if (gestureCharacteristic.written()) {
         command = gestureCharacteristic.value();
         //Serial.print(F("commmand value:  "));
         //Serial.println(command);
       }

    }

// ... etc

--> Both devices are connected correctly --> in version 1.1.0 it works correctly!!!

polldo commented 4 years ago

Hi @avilmaru , I have tried your same scenario with the two sketches below. Everything seems to be working, so you must have a problem in your sketch. Could you try again taking these sketches as references?
Thanks

Central Sketch:

#include "ArduinoBLE.h"

// Value to write into peripheral gesture characteristic
uint8_t gesture = 66;

void setup()
{
  Serial.begin(115200);
  BLE.begin();
  BLE.debug(Serial);

  BLE.scanForName("peripheral");
}

void loop()
{
  static unsigned long refTime = millis();

  BLEDevice peripheral = BLE.available();
  if (peripheral) {
    BLE.stopScan();
    if (peripheral.connect()) {
      peripheral.discoverAttributes();
      BLECharacteristic gestureCharacteristic = peripheral.characteristic("2A56");

      while (peripheral.connected()) {
        if (millis() > refTime + 5000) {
          refTime = millis();

          if (gestureCharacteristic) {
            // Update value to write into peripheral characteristic
            gesture += 10;

            auto ret = gestureCharacteristic.writeValue(gesture);
            if (ret) {
              Serial.print("gesture written: ");
              Serial.println(gesture);
            } else {
              Serial.println("write failed");
            }

          }
        }
      }
    }
    BLE.scanForName("peripheral");
  }
}

Peripheral Sketch:

#include "ArduinoBLE.h"

BLEService gestureService("180A");
BLEUnsignedCharCharacteristic gestureCharacteristic("2A56", BLERead | BLEWrite);

uint8_t gesture = 66;

void setup()
{
  Serial.begin(115200);
  BLE.begin();
  BLE.debug(Serial);

  BLE.setLocalName("peripheral");
  BLE.setAdvertisedService(gestureService);
  gestureService.addCharacteristic(gestureCharacteristic);
  BLE.addService(gestureService);

  gestureCharacteristic.writeValue(gesture);
  BLE.advertise();
}

void loop()
{
  static unsigned long refTime = millis();

  BLEDevice central = BLE.central();
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);
    while (central.connected()) {

      if (gestureCharacteristic.written()) {
         auto val = gestureCharacteristic.value();
         Serial.print("command value:  ");
         Serial.println(val);
      }

    }
  }
}