ESP32 BLE - iBeacon scan not working reliably

neilyoung
Posts: 12
Joined: Fri Mar 03, 2017 8:12 pm

ESP32 BLE - iBeacon scan not working reliably

Postby neilyoung » Sun Mar 12, 2017 6:13 pm

Hi,

I'm running the latest ESP-IDF SDK on a Sparkfun board. Whereas it seems to me, that "ordinary" BLE advertisements are seen correctly by the device, this is not the case with iBeacons. iBeacon scans just come in very scattered with absolutely nonsense RSSI values. For instance two consecutive scans from one and the same stationary iBeacon show RSSIs of -27 and -78. This is simply bullshit.

Anybody else making this experience?

neilyoung
Posts: 12
Joined: Fri Mar 03, 2017 8:12 pm

Re: ESP32 BLE - iBeacon scan not working reliably

Postby neilyoung » Mon Mar 13, 2017 1:05 pm

Maybe code sharing helps to come behind:

Here is the code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "bt.h"

#include "esp_blufi_api.h"
#include "esp_bt_defs.h"
#include "esp_gap_ble_api.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"

extern void bt_task(void *ignore);

static const char tag[] = "BLE";

static uint8_t ibeacon_prefix[] = {
    0x02,0x01,0x00,0x1A,0xFF,0x4C,0x00,0x02,0x15
};


void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
   esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;

   if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
      // Check for iBeacon adv prefix. Ignore 3rd byte, this varies from beacon type to beacon type
      p->scan_rst.ble_adv[2]  = 0x00;
      for (int i=0; i < sizeof(ibeacon_prefix); i++) {
         if (p->scan_rst.ble_adv[i] != ibeacon_prefix[i]) {
            return;
         }
      }
      ESP_LOGI(tag, "BDA: %02x:%02x:%02x:%02x:%02x:%02x, RSSI %d",
         p->scan_rst.bda[0],
         p->scan_rst.bda[1],
         p->scan_rst.bda[2],
         p->scan_rst.bda[3],
         p->scan_rst.bda[4],
         p->scan_rst.bda[5], 
         p->scan_rst.rssi);
   }
}


void bt_task(void *ignore) {
   esp_err_t ret;

   esp_bt_controller_init();

   ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
   if (ret) {
      ESP_LOGE(tag, "%s enable bt controller failed\n", __func__);
      goto end;
   }

   ret = esp_bluedroid_init();
   if (ret != ESP_OK) {
      ESP_LOGE(tag, "%s init bluedroid failed\n", __func__);
      goto end;
   }

   ret = esp_bluedroid_enable();
   if (ret) {
      ESP_LOGE(tag, "%s enable bluedroid failed\n", __func__);
      goto end;
   }
   ret = esp_ble_gap_register_callback(gap_event_handler);
   if (ret != ESP_OK) {
      ESP_LOGE(tag, "esp_ble_gap_register_callback: rc=%d", ret);
      goto end;
   }
   static esp_ble_scan_params_t ble_scan_params = {
      .scan_type              = BLE_SCAN_TYPE_ACTIVE,
      .own_addr_type          = ESP_PUBLIC_ADDR,
      .scan_filter_policy     = BLE_SCAN_FILTER_ALLOW_ALL,
      .scan_interval          = 0x50,
      .scan_window            = 0x30
   };
   ret = esp_ble_gap_set_scan_params(&ble_scan_params);
   if (ret != ESP_OK) {
      ESP_LOGE(tag, "esp_ble_gap_set_scan_params: rc=%d", ret);
      goto end;
   }

   ret = esp_ble_gap_start_scanning(60);
   if (ret != ESP_OK) {
      ESP_LOGE(tag, "esp_ble_gap_start_scanning: rc=%d", ret);
      goto end;
   }
   ESP_LOGI(tag, "Wait for scans...");
end:
   vTaskDelete(NULL);   
}


void app_main(void) {
   xTaskCreatePinnedToCore(&bt_task, "btTask", 2048, NULL, 5, NULL, 0);
}


The code is scanning for 60 seconds and it is examining incoming traces in order to accept just iBeacon advertisements. I have tried to vary esp_ble_scan_params_t to no avail.

Some info regarding the environment:

(29) boot: ESP-IDF v2.0-rc1-257-g4745895 2nd stage bootloader
I (29) boot: compile time 02:20:42
I (56) boot: Enabling RNG early entropy source...
I (56) boot: SPI Speed : 40MHz
I (56) boot: SPI Mode : DIO
I (59) boot: SPI Flash Size : 4MB
I (69) boot: Partition Table:
I (77) boot: ## Label Usage Type ST Offset Length
I (96) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (116) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (137) boot: 2 factory factory app 00 00 00010000 00100000
I (157) boot: End of partition table

This code running on a Sparkfun ESP32Thing does

Just displaying scattered scans from an Estimote Hardware beacon with weak battery like so:
I (325) BLE: Wait for scans...
I (2745) BLE: BDA: ea:19:6f:c1:48:39, RSSI -60
I (4585) BLE: BDA: ea:19:6f:c1:48:39, RSSI -58
I (5205) BLE: BDA: ea:19:6f:c1:48:39, RSSI -60
I (13205) BLE: BDA: ea:19:6f:c1:48:39, RSSI -61
I (15045) BLE: BDA: ea:19:6f:c1:48:39, RSSI -67
I (15655) BLE: BDA: ea:19:6f:c1:48:39, RSSI -67

Looks OK, but frequency of scans results is too low and RSSI is too high.

Does show this with an iPhone running an virtual beacon Estimote beacon:
I (6445) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6515) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6545) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (6605) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6635) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6695) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (6735) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6795) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6865) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (6895) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6955) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (6985) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (7045) BLE: BDA: ea:19:6f:c1:48:39, RSSI -59
I (7045) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (7115) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (7145) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (7255) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9
I (7305) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -8
I (7335) BLE: BDA: 5a:4d:31:43:d2:21, RSSI -9

Frequency is OK, IMHO, but RSSI is nonsense... The Sparkfun lies on top of the iPhone.

Does literally show NOTHING at all (OK, maybe one or two in a minute, if in luck), if scanning a RadBeacon Dot.

jkhax0r
Posts: 3
Joined: Mon Jan 23, 2017 6:54 am

Re: ESP32 BLE - iBeacon scan not working reliably

Postby jkhax0r » Tue Mar 21, 2017 4:17 am

Did you have any luck with this? I'm having some similar issues.

If I scan on my Android phone using the "nRF Connect" app, I can see advertisements coming from my device very regularly at ~25ms with nothing more than ~50ms or so.

Doing the same scan on ESP32 I get some random gaps of around 400ms or more.

I too have played with the scan parameters quite a bit. Including passive scanning. Also tried stopping and restarting scan every time I receive one of my advertisements, but still no luck.

neilyoung
Posts: 12
Joined: Fri Mar 03, 2017 8:12 pm

Re: ESP32 BLE - iBeacon scan not working reliably

Postby neilyoung » Tue Mar 21, 2017 5:33 am

@jkhax0r: Yes, it seems, there is something rolling. Seems, this forum is pretty uninformative. I never got any substantial answer to valid questions here. But I opened an issue here: https://github.com/espressif/esp-idf/issues/421

Right now the RSSI bug is confirmed and fixed. Just waiting for the appearance of the fix in one of the next ESP-IDF releases. Then I think the Expressif contributor in the github thread is purchasing one of the RadBeacons in order to come closer to the problem.

There is nothing we can do from client side right now.

Who is online

Users browsing this forum: Google [Bot] and 4 guests