esp32经典蓝牙在VScode加idf的环境下发送数据示例代码
时间: 2024-01-04 22:04:16 浏览: 177
以下是一个简单的 ESP32 经典蓝牙发送数据的示例代码,使用 VSCode + ESP-IDF 的开发环境:
```c
#include <stdio.h>
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#define DEVICE_NAME "ESP32_BT"
static uint8_t peer_bdaddr[ESP_BD_ADDR_LEN];
static esp_bt_uuid_t uuid = {
.len = ESP_UUID_LEN_16,
.uuid = { .uuid16 = 0x1101 },
};
static void gap_callback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
esp_ble_gap_cb_param_t *scan_param = (esp_ble_gap_cb_param_t *)param;
if (scan_param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
memcpy(peer_bdaddr, scan_param->scan_rst.bda, ESP_BD_ADDR_LEN);
esp_ble_gap_stop_scan();
}
break;
}
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
esp_ble_gattc_open(gattc_if, peer_bdaddr, ESP_BLE_ADDR_TYPE_PUBLIC, true);
break;
case ESP_GAP_BLE_GATTC_OPEN_EVT: {
esp_ble_gattc_cb_param_t *gattc_param = (esp_ble_gattc_cb_param_t *)param;
if (gattc_param->status == ESP_GATT_OK) {
esp_gattc_char_elem_t char_elem = {
.uuid = uuid,
.char_prop = ESP_GATT_CHAR_PROP_BIT_WRITE,
};
esp_ble_gattc_get_char_by_uuid(gattc_if, gattc_param->conn_id, &uuid, &char_elem, 1);
}
break;
}
case ESP_GAP_BLE_GATTC_REG_EVT:
esp_ble_gap_set_scan_params(&scan_params);
break;
default:
break;
}
}
void app_main()
{
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT);
esp_bluedroid_init();
esp_bluedroid_enable();
esp_ble_gap_register_callback(gap_callback);
esp_ble_gattc_register_callback(gattc_callback);
esp_bt_dev_set_device_name(DEVICE_NAME);
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
while (true) {
esp_ble_gap_start_scan(ESP_BLE_SCAN_TYPE_ACTIVE, 0, NULL);
vTaskDelay(pdMS_TO_TICKS(10000)); // 每隔 10 秒发送一次数据
}
}
static void gattc_callback(esp_gattc_cb_event_t event, esp_gattc_cb_param_t *param)
{
switch (event) {
case ESP_GATTC_REG_EVT: {
esp_gattc_cb_param_t *reg_param = (esp_gattc_cb_param_t *)param;
esp_err_t err = esp_ble_gattc_get_service_by_uuid(gattc_if, reg_param->conn_id, &uuid);
if (err != ESP_OK) {
esp_ble_gattc_close(gattc_if, reg_param->conn_id);
}
break;
}
case ESP_GATTC_CONNECT_EVT: {
esp_gattc_cb_param_t *connect_param = (esp_gattc_cb_param_t *)param;
esp_ble_gattc_search_service(gattc_if, connect_param->conn_id, &uuid);
break;
}
case ESP_GATTC_SEARCH_RES_EVT: {
esp_gattc_cb_param_t *search_res_param = (esp_gattc_cb_param_t *)param;
esp_gatt_id_t char_id = {
.uuid = uuid,
.inst_id = 0,
};
esp_gattc_get_char_by_uuid(gattc_if, search_res_param->conn_id, &search_res_param->search_res.srvc_id,
&char_id, 1);
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT:
esp_ble_gattc_close(gattc_if, param->search_cmpl.conn_id);
break;
case ESP_GATTC_DISCONNECT_EVT:
esp_ble_gattc_close(gattc_if, param->disconnect.conn_id);
break;
case ESP_GATTC_WRITE_CHAR_EVT: {
esp_gattc_cb_param_t *write_char_param = (esp_gattc_cb_param_t *)param;
if (write_char_param->status != ESP_GATT_OK) {
esp_ble_gattc_close(gattc_if, write_char_param->write.conn_id);
}
break;
}
default:
break;
}
}
static void send_data(uint8_t *data, uint16_t len)
{
esp_gattc_char_elem_t char_elem = {
.uuid = uuid,
.char_prop = ESP_GATT_CHAR_PROP_BIT_WRITE,
};
esp_ble_gattc_write_char(gattc_if, 0, &char_elem, data, len, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
}
```
在 `gap_callback` 函数中,我们在每个循环中调用 `esp_ble_gap_start_scan` 来扫描蓝牙设备,找到目标设备后停止扫描并连接。在连接成功后,我们调用 `esp_ble_gattc_get_char_by_uuid` 来获取可写的特征值,并在 `gattc_callback` 中将数据写入该特征值。
注意:该示例仅提供参考,实际应用中需要根据具体情况进行修改。
阅读全文