BLE: can't receive Scan Response
Posted: Sun Dec 24, 2017 10:29 am
Hey all!
Anyone around the esp32 world ever managed to RX a BLE scan response in an esp32?
I advertise using bluez and can receive in another bluez host: (btmon output while running hcitool lescan)
...so I know my advertiser sends a scan response...
However, when I try to RX this type of event in an esp32: no luck. I'm able to receive regular scan result events but only of type ESP_BLE_EVT_CONN_ADV.
Here is my code:
...which outputs in make monitor:
I ALWAYS get SCAN_RESULT_EVT of type 0 but should from time to time receive a scan result of type 4 (ESP_BLE_EVT_SCAN_RSP) ...
Any ideas???
Please help me esp32 lovers!!!! (and a merry XMAS!!)
Anyone around the esp32 world ever managed to RX a BLE scan response in an esp32?
I advertise using bluez and can receive in another bluez host: (btmon output while running hcitool lescan)
Code: Select all
> HCI Event: LE Meta Event (0x3e) plen 28 #8 [hci0] 24.931439
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 00:C2:C6:D1:E8:44 (OUI 00-C2-C6)
Data length: 9
Name (complete): 60 c6 fd 3f 0a eb 03 03 ab
RSSI: -57 dBm (0xc7)
However, when I try to RX this type of event in an esp32: no luck. I'm able to receive regular scan result events but only of type ESP_BLE_EVT_CONN_ADV.
Here is my code:
Code: Select all
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "controller.h"
#include "driver/uart.h"
#include "bt.h"
#include "nvs_flash.h"
#include "esp_bt_device.h"
#include "esp_gap_ble_api.h"
#include "esp_bt_main.h"
#include "esp_system.h"
#include "btc_main.h"
static const char *MY_TAG = "BLE_PURE";
static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30
};
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
esp_err_t ret;
uint8_t *adv_data = NULL;
esp_ble_gap_cb_param_t *p_data = (esp_ble_gap_cb_param_t *) param;
ESP_LOGI(MY_TAG, "We're in the cb func to the gap module, event = %x", event); //event types: see esp_gap_ble_api.h
if (event == ESP_GAP_BLE_SCAN_RESULT_EVT) {
esp_log_buffer_hex(MY_TAG, param->scan_rst.bda, sizeof(esp_bd_addr_t)); //bdaddr
ESP_LOGI(MY_TAG, "SCAN_RESULT_EVT of type %x", p_data->scan_rst.ble_evt_type); //****I always get 0 ESP_BLE_EVT_CONN_ADV and should get ESP_BLE_EVT_SCAN_RSP sometimes *****
ESP_LOGI(MY_TAG, "adv_data_len = %i", p_data->scan_rst.adv_data_len);
adv_data = p_data->scan_rst.ble_adv;
printf("data: ");
for (int j = 0; j < p_data->scan_rst.adv_data_len; j++) {
printf("%02x ", adv_data[j]);
}
printf("\n");
}
if (event == ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT) {
ESP_LOGI(MY_TAG, "it's a SCAN_PARAM_SET_COMPLETE_EVT");
ESP_LOGI(MY_TAG, "+++++++status = %i ", p_data->scan_param_cmpl.status); //esp_bt_status_t
ret = esp_ble_gap_start_scanning(200); //duration in s
}
}
void app_main()
{
esp_err_t ret;
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
nvs_flash_init();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(MY_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM); //"Now only support BTDM" (components/bt/include/bt.h) donc si tu tentes BLE ça plante
if (ret) {
ESP_LOGE(MY_TAG, "%s enable controller failed\n", __func__);
return;
}
ESP_LOGI(MY_TAG, "%s init bluetooth", __func__);
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(MY_TAG, "%s init bluetooth failed", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(MY_TAG, "%s enable bluetooth failed\n", __func__);
return;
}
//register the scan callback function to the gap module
if ((ret = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) {
ESP_LOGE(MY_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_set_scan_params(&ble_scan_params);
ESP_LOGI(MY_TAG, "******set scan param returns = %i", ret); //will generate ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
}
Code: Select all
...
I (186338) BLE_PURE: We're in the cb func to the gap module, event = 3
I (186338) BLE_PURE: 00 c2 c6 d1 e8 44
I (186338) BLE_PURE: SCAN_RESULT_EVT of type 0
I (186348) BLE_PURE: adv_data_len = 14
data: 01 02 03 04 05 06 07 0e cb 05 0c 20 00 80
...
Any ideas???
Please help me esp32 lovers!!!! (and a merry XMAS!!)