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 < 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.

Here is the eir

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

<be ca 2c 02 46 02 58 56> - > open
<be ca 2c 02 46 00 59 55> - > close
<be ca 2c 02 46 02 5a 58> - > open
<be ca 2c 02 46 00 5b 57> - > close
<be ca 2c 02 46 02 5c 5a> - > open
<be ca 2c 02 46 00 5d 59> - > close
<be ca 2c 02 46 02 5e 5c> - > open
<be ca 2c 02 46 00 5f 5b> - > 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...

enter image description here

  • Please show your code which is doing the printing. – Mark Smith Feb 12 '17 at 11:48
  • @MarkSmith added! – Roberto Pezzali Feb 12 '17 at 12:19
  • Could you indicate what format you'd like this processed into, please? – Mark Smith Feb 12 '17 at 14:39
  • Just the name is char and the manufacturer data – Roberto Pezzali Feb 12 '17 at 15:55
  • Do you know how arrays work in C(++)? And do you have a spec, please? I can show you code which works for this example, but with only one example I have no way to generalise it. – Mark Smith Feb 12 '17 at 16:44
up vote 1 down vote accepted

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 < 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 = &(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 < 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;
    }
}
  • thank you, I notie now the Paul response! I begin to understand... I add the data I know from a sensor I know! – Roberto Pezzali Feb 12 '17 at 13:19
  • Thank you so much. I also find the specs of the sensors (I edited the question). I understand that I have 0x08 and then the name (with the space at the end) and 0xFF and then the data. I will try to adapt the code. – Roberto Pezzali Feb 13 '17 at 9:33
  • That makes much more sense now, with the spec. All the concepts you need should be there in the code above, though. Feel free to accept and optionally upvote the answer if it helped :-) – Mark Smith Feb 13 '17 at 9:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.