BLE HCI 组件

[English]

BLE HCI 组件用于通过 VHCI 接口直接操作 BLE Controller 实现广播,扫描等功能。 相比于通过 Nimble 或 Bluedroid 协议栈发起广播和扫描,使用该组件有如下优点: - 更少的内存占用 - 更小的固件尺寸 - 更快的初始化流程

BLE HCI 使用方法

对于广播应用: 1.初始化 BLE HCI: 使用 ble_hci_init() 函数进行初始化。 2.设定本机随机地址(可选): 如果需要使用随机地址作为广播地址,使用:cpp:func:ble_hci_set_random_address`函数进行设定。 3.配置广播参数:使用 :cpp:func:`ble_hci_set_adv_param 配置广播参数。 4.配置广播数据:使用 ble_hci_set_adv_data() 设定需要广播的数据内容。 5.开始广播: 使用 ble_hci_set_adv_enable()

对于扫描应用: 1.初始化 BLE HCI: 使用 ble_hci_init() 函数进行初始化。 2.配置扫描参数:使用 ble_hci_set_scan_param() 配置扫描参数。 3.使能meta事件: 使用 ble_hci_enable_meta_event() 使能中断事件。 4.注册扫描事件函数: 使用 ble_hci_set_register_scan_callback() 注册中断事件。 5.开始扫描: 使用 ble_hci_set_scan_enable()

API 参考

Header File

Functions

esp_err_t ble_hci_init(void)

BLE HCI initialization.

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_deinit(void)

BLE HCI de-initialization.

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_reset(void)

BLE HCI reset controller.

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_enable_meta_event(void)

Enable BLE HCI meta event.

返回

esp_err_t

esp_err_t ble_hci_set_adv_param(ble_hci_adv_param_t *param)

Set BLE HCI advertising parameters.

参数

param – : advertising parameters

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_adv_data(uint8_t len, uint8_t *data)

Set BLE HCI advertising data.

参数
  • len – : advertising data length

  • data – : advertising data

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_adv_enable(bool enable)

Set BLE HCI advertising enable.

参数

enable – : true for enable advertising

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_scan_param(ble_hci_scan_param_t *param)

Set BLE HCI scan parameters.

参数

param – : scan parameters

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_scan_enable(bool enable, bool filter_duplicates)

Set BLE HCI scan enable.

参数
  • enable – : enable or disable scan

  • filter_duplicates – : filter duplicates or not

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_register_scan_callback(ble_hci_scan_cb_t cb)

Set BLE HCI scan callback.

参数

cb – : scan callback function pointer

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_add_to_accept_list(ble_hci_addr_t addr, ble_hci_addr_type_t addr_type)

Add BLE white list.

参数
  • addr – : address to be added to white list

  • addr_type – : address type to be added to white list

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_clear_accept_list(void)

Clear BLE white list.

返回

esp_err_t

  • ESP_OK: succeed

  • others: fail

esp_err_t ble_hci_set_random_address(ble_hci_addr_t addr)

Set BLE owner address.

参数

addr – : owner address

返回

  • ESP_OK: succeed

  • others: fail

Structures

struct ble_hci_scan_result_t

BLE scan result struct.

Public Members

ble_hci_search_evt_t search_evt

Search event type

ble_hci_dev_type_t dev_type

Device type

ble_hci_addr_t bda

Bluetooth device address which has been searched

ble_hci_addr_type_t ble_addr_type

Ble device address type

uint8_t ble_adv[ESP_BLE_ADV_DATA_LEN_MAX + ESP_BLE_SCAN_RSP_DATA_LEN_MAX]

Received EIR

uint8_t adv_data_len

Adv data length

uint8_t scan_rsp_len

Scan response length

int rssi

Searched device’s RSSI

struct ble_hci_adv_param_t

Ble adv parameters.

Public Members

uint16_t adv_int_min

Time = N × 0.625 ms Range: 0x0020 to 0x4000

uint16_t adv_int_max

Time = N × 0.625 ms Range: 0x0020 to 0x4000

ble_hci_adv_type_t adv_type

Advertising Type

ble_hci_addr_type_t own_addr_type

Own Address Type

ble_hci_addr_t peer_addr

Peer device bluetooth device address

ble_hci_addr_type_t peer_addr_type

