乐鑫技术交流
直播中

张莹

8年用户 1108经验值
私信 关注
[问答]

ESP32如何获取到要连接的蓝牙uuid?

我现在使用esp32 wroom 32 烧写了 例程里面的gatt_client 但是我搜索到周边的蓝牙并且连接上,获取不到 对方的uuid
如何 获取到要连接的蓝牙uuid
我看例程里面是写死的uuid 我改成了要连接的蓝牙uuid 后能显示对方的uuid 但是我在实际使用的时候是不知道这个uuid  如何获取?
                                                                                                                                                                                                                       

回帖(1)

倪山骋

2024-6-24 17:02:03
要获取要连接的蓝牙设备的UUID,您可以按照以下步骤操作:

1. 首先,确保您的ESP32已经成功连接到目标蓝牙设备。这通常是通过扫描周围的蓝牙设备并选择一个设备进行连接实现的。

2. 连接到蓝牙设备后,您可以使用ESP32的GATT客户端功能来查询设备的服务和特征。这通常涉及到发送GATT发现服务的请求。

3. 当ESP32收到GATT发现服务的响应时,它将包含一个或多个服务的UUID。这些UUID可以用来识别设备上提供的不同服务。

4. 您可以将收到的UUID与您预期的UUID进行比较,以确定是否连接到了正确的设备。

以下是一个简化的示例代码,展示了如何使用ESP32的GATT客户端功能来查询设备的服务UUID:

```c
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "nvs_flash.h"
#include "bt_app_core.h"
#include "bt_app_api.h"

static const char *TAG = "GATT_CLIENT";

void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);

void app_main()
{
    // 初始化蓝牙和NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_CFG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret));
        return;
    }
    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_ble_gattc_app_register(0x5);
    if (ret) {
        ESP_LOGE(TAG, "%s gattc app register failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_ble_gattc_app_unregister(0x5);
    if (ret) {
        ESP_LOGE(TAG, "%s gattc app unregister failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_ble_gatts_app_unregister(0x5);
    if (ret) {
        ESP_LOGE(TAG, "%s gatts app unregister failed: %s", __func__, esp_err_to_name(ret));
        return;
    }

    esp_ble_gattc_register_callback(gattc_profile_event_handler);

    // 启动扫描和连接
    // ...
}

void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
    switch (event) {
        case ESP_GATTC_REG_EVT:
            ESP_LOGI(TAG, "REG EVT");
            break;
        case ESP_GATTC_OPEN_EVT:
            ESP_LOGI(TAG, "OPEN EVT");
            break;
        case ESP_GATTC_SEARCH_RES_EVT:
            ESP_LOGI(TAG, "SEARCH RES EVT");
            for (int i = 0; i < param->search_res.num_uuids; i++) {
                ESP_LOGI(TAG, "UUID16: %x", param->search_res.uuid16_list[i]);
            }
            break;
        // 其他事件处理
    }
}
```

在这个示例中,我们首先初始化蓝牙和NVS,然后注册GATT客户端应用程序。接下来,我们定义了一个事件处理函数`gattc_profile_event_handler`,该函数将在GATT客户端发生事件时被调用。在这个处理函数中,我们特别关注`ESP_GATTC_SEARCH_RES_EVT`事件,该事件会在发现服务时触发。在该事件的处理中,我们遍历返回的UUID列表,并将它们打印出来。

这样,您就可以在实际使用
举报

更多回帖

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