BLE Direct Advertising to Android Device
Posted: 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:
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:
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?
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
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;
}