Peer device bluetooth device address type, only support public address type and random address type

ble_hci_adv_channel_t channel_map

Advertising channel map

ble_hci_adv_filter_t adv_filter_policy

Advertising Filter Policy:

struct ble_hci_scan_param_t

Ble sccan parameters.

Public Members

ble_hci_scan_type_t scan_type

Scan Type

uint16_t scan_interval

Time = N × 0.625 ms Range: 0x0004 to 0x4000

uint16_t scan_window

Time = N × 0.625 ms Range: 0x0004 to 0x4000

ble_hci_addr_type_t own_addr_type

Own Address Type

ble_hci_adv_filter_t filter_policy

Scanning Filter Policy

Macros

ESP_BLE_ADV_DATA_LEN_MAX

Advertising data maximum length.

ESP_BLE_SCAN_RSP_DATA_LEN_MAX

Scan response data maximum length.

BLE_HCI_ADDR_LEN

BLE Address Length.

Type Definitions

typedef uint8_t ble_hci_addr_t[BLE_HCI_ADDR_LEN]

Bluetooth device address.

typedef void (*ble_hci_scan_cb_t)(ble_hci_scan_result_t *scan_result, uint16_t result_len)

BLE HCI scan callback function type.

Param scan_result

: ble advertisement scan result

Param result_len

: length of scan result

Enumerations

enum ble_hci_search_evt_t

Sub Event of BLE_HCI_BLE_SCAN_RESULT_EVT.

Values:

enumerator BLE_HCI_SEARCH_INQ_RES_EVT

Inquiry result for a peer device.

enumerator BLE_HCI_SEARCH_INQ_CMPL_EVT

Inquiry complete.

enumerator BLE_HCI_SEARCH_DISC_RES_EVT

Discovery result for a peer device.

enumerator BLE_HCI_SEARCH_DISC_BLE_RES_EVT

Discovery result for BLE GATT based service on a peer device.

enumerator BLE_HCI_SEARCH_DISC_CMPL_EVT

Discovery complete.

enumerator BLE_HCI_SEARCH_DI_DISC_CMPL_EVT

Discovery complete.

enumerator BLE_HCI_SEARCH_SEARCH_CANCEL_CMPL_EVT

Search cancelled

enumerator BLE_HCI_SEARCH_INQ_DISCARD_NUM_EVT

The number of pkt discarded by flow control

enum ble_hci_addr_type_t

BLE address type.

Values:

enumerator BLE_ADDR_TYPE_PUBLIC

Public Device Address

enumerator BLE_ADDR_TYPE_RANDOM

Random Device Address.

enumerator BLE_ADDR_TYPE_RPA_PUBLIC

Resolvable Private Address (RPA) with public identity address

enumerator BLE_ADDR_TYPE_RPA_RANDOM

Resolvable Private Address (RPA) with random identity address.

enum ble_hci_dev_type_t

Bluetooth device type.

Values:

enumerator BLE_HCI_DEVICE_TYPE_BREDR
enumerator BLE_HCI_DEVICE_TYPE_BLE
enumerator BLE_HCI_DEVICE_TYPE_DUMO
enum ble_hci_adv_type_t

BLE advertising type.

Values:

enumerator ADV_TYPE_IND
enumerator ADV_TYPE_DIRECT_IND_HIGH
enumerator ADV_TYPE_SCAN_IND
enumerator ADV_TYPE_NONCONN_IND
enumerator ADV_TYPE_DIRECT_IND_LOW
enum ble_hci_adv_channel_t

Advertising channel mask.

Values:

enumerator ADV_CHNL_37
enumerator ADV_CHNL_38
enumerator ADV_CHNL_39
enumerator ADV_CHNL_ALL
enum ble_hci_scan_type_t

Ble scan type.

Values:

enumerator BLE_SCAN_TYPE_PASSIVE

Passive scan

enumerator BLE_SCAN_TYPE_ACTIVE

Active scan

enum ble_hci_adv_filter_t

Ble adv filteer type.

Values:

enumerator ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY

Allow both scan and connection requests from anyone.

enumerator ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY

Allow both scan req from White List devices only and connection req from anyone.

enumerator ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST

Allow both scan req from anyone and connection req from White List devices only.

enumerator ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST

Allow scan and connection requests from White List devices only.