Bluetooth data: how to handle these data? I'm reading a BLE device and my api return me this struct

typedef struct {
  LGATTAddress bd_addr;
  int32_t rssi;
  uint8_t eir_len;
  uint8_t eir[256];
} LGATTDeviceInfo;


I try to print some eir_len and eir[256] and this is the result

  for(int i = 0; i &lt; 255; i++)
  {
    Serial.print(info.eir[i], HEX);
  }
  Serial.println();

  Serial.print("Advertising data lenght: ");
  Serial.print(info.eir_len);
  Serial.println();


    21613FF4C0CE8E1B87D83CB43431A739F4496E0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    Advertising data lenght: 23

    2167FF4C0102B00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    Advertising data lenght: 11

    216320A0109524543414D3532474A5830313235315125005002A0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    Advertising data lenght: 33


I cannot understand the relation between the air and the lenght: in the second example the response is 13 char but the lenght is 11. Why?

According to bluetooth specs..

The data is 240 octets and consists of a significant part and a non-significant part. The significant part contains a sequence of data structures. Each data structure shall have a length field of one octet, which contains the Length value, and a data field of Length octets. The first n octets of the data field contain the extended inquiry response (EIR) data type. The content of the remaining Length - n octets in the data field depends on the value of the EIR data type and is called the EIR data. The non-significant part extends the extended inquiry response to 240 octets and shall contain all-zero octets.



How can I parse the eir to obtain all the data? For example the name of the device and the manufacturer data?

update

Let's take for example this response

[LGATTC ino]dev address : [8:46:be:ca:2c:2] rssi [-63]

2 1 6 9 8 69 53 65 6E 73 6F 72 20 9 FF BE CA 2C 2 46 2 5F 5D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Advertising data lenght: 23


I try to convert the hex to string and this part 69 53 65 6E 73 6F 72 is the sensor name (iSensor). 

This is a bluetooth sensor that change in this way

&lt;be ca 2c 02 46 02 58 56&gt; - &gt; open
&lt;be ca 2c 02 46 00 59 55&gt; - &gt; close
&lt;be ca 2c 02 46 02 5a 58&gt; - &gt; open
&lt;be ca 2c 02 46 00 5b 57&gt; - &gt; close
&lt;be ca 2c 02 46 02 5c 5a&gt; - &gt; open
&lt;be ca 2c 02 46 00 5d 59&gt; - &gt; close
&lt;be ca 2c 02 46 02 5e 5c&gt; - &gt; open
&lt;be ca 2c 02 46 00 5f 5b&gt; - &gt; close


These are the manufacturer data: the first part be ca 2c 02 is the unique identifier of the device, the 02/00 identify the status of the sensor...

By the way I negin to understand... I notice now the match between the lenght and the eir!

I must now extract the name in char, the status and the device code!

edit 2
I found the specs of my sensors...
I cannot understand the relation between the air and the lenght: in the second example the response is 13 char but the lenght is 11. Why?


The hex numbers may be one or two digits long.  The way the values are being printed does not enable you to discern between 1 2 3, 12 3 and 1 23. Try this:

for(int i = 0; i &lt; 255; i++)
{
  Serial.print(info.eir[i], HEX);
  Serial.print(" ");
}


(Someone already told you this.)




  How can I parse the eir to obtain all the data? For example the name of the device and the manufacturer data?


This works for the only example of the data you have given.  If I have guessed correctly how further data will follow, it'll work for them too.  (If I haven't, it won't.)  I have assumed that the name always starts in the same place, ends with a space, three bytes of stuff we don't care about, and that the data is groups of 8 bytes.  It's very likely that some of the other bytes give details of the data to follow, but with only one example and no spec, it's basically impossible to tell.

// Let's assume the sensor name always starts at element 5...
char *sensorName = &amp;(info.eir[5]);
// ...and ends on a 0x20 (space) - which is probably a risky assumption
char *end = strchr(sensorName, ' ');
if (end) 
{
    *end = '\0';
    // Sensor name is now in sensorName

    // Let's assume the data starts exactly 4 bytes after the end of the name
    uint8_t *dataItemStart = (uint8_t*)(end + 3);
    uint8_t *dataEnd = info.eir + info.eir_len - 1;
    while(dataItemStart &lt; dataEnd)
    {
        // We have a data item here, as 'dataItemStart'.
        // You can get bytes within it using, for example
        //   dataItemStart[5]
        // to get the 6th item (the 00 or 02 in your examples)

        // Each data item is 8 bytes long, so the next one starts 8 bytes later 
        dataItemStart += 8;
    }
}