init: initial commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
#include "ble_scanner.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
#include "esp_log.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TAG "ble_scanner"
|
||||
|
||||
static ble_scanner_cb_t s_cb = NULL;
|
||||
|
||||
/* Passive scan: 100ms interval, 50ms window (50% duty cycle) */
|
||||
static esp_ble_scan_params_t s_scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_PASSIVE,
|
||||
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
||||
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
|
||||
.scan_interval = 0xA0, /* 100ms: 0xA0 * 0.625ms */
|
||||
.scan_window = 0x50, /* 50ms: 0x50 * 0.625ms */
|
||||
.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE,
|
||||
};
|
||||
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event,
|
||||
esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
|
||||
if (param->scan_param_cmpl.status == ESP_BT_STATUS_SUCCESS) {
|
||||
esp_ble_gap_start_scanning(0); /* 0 = scan indefinitely */
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Scan param set failed: %d", param->scan_param_cmpl.status);
|
||||
}
|
||||
break;
|
||||
|
||||
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
|
||||
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Scan start failed: %d", param->scan_start_cmpl.status);
|
||||
}
|
||||
break;
|
||||
|
||||
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
|
||||
esp_ble_gap_cb_param_t *p = param;
|
||||
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT && s_cb) {
|
||||
char tag_id[18];
|
||||
uint8_t *a = p->scan_rst.bda;
|
||||
snprintf(tag_id, sizeof(tag_id),
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
a[0], a[1], a[2], a[3], a[4], a[5]);
|
||||
s_cb(tag_id, p->scan_rst.rssi);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
||||
ESP_LOGI(TAG, "Scan stopped");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ble_scanner_init(ble_scanner_cb_t cb)
|
||||
{
|
||||
s_cb = cb;
|
||||
|
||||
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
|
||||
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
||||
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
||||
ESP_ERROR_CHECK(esp_bluedroid_init());
|
||||
ESP_ERROR_CHECK(esp_bluedroid_enable());
|
||||
|
||||
ESP_ERROR_CHECK(esp_ble_gap_register_callback(gap_event_handler));
|
||||
ESP_LOGI(TAG, "BLE scanner initialised");
|
||||
}
|
||||
|
||||
void ble_scanner_start(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting BLE scan");
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&s_scan_params));
|
||||
/* Scanning begins in the param-set callback once confirmed */
|
||||
}
|
||||
|
||||
void ble_scanner_stop(void)
|
||||
{
|
||||
esp_ble_gap_stop_scanning();
|
||||
}
|
||||
Reference in New Issue
Block a user