Using BLECharacteristic.writeValue() for byte arrays

Hello,

I am trying to create a BLE characteristic so that it can store an array with 5 bytes. The code for BLE Nano 33 is attached. The goal is to use the Nano to control a 40-bit IO expander (5 Ports). I am using the writeValue(buffer,length) function to initialize the BLE characteristic, but it gives me following errors -

  1. PowerPatch_BLE:40:47: error: no matching function for call to BLEByteCharacteristic::writeValue(byte [5], int)’ IOPortCharacteristic.writeValue(PortValues,5);

  2. PowerPatch_BLE:76:50: error: cannot bind rvalue ‘(uint8_t)((byte*)(& PortValues))’ to ‘uint8_t& {aka unsigned char&}’

Any suggestions on how I can fix these issues? I wish the documentation for BLE library was improved with more examples.

Thanks guys!

#include <ArduinoBLE.h> 

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); 

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic IOPortCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN;
byte PortValues[5] = {0b00001111, 0, 60, 0 ,0};  // Ports 0, 1, 2, 3, 4

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

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

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

  // set advertised local name and service UUID:
  BLE.setLocalName("PowerPatch1");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(IOPortCharacteristic);
  
  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  IOPortCharacteristic.writeValue(PortValues,5);

  // start advertising
  BLE.advertise();

  Serial.println("BLE PowerPatch Peripheral");
  Serial.print("value size = ");
  Serial.println(IOPortCharacteristic.valueSize());

}

void loop() {
  // put your main code here, to run repeatedly:
  // 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());

    // 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 (IOPortCharacteristic.written()) {
        /*if (IOPortCharacteristic.value()) {   // 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
        }*/
        //byte portvalue = IOPortCharacteristic.value(); 
        //Serial.println(portvalue); 
        IOPortCharacteristic.readValue(PortValues); 
        Serial.println("Data received:"); 
        for (int i = 0; i < 5; i++){
          Serial.println(PortValues[i]);
        }       
      }
    }

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

BLEByteCharacteristic is for sending a single byte. If you want to send byte arrays, use BLECharacteristic.

When posting error messages, always post the full error message.

Pieter

Have a look at the ArduinoBLE Reference.

https://www.arduino.cc/en/Reference/ArduinoBLEBLECharacteristicBLECharacteristic

When you use BLECharacteristic you need to specify the size in bytes during the declaration as a third parameter. In your case its the same as the size of the array because you use bytes. Just in case you want to send something else.

morphel: I wish the documentation for BLE library was improved with more examples.

If you have a look at the following sub forum there are additional information and some extra examples in the posts.

Nano Boards (NEW types)

Thank you for the quick replies Pieter and Klaus. I followed your suggestions and redefined the function as BLEcharacteristic IOPortCharacteristic (UUID, BLERead | BLEWrite, 5). Also changed the read function to IOPortCharacteristic.readValue(PortValues,5). Its working perfectly now.

I will check out the Nano forum too. Thanks!