Problem reading value from a Nano 33 BLE Sense

Hello,
This message was also posted on the Networks sub-boards. Being a beginner with Arduino, I try my few sketches, and I am currently facing a problem when trying to read a dummy value sent through bluetooth. Here is the sketch adapted from a LED example:

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

BLEByteCharacteristic fooCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", 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);
  }

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

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

  // add service
  BLE.addService(ledService);
  
  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  float testfloat = 3.14152;
  
  // 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()) {
      fooCharacteristic.writeValue((byte)testfloat);
    }

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

With this on the device, I can connect with gatttool on linux.

hms@joe:~/projects/arduino$ gatttool -I
[                 ][LE]> connect fc:57:4a:cb:ea:2e
Attempting to connect to fc:57:4a:cb:ea:2e
Connection successful
[fc:57:4a:cb:ea:2e][LE]> char-read-uuid 19B10001-E8F2-537E-4F6C-D104768A1214
Error: Read characteristics by UUID failed: Attribute PDU was invalid
[fc:57:4a:cb:ea:2e][LE]>

And so there is this error:

Error: Read characteristics by UUID failed: Attribute PDU was invalid

I can connect with LightBlue from Android, read a value, but it appears that the hex on binary value that is read does not correspond to the value that is written by the device.
What am I doing wrong here? Can it be a problem with my laptop (and me being dumb at reading the value on LightBlue)?
Thanks for yout help!

Please don't do that. It is called cross posting and forbidden by the forum rules to avoid wasting time of multiple users that try to help.

I read 0x03 in the LightBlue app, which is what I would expect.

Additionally, I would recommend the following two changes.

  • Do not write to the characteristic in a tight loop, e.g. use a millis() timer to limit the update rate.

  • Initialize the characteristics with a value before you start advertising.

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