Sending multiple floats over BLE

Hello,

I'm trying to make a sketch that sends three floats over BLE to an Android device. The three floats are converted into an array which holds every single bytes of the three floats. The problem is that on my Android device the hexadecimal values aren't enteriley correctley. The values visible in the app are: (0x) C3-F5-48-40-52-B8-16-41-00-00-00-00.
This is my code:

#include <ArduinoBLE.h>

float value1 = 3.14;
float value2 = 6.28;
float value3 = 9.42;

byte packetArray[12] = {
  ((uint8_t*)&value1)[0],
  ((uint8_t*)&value1)[1],
  ((uint8_t*)&value1)[2],
  ((uint8_t*)&value1)[3],
  ((uint8_t*)&value2)[4],
  ((uint8_t*)&value2)[5],
  ((uint8_t*)&value2)[6],
  ((uint8_t*)&value2)[7],
  ((uint8_t*)&value3)[8],
  ((uint8_t*)&value3)[9],
  ((uint8_t*)&value3)[10],
  ((uint8_t*)&value3)[11],
};

BLEService dataService("180C"); 

BLECharacteristic newDataCharacteristic("2A56", BLERead | BLENotify, sizeof(packetArray), true);

void setup() {

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

  BLE.setLocalName("receiverUnit");
  
  dataService.addCharacteristic(newDataCharacteristic);
  
  BLE.addService(dataService);
  
  newDataCharacteristic.setValue(packetArray, sizeof(packetArray));

  BLE.advertise();
}
void loop() {
  BLEDevice device = BLE.central();
  
  while (device.connected()){
    // Do nothing
  }
}

C3-F5-48-40 does indeed represent value1. However, 52-B8-16-41 doesn't represent value2, but value3 and value2 somehow disappeared. Does anyone know why this is happening, what I am doing wrong or is what I'm tryting to do even impossible with BLE?

The indexes should just stay between 0 and 3


byte packetArray[12] = {
  ((uint8_t*)&value1)[0],
  ((uint8_t*)&value1)[1],
  ((uint8_t*)&value1)[2],
  ((uint8_t*)&value1)[3],
  ((uint8_t*)&value2)[0],
  ((uint8_t*)&value2)[1],
  ((uint8_t*)&value2)[2],
  ((uint8_t*)&value2)[3],
  ((uint8_t*)&value3)[0],
  ((uint8_t*)&value3)[1],
  ((uint8_t*)&value3)[2],
  ((uint8_t*)&value3)[3],
};

As you get access to the bytes from each float in memory.

Having the 3 floats in a structure and sending the structure would make it easier…

It works now. I confused the indexes with the size of the array. Thanks!

Great. Have fun

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