Buenas tardes, se trata de un problema que llevo semanas intentando solucionar y no logro encontrar información al respecto.
Actualmente, estoy trabajando con un Arduino 33 BLE y la librería Arduino BLE. El caso, es que no consigo enviar ningún tipo de array (byte, char...) y mucho menos un string (que es el propósito final, mediante (char*)). He realizado muchos intentos consultando la documentación proporcionada por Arduino, pero es escasa al respecto y solo consigo información de comunicación Bluetooth BLE relacionada con módulos externos.
Adjunto un ejemplo de prueba realizada al respecto y sin ningún éxito:
#include <ArduinoBLE.h>
// BLE rfid Service
BLEService rfidService("FFE0");
// BLE rfid Characteristic
BLECharCharacteristic rfid_Label ("FFE1", BLERead | BLENotify);
void setup() {
Serial.begin(9600); // initialize serial communication
initBLE();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
update_RfidLabelNumber();
// wait for a BLE central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
while (central.connected()) {
update_RfidLabelNumber();
}
}
else{
Serial.println("ERROR DE CONEXIÓN BLUETOOTH");
}
}
void update_RfidLabelNumber() {
char result[] = "Communication";
const size_t dataLength = sizeof(result) + 1;
rfid_Label.writeValue((char*)result, dataLength);
}
void initBLE()
{
// begin initialization
if (!BLE.begin())
{
while (1)
{
Serial.println("starting BLE failed!");
delay(1000);
}
}
BLE.setLocalName("Rfid_ISO");
BLE.setAdvertisedService(rfidService); // add the service UUID
rfidService.addCharacteristic(rfid_Label); // add the rfid characteristic
BLE.addService(rfidService); // Add the rfid service
// start advertising
BLE.advertise();
}
Este es el error devuelto:
no matching function for call to 'BLECharCharacteristic::writeValue(char*, const size_t&)'
en la línea 34:
rfid_Label.writeValue((char*)result, dataLength);
He realizado pruebas sin indicar el Buffer pero remite error de conversión (char*) a (char), tampoco funciona indicar el tamaño del buffer en formato int.
Muchas gracias por el interés y las posibles ayudas.
Gracias Jose, pero eso me remite el mismo error y si quito la longitud del buffer, me remite error de conversion del array al formato elegido en BLE Characteristic.
Me he fiado de lo que has puesto ,pero si miras en la referencia de la pagina de arduino ,puedes llamar a esa funcion con uno o dos parametros: bleCharacteristic.writeValue(buffer, length)
Para enviar un array de bytes
bleCharacteristic.writeValue(value)
para enviar un solo byte
y justo debajo indica que el array pasado ,debe ser de tipo byte....
Parameters
buffer: byte array to write value with
length: number of bytes of the buffer argument to write
value: value to write
En teoría debería de funcionar, pero no hay manera. En este ejemplo lo he intentado con tipo byte e indicando longitud del buffer y sigue devolviendo el mismo error:
Pues resulta que al final si era falta de documentación sobre la librería Arduino BLE. Comparto el enlace donde encontré la solución y os resumo para quién pueda serle útil.
La solución está en "BLEStringCharacteristic" que no viene definida como una opción en la documentación de la librería Arduino BLE (al menos a día de hoy, espero que lo solucionen). Es necesario declarar la característica como String e indicar la longitud máxima del mismo cuando declaramos la propia característica (en este caso una cadena con un máximo de 20 carácteres):
Para evitar problemas en la comunicación es recomendable dejar, al menos, dos espacios más de la longitud de la cadena para que al ser enviada pueda ser indicado (automáticamente por Arduino) el final de la cadena.
Después, solo hay que declarar un String y enviarlo sin indicar longitud de buffer.
Adjunto código de ejemplo que funciona correctamente:
#include <ArduinoBLE.h>
// BLE rfid Service
BLEService rfidService("FFE0");
// BLE rfid Characteristic
BLEStringCharacteristic rfid_Label ("FFE1", BLERead | BLENotify, 20);
void setup() {
Serial.begin(9600); // initialize serial communication
initBLE();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
update_RfidLabelNumber();
// wait for a BLE central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
while (central.connected()) {
update_RfidLabelNumber();
}
}
else{
Serial.println("ERROR DE CONEXIÓN BLUETOOTH");
}
}
void update_RfidLabelNumber() {
String result = "SOLUCIONADO";
rfid_Label.writeValue(result);
}
void initBLE()
{
// begin initialization
if (!BLE.begin())
{
while (1)
{
Serial.println("starting BLE failed!");
delay(1000);
}
}
BLE.setLocalName("Rfid_ISO");
BLE.setAdvertisedService(rfidService); // add the service UUID
rfidService.addCharacteristic(rfid_Label); // add the rfid characteristic
BLE.addService(rfidService); // Add the rfid service
// start advertising
BLE.advertise();
}
Perfecto!! La verdad es que yo solo uso bluetooth clasico,BLE me parece interesante si necesitas poco consumo pero a nivel de programacion es mucho mas lioso y complicado.