esp-idf icon indicating copy to clipboard operation
esp-idf copied to clipboard

BLE wrong GAP advertisement packet using raw data

Open HardwireIO opened this issue 9 years ago • 7 comments

I'm creating a new application, using latest sources of esp-idf.

We're setting raw GAP advertising data, in order to have full control on adv packet.

This is raw binary adv passed to function "esp_ble_gap_config_adv_data_raw":

0x0201060302FEFF0CFFE5027D240AC403B01C00A509094861726477697265

with a size of 30 bytes (less than max allowed value). Using both App "BLE Scanner" on Android and "LightBlue" on iOS, they detect an advertisement packet different, partially malformed.

Moreover, on App "BLE Scanner" I can print out raw adv data, and this is what I get:

0x0201060302FEFF0CFFE5027D240AC403B01C00A5090948617264776972650201060302FEFF0CFFE5027D240AC403B01C00A509094861726477697265

It looks like packet is doubled (don't know how because 31 should be the maximum size), but also App "LightBlue", that doesn't show raw data but can print manufacturer data, has a double size on manufacturer data field.

It looks like there's a sort of issue with raw data, because if I use esp_ble_gap_config_adv_data there's no such problem (but I'm not able to specify 16bit UUID services on adv packet)

HardwireIO avatar Mar 03 '17 17:03 HardwireIO

Found an interesting thing. This is my code:

esp_ble_gap_config_adv_data_raw(raw_adv_data, raw_adv_data_size); esp_ble_gap_config_scan_rsp_data_raw(raw_adv_data, raw_adv_data_size);

Issue seems to disapper if I comment the secondo line, related to scan response data. Shouldn't they be independent?

HardwireIO avatar Mar 03 '17 17:03 HardwireIO

Hi HardwireIO,

There are actually two packets that can be sent by BLE peripheral during advertising, each with up to 31 bytes of data. Hence you finding twice the data with the mentioned apps.

The first packet is the advertising data. this is set by esp_ble_gap_config_adv_data_raw(raw_adv_data, raw_adv_data_size);

The second packet is the scan response data. This is sent as a response to BLE Central devices (phones etc) when they perform an active scan. This is so that a peripheral can send more data to the phone before connection. This is set using esp_ble_gap_config_adv_data_raw(raw_adv_data, raw_adv_data_size);

lucashutchinson avatar Mar 05 '17 19:03 lucashutchinson

Hi lucashutchinson,

thanks for the explanation, but those 2 messages are independent each other (once send by device that advertises and once as an answer from a scan request).

In my case, if I set both the two messages (pointing to the same raw binary buffer), the advertising packet is corrupted and malformed.

Simply removing the esp_ble_gap_config_scan_rsp_data_raw call, I can get correct adv packet again.

HardwireIO avatar Mar 06 '17 17:03 HardwireIO

Hi HardwareIO,

Can you provide an example of what the data is set to in the esp32, then the data that you receive on the phone?

lucashutchinson avatar Mar 06 '17 18:03 lucashutchinson

HI HardwareIO,

If you got a linux machine which supports BLE (use a BLE dongle), you can use wireshark to capture HCI event and do raw data compare.

  1. capture on bluetooth device in wireshark and then send hcitool lescan to trigger local scan.
  2. find HCIEvent with opcode LEMeta, sub event code LE Adv Report, BT address is your ESP32 address.
  3. compare the data field with your raw data.

heyinling avatar Mar 07 '17 08:03 heyinling

HI HardwareIO, For each advertising event type, a corresponding Advertising Channel PDU is used. An advertising event can be one of the following types:

  1. a connectable undirected event (ADV_IND)
  2. a connectable directed event (ADV_DIRECT)
  3. a non-connectedable undirected event (ADV_NONCONN_IND)
  4. a scannable undirected event (ADV_SCAN_IND)

The advertising event type determines the allowable response PDUs:

  1. ADV_IND ------ Allowable SCAN_REQ:YES
  2. ADV_DIRECT_IND ------ Allowable SCAN_REQ:NO
  3. ADV_NONCONN_IND ------ Allowable SCAN_REQ:NO
  4. ADV_SCAN_IND ------ Allowable SCAN_REQ:YES

If ADV TYPE is ADV_IND or ADV_SCAN_IND, The advertiser will send Scan Rsp DATA after it received a SCAN_REQ .

There are two types of scanning ,determined by the Host:passive and active

  1. Passive Scanning:When in passive scanning , the Link Layer will only receive packets;it shall not send any packets(SCAN_REQ).
  2. Active Scanning:When in active scanning, the Link Layer shall listen for advertising PDUs and depending on the advertising PDU type it may request an advertier to send additional information(SCAN_RSP_DATA).

So, the packet you geted by "BLE Scanner" and "LightBlue" is ADV_DATA+SCAN_RSP_DATA . The premise condition that can get ADV_DATA+SCAN_RSP_DATA:

  1. ADV type : ADV_IND or ADV_SCAN_IND;
  2. SCAN type : Active SCAN
  3. Configuration SCAN RSP DATA(esp_ble_gap_config_scan_rsp_data_raw)

If you do not meet the above conditions,only the ADV_DATA will be geted.

xiewenxiang avatar Apr 21 '17 06:04 xiewenxiang

Hi HardwireIO, is this problem solved? Can we close this issue?

FayeY avatar May 02 '17 05:05 FayeY