BLE Direct Advertising to Android Device

mrrosen
Posts: 18
Joined: Tue Sep 26, 2017 12:53 am

BLE Direct Advertising to Android Device

Postby mrrosen » Fri Oct 26, 2018 6:30 pm

I am working on connecting an ESP32 over BLE (as a GATT Server; Peripheral Device, IDFv3.1) to an Android phone (Samsung Galaxy S6). I have gotten the connection and bonding to work fine; however, the ESP32 application frequency goes into deep sleep so the BLE connection must be reestablished with the bonded phone. I can do this just fine with now using:

Code: Select all

BluetoothDevice.connectGatt(context, true, callbacks); // Autoconnect allows the phone to passively scan for the device in the background and connect to it if seen
However, I would like to use ADV_DIRECT_IND and whitelist filtering of the advertisement on ESP32-side to improve security; but nothing Ive tried works. The phone simply cannot see the advertisement if I use an combination of ADV_DIRECT_IND_HIGH, ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST, ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST. See the code below:

Code: Select all

esp_ble_adv_params_t advertise = {
    .adv_int_min         = BT_BLE_MIN_ADV_INT_MS * BT_BLE_ADV_INT_FACTOR,
    .adv_int_max         = BT_BLE_MAX_ADV_INT_MS * BT_BLE_ADV_INT_FACTOR,
    .adv_type            = ADV_TYPE_DIRECT_IND_HIGH,
    .own_addr_type       = BLE_ADDR_TYPE_PUBLIC,
    .channel_map         = ADV_CHNL_ALL,
    .adv_filter_policy   = ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST,
  };
  int num = esp_ble_get_bond_device_num();
  esp_ble_bond_dev_t *peers = alloca(num * sizeof(esp_ble_bond_dev_t));

  if (!peers) {
    return ESP_ERR_NO_MEM;
  }

  if (esp_ble_get_bond_device_list(&num, peers) != ESP_OK) {
    return ESP_FAIL;
  }

  if ((peers[0].bond_key.key_mask & ESP_BLE_ID_KEY_MASK) != ESP_BLE_ID_KEY_MASK) {
    return ESP_FAIL;
  }

  memcpy(advertise.peer_addr, peers[0].bond_key.pid_key.static_addr, sizeof(esp_bd_addr_t));
  advertise.peer_addr_type = peers[0].bond_key.pid_key.addr_type;

  if (esp_ble_gap_start_advertising(&advertise) != ESP_OK) {
    return ESP_FAIL;
  }
  
I have explicitly tried to add the phone to the whitelist but since Android uses random addresses, Im not sure the correct way to handle this or if bonded devices are automatically added to the whitelist? What is the correct way to do ADV_DIRECT_IND with whitelist filtering?

mrrosen
Posts: 18
Joined: Tue Sep 26, 2017 12:53 am

Re: BLE Direct Advertising to Android Device

Postby mrrosen » Fri Oct 26, 2018 6:32 pm

I just want to note that above code works fine if adv_type is ADV_TYPE_IND and adv_filter_policy is ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY so nothing else in the system seems to be the problem.

MarioVLW
Posts: 1
Joined: Tue Jan 15, 2019 3:34 am

Re: BLE Direct Advertising to Android Device

Postby MarioVLW » Tue Jan 15, 2019 3:43 am

Hi mrrosen!
I know it's been a long time but I was wondering if you were able to do the ADV_TYPE_DIRECT_IND_HIGH connection with your phone. If you have some code example or a source, where I can find some useful information, would be really helpful.

Let me know if you know something!:)

mrrosen
Posts: 18
Joined: Tue Sep 26, 2017 12:53 am

Re: BLE Direct Advertising to Android Device

Postby mrrosen » Wed Jan 23, 2019 6:02 pm

MarioVLW wrote:
Tue Jan 15, 2019 3:43 am
Hi mrrosen!
I know it's been a long time but I was wondering if you were able to do the ADV_TYPE_DIRECT_IND_HIGH connection with your phone. If you have some code example or a source, where I can find some useful information, would be really helpful.

Let me know if you know something!:)
No luck yet, I assume my problem is a result of the bug briefly mentioned in this thread, so Im waiting for it to be resolved before trying anything else:
https://github.com/espressif/esp-idf/issues/2262

Who is online

Users browsing this forum: Baidu [Spider], noweare and 199 guests