NXP MCU 技术william hill官网
直播中

carey123

10年用户 1521经验值
擅长:可编程逻辑 嵌入式技术
私信 关注
[问答]

如何将对象传递给BLE回调函数?

我正在为 esp_ble_gatts_register_callback 和 esp_ble_gap_register_callback 编写回调,使其成为新类的成员函数,但它们是静态的,我需要在回调中访问该类的成员对象。我希望将 this 指针作为参数传递,但寄存器函数需要特定的结构。有什么方法可以将会员数据放入回调中吗?

代码:全选
class BLEClass {
public:
    BLEClass(ISettings *settings) : settings(settings){}

    void initialize(void)
    {
        esp_err_t ret;

        ret = nvs_flash_init();
        esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
        ret = esp_bt_controller_init(&bt_cfg);
        ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
        ret = esp_bluedroid_init();
        ret = esp_bluedroid_enable();
        ret = esp_ble_gatts_register_callback(gatts_event_handler);
        ret = esp_ble_gap_register_callback(gap_event_handler);
        ret = esp_ble_gatts_app_register(PROFILE_A_APP_ID);

        esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);

        return;
    }

代码:全选
/**
* @brief GAP callback function type
* @param event : Event type
* @param param : Point to callback parameter, currently is union type
*/
typedef void (* esp_gap_ble_cb_t)(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);

/**
* @brief           This function is called to occur gap event, such as scan result
*
* @param[in]       callback: callback function
*
* @return
*                  - ESP_OK : success
*                  - other  : failed
*
*/
esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback);


显然,无法将某些用户参数与蓝牙回调相关联。然而,鉴于 SoC 中只有一个蓝牙外设,拥有它没有多大意义。

如何只使用一个全局变量来保存你的对象(单例),然后让 C 风格的回调函数调用该全局对象的适当成员函数?

更多回帖

发帖
×
20
完善资料,
赚取积分