ArduinoBLE - char.writeValue(buf, sizeof(buf) failing

Hi guys,

the char.writeValue() function frustrates me a bit.
Anyone having experience how to fix it? My code compiles and everything works fine but the IMU9DofPackage characteristic is never updated and also the writeValue() returns 0;

buf[45] = {0}
...
setup(){}
...
void loop()
{
  while(bleIsConntected())
  {
    if(buttonIsPressed()) {
      enableImu = true;
      Serial.println("IMU turned on");
    }
    while(enableImu) { 
      ICMupdate(&imuData);
      //Serial.println(imuData.aZ);
        // Encode the data using protobuf library
      pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
      bool encode_status = pb_encode(&stream, ImuData_fields, &imuData);
      if (!encode_status)
      {
        Serial.println("Failed to encode");
        return;
      }

      if(!IMU9DofPackage.writeValue(buf, sizeof(buf))){
        Serial.println("Failed to update Characteristic!");
      }
      if(buttonIsPressed()) {
        enableImu = false;
        Serial.println("IMU turned off");
      }
    }
  }

my BLE lib:

//.h
#ifndef EW_BLE_H
#define EW_BLE_H


#include <ArduinoBLE.h>

//extern BLEDevice central;
extern BLEService eWService;
extern BLECharacteristic IMU9DofPackage;
extern BLECharacteristic TotalInGram;
extern BLECharacteristic ButtonPress;


void BLEinit(const char* ID, int bufferSize, int valueSize = sizeof(uint16_t), int valueSizeButton = sizeof(uint16_t));

bool bleWaitForConnection();

bool bleIsConntected();

void updateArrCharacteristic(BLECharacteristic characteristic, const uint8_t arr[], int lengthOfArray);

void updateUint16_tCharacteristic(BLECharacteristic characteristic, const uint16_t value);

#endif

Please post complete code that compiles without errors.

I found the solution.

For some reason the BLECharacteristic definition has to be global inside the .ino. I do not quite understand why it would be different than global in my header file. Anyway that solved my problem. I could leave all the other definitions/functions and settings in my BLE.cpp/.h

So overall, be careful with exporting the ArduinoBLE functionalities in other files!

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