How to transfer ArduinoBLE bytes data to int on Computer.

Hello Everyone,
I am a little new to ArduinoBLE Library. I have conducted some experiments and successfully collected current and velocity data via bluetooth (from Nano33 ble to Mac). However, I cannot find how to transfer these bytes to original int value. Does anyone know how to convert it back to integer values ?
Below is the abstract about how I sent my values:

BLEIntCharacteristic batteryLevelChar("2101",  BLERead | BLENotify);
BLEDevice central = BLE.central();

v_val = analogRead(A2) ; // 0-1024
cur_val = analogRead(A3);
batteryLevelChar.writeValue(cur_val);
batteryLevelChar.writeValue(v_val);

For example
I got <8500000>,<8900000>,<9500000>,<6f00000>, and I know this value is between 0~1024 ( probably around 300~500)

I have checked the ArduinoBLE Library, but I still cannot find how the integer is convert to byte arrays.
Any suggestions will be very helpful
Thank you very much.

0125Escon_blue.ino (5.86 KB)

Welcome to the forum

b05502045:
I got <8500000,8900000,9500000,6f00000>,

Where did you get these values? I suspect somewhere from your code on the MAC?

To find where your bytes are going just send an easy to recognize value e.g. 0x55AA44FF. This will also give you the byte order in case it is different on the MAC.

I recommend you have a look at the General guidance and How to use this forum Try to post your code in code tags instead of attaching a file. Many users here do not like downloading files or use tablets. Increases your chance of getting help. Should look like this.

Your example code

Also, please reduce examples to the minimum before posting. Makes it easier to help.

About your code.

If you define variables for pins, make use of it.
Remove wrong comments and reduce unnecessary comments. e.g.

// Used for reading DT signal
const int PinB = 3;
pinMode(3, INPUT);    // sets the digital pin 13 as output
// Used for reading DT signal
const int dtSignalPin = 3;
pinMode(dtSignalPin, INPUT);

or even better (no memory for variable needed and the naming convention tells everyone it is a constant)

#define DT_SIGNAL_PIN     3
pinMode(DT_SIGNAL_PIN, INPUT);

You must not use 16-bit UUIDs unless you have characteristics defined by the Bluetooth SiG. For your own services and characteristics you must use 128-bit random UUIDs. It looks like you looked at a few examples from this forum. Make use of defines for the UUIDs like in these examples.

Your variable naming convention is inconsistent. I would recommend to stay with the Arduino naming convention or at least stay with one.

I would also recommend to use more descriptive variable and function names to make your code more readable.

You need to look into data types:

  • micros() does not return a float
  • double is not a good data type for a counter they are usually integers of different sizes

Thank you for your reply! I really appreciate it.
1.

Klaus_K:
Welcome to the forum
Where did you get these values? I suspect somewhere from your code on the MAC?

To find where your bytes are going just send an easy to recognize value e.g. 0x55AA44FF. This will also give you the byte order in case it is different on the MAC.

For these values, I use the BlueSee App from MAC to receive the data. I think your solution it is possible. Thank you! But are there any insights about how the Library convert it to byte arrays? I found a post talking about using bit masks ( Link: android - Converting sections of byte arrays to integer values - Stack Overflow). Maybe the converting method is quite similar.

unsigned int val = 0xdeadbeef;

unsigned int mask1 = 0xC0000000;
unsigned int mask2 = 0x007F0000;

unsigned int YourValue = (val&mask1)>>23 | (val&mask2)>>16;

Also, the suggestions about codes are really helpful, I will try to make it clearer. I am sorry for the inconvenience about my post.
Thank you.

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