{"id":76373,"date":"2026-02-22T10:41:38","date_gmt":"2026-02-22T02:41:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76373.html"},"modified":"2026-02-22T10:41:38","modified_gmt":"2026-02-22T02:41:38","slug":"esp32%e8%93%9d%e7%89%99%e5%bc%80%e5%8f%91%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e9%9b%b6%e6%90%ad%e5%bb%bagatt%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%88%e5%9f%ba%e4%ba%8eesp-idf-4-4-vscode%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76373.html","title":{"rendered":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09"},"content":{"rendered":"<h2>ESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u6df1\u5ea6\u5b9e\u6218&#xff1a;\u6784\u5efa\u7a33\u5b9a\u53ef\u9760\u7684\u4f4e\u529f\u8017\u7269\u8054\u7f51\u6570\u636e\u901a\u9053<\/h2>\n<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4e2d\u63a7\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u4f5c\u4e3a\u6570\u636e\u67a2\u7ebd&#xff0c;\u901a\u8fc7\u84dd\u7259\u4e0e\u591a\u4e2a\u4f20\u611f\u5668\u8282\u70b9\u901a\u4fe1\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u6848\u4f8b&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565\u53ea\u8bb2\u4e2a\u201cHello World\u201d&#xff0c;\u8981\u4e48\u4ee3\u7801\u5806\u780c\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u6298\u817e\u4e86\u4e24\u5468&#xff0c;\u8e29\u4e86\u65e0\u6570\u5751\u4e4b\u540e&#xff0c;\u6211\u51b3\u5b9a\u628a\u6574\u4e2aESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u7684\u6784\u5efa\u8fc7\u7a0b\u91cd\u65b0\u68b3\u7406\u4e00\u904d&#xff0c;\u7279\u522b\u662f\u90a3\u4e9b\u5b98\u65b9\u4f8b\u7a0b\u6ca1\u660e\u8bf4\u3001\u4f46\u5b9e\u9645\u5f00\u53d1\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5f00\u53d1\u9700\u8981\u84dd\u7259\u901a\u4fe1\u7684\u7269\u8054\u7f51\u8bbe\u5907\u2014\u2014\u65e0\u8bba\u662f\u73af\u5883\u76d1\u6d4b\u4f20\u611f\u5668\u3001\u53ef\u7a7f\u6234\u8bbe\u5907&#xff0c;\u8fd8\u662f\u667a\u80fd\u5bb6\u5c45\u63a7\u5236\u5668\u2014\u2014\u8fd9\u7bc7\u6587\u7ae0\u5e94\u8be5\u80fd\u5e2e\u4f60\u5c11\u8d70\u4e0d\u5c11\u5f2f\u8def\u3002\u6211\u4f1a\u4ece\u6700\u57fa\u7840\u7684\u73af\u5883\u642d\u5efa\u8bb2\u8d77&#xff0c;\u9010\u6b65\u6df1\u5165\u5230\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u3001\u4e8b\u4ef6\u5904\u7406\u4f18\u5316\u3001\u529f\u8017\u63a7\u5236\u7b49\u5b9e\u6218\u5c42\u9762&#xff0c;\u4e0d\u4ec5\u4ec5\u662f\u4ee3\u7801\u590d\u5236\u7c98\u8d34&#xff0c;\u66f4\u91cd\u8981\u7684\u662f\u7406\u89e3\u6bcf\u4e2a\u6b65\u9aa4\u80cc\u540e\u7684\u8bbe\u8ba1\u903b\u8f91\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h3>1. \u5f00\u53d1\u73af\u5883\u914d\u7f6e\u4e0e\u9879\u76ee\u521d\u59cb\u5316\u7b56\u7565<\/h3>\n<p>\u5f88\u591a\u5f00\u53d1\u8005\u7b2c\u4e00\u6b65\u5c31\u5361\u5728\u73af\u5883\u914d\u7f6e\u4e0a\u3002\u867d\u7136ESP-IDF\u5b98\u65b9\u63d0\u4f9b\u4e86\u591a\u79cd\u5b89\u88c5\u65b9\u5f0f&#xff0c;\u4f46\u6839\u636e\u6211\u7684\u7ecf\u9a8c&#xff0c;\u5728VSCode\u4e2d\u4f7f\u7528ESP-IDF\u63d2\u4ef6\u662f\u6700\u7a33\u5b9a\u9ad8\u6548\u7684\u9009\u62e9&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u8c03\u8bd5\u7684\u84dd\u7259\u9879\u76ee\u3002<\/p>\n<p>\u9996\u5148\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86Python 3.8\u6216\u66f4\u9ad8\u7248\u672c\u3002\u6253\u5f00VSCode&#xff0c;\u5728\u6269\u5c55\u5546\u5e97\u641c\u7d22\u201cESP-IDF\u201d\u5e76\u5b89\u88c5\u5b98\u65b9\u63d2\u4ef6\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d&#xff0c;\u63d2\u4ef6\u4f1a\u5f15\u5bfc\u4f60\u5b8c\u6210ESP-IDF\u6846\u67b6\u7684\u4e0b\u8f7d\u548c\u914d\u7f6e\u3002\u8fd9\u91cc\u6709\u4e2a\u5173\u952e\u70b9&#xff1a;\u4e00\u5b9a\u8981\u9009\u62e9ESP-IDF 4.4\u6216\u66f4\u9ad8\u7248\u672c&#xff0c;\u56e0\u4e3a\u65e9\u671f\u7248\u672c\u5728\u84dd\u7259\u534f\u8bae\u6808\u7684\u7a33\u5b9a\u6027\u548c\u529f\u80fd\u5b8c\u6574\u6027\u4e0a\u5b58\u5728\u4e0d\u5c11\u95ee\u9898\u3002<\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee&#xff1a;<\/p>\n<p>idf.py create-project bluetooth_gatt_server<br \/>\ncd bluetooth_gatt_server<\/p>\n<p>\u9879\u76ee\u7ed3\u6784\u521d\u59cb\u5316\u540e&#xff0c;\u9700\u8981\u4fee\u6539CMakeLists.txt\u6587\u4ef6&#xff0c;\u6dfb\u52a0\u84dd\u7259\u7ec4\u4ef6\u4f9d\u8d56&#xff1a;<\/p>\n<p>set(COMPONENTS<br \/>\n    main<br \/>\n    bt<br \/>\n    nvs_flash<br \/>\n    esp_bt<br \/>\n)<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5f88\u591a\u6559\u7a0b\u4f1a\u5ffd\u7565nvs_flash\u7ec4\u4ef6&#xff0c;\u4f46\u84dd\u7259\u914d\u7f6e\u4fe1\u606f\u9700\u8981\u6301\u4e45\u5316\u5b58\u50a8&#xff0c;\u5426\u5219\u6bcf\u6b21\u91cd\u542f\u8bbe\u5907\u90fd\u9700\u8981\u91cd\u65b0\u914d\u5bf9&#xff0c;\u7528\u6237\u4f53\u9a8c\u4f1a\u5f88\u5dee\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u914d\u7f6e\u9879\u76ee\u53c2\u6570\u3002\u8fd0\u884cidf.py menuconfig&#xff0c;\u8fdb\u5165\u914d\u7f6e\u754c\u9762\u540e&#xff0c;\u91cd\u70b9\u5173\u6ce8\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206&#xff1a;<\/p>\n<ul>\n<li>Component config \u2192 Bluetooth \u2192 Bluetooth controller mode&#xff1a;\u9009\u62e9BR\/EDR\/BLE\/DUALMODE&#xff08;\u6839\u636e\u4f60\u7684\u9700\u6c42&#xff0c;\u5982\u679c\u53ea\u9700\u8981BLE&#xff0c;\u9009\u62e9BLE only\u53ef\u4ee5\u8282\u7701\u5185\u5b58&#xff09;<\/li>\n<li>Component config \u2192 Bluetooth \u2192 Bluetooth Host&#xff1a;\u786e\u4fddBluedroid Enabled\u88ab\u9009\u4e2d<\/li>\n<li>Component config \u2192 Bluetooth \u2192 Bluedroid Options&#xff1a;\u6839\u636e\u8bbe\u5907\u5185\u5b58\u5927\u5c0f\u8c03\u6574BT\/BLE DU memory size&#xff0c;\u5bf9\u4e8e\u590d\u6742\u670d\u52a1\u5efa\u8bae\u8bbe\u7f6e\u4e3a35000\u4ee5\u4e0a<\/li>\n<\/ul>\n<p>\u73af\u5883\u914d\u7f6e\u5b8c\u6210\u540e&#xff0c;\u5148\u7f16\u8bd1\u4e00\u4e2a\u7a7a\u9879\u76ee\u6d4b\u8bd5\u73af\u5883\u662f\u5426\u6b63\u5e38&#xff1a;<\/p>\n<p>idf.py build<\/p>\n<p>\u5982\u679c\u7f16\u8bd1\u6210\u529f&#xff0c;\u8bf4\u660e\u57fa\u7840\u73af\u5883\u5df2\u7ecf\u5c31\u7eea\u3002\u8fd9\u91cc\u6211\u5efa\u8bae\u5728\u7ee7\u7eed\u4e4b\u524d&#xff0c;\u5148\u4e86\u89e3\u4e00\u4e0bESP32\u84dd\u7259\u534f\u8bae\u6808\u7684\u6574\u4f53\u67b6\u6784&#xff0c;\u8fd9\u5bf9\u540e\u7eed\u7684\u8c03\u8bd5\u548c\u95ee\u9898\u6392\u67e5\u4f1a\u6709\u5f88\u5927\u5e2e\u52a9\u3002<\/p>\n<p>ESP32\u7684\u84dd\u7259\u534f\u8bae\u6808\u91c7\u7528\u5206\u5c42\u8bbe\u8ba1&#xff0c;\u4ece\u4e0b\u5230\u4e0a\u4e3b\u8981\u5305\u62ec&#xff1a;<\/p>\n<table>\n<tr>\u5c42\u7ea7\u7ec4\u4ef6\u4e3b\u8981\u529f\u80fd<\/tr>\n<tbody>\n<tr>\n<td>\u63a7\u5236\u5668\u5c42<\/td>\n<td>Bluetooth Controller<\/td>\n<td>\u5c04\u9891\u63a7\u5236\u3001\u57fa\u5e26\u5904\u7406\u3001\u94fe\u8def\u7ba1\u7406<\/td>\n<\/tr>\n<tr>\n<td>\u4e3b\u673a\u5c42<\/td>\n<td>Bluedroid\/ NimBLE<\/td>\n<td>L2CAP\u3001SMP\u3001GATT\u3001GAP\u534f\u8bae\u5b9e\u73b0<\/td>\n<\/tr>\n<tr>\n<td>\u5e94\u7528\u5c42<\/td>\n<td>\u7528\u6237\u5e94\u7528\u7a0b\u5e8f<\/td>\n<td>\u4e1a\u52a1\u903b\u8f91\u3001\u670d\u52a1\u5b9a\u4e49\u3001\u6570\u636e\u5904\u7406<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u79cd\u5206\u5c42\u67b6\u6784\u610f\u5473\u7740&#xff0c;\u5f53\u51fa\u73b0\u901a\u4fe1\u95ee\u9898\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u5148\u5b9a\u4f4d\u95ee\u9898\u53d1\u751f\u5728\u54ea\u4e00\u5c42\u3002\u6bd4\u5982&#xff0c;\u5982\u679c\u8bbe\u5907\u65e0\u6cd5\u88ab\u626b\u63cf\u5230&#xff0c;\u53ef\u80fd\u662fGAP\u5c42\u914d\u7f6e\u95ee\u9898&#xff1b;\u5982\u679c\u80fd\u8fde\u63a5\u4f46\u65e0\u6cd5\u8bfb\u5199\u6570\u636e&#xff0c;\u5219\u53ef\u80fd\u662fGATT\u670d\u52a1\u5b9a\u4e49\u6709\u95ee\u9898\u3002<\/p>\n<h3>2. \u84dd\u7259\u534f\u8bae\u6808\u521d\u59cb\u5316\u4e0e\u8d44\u6e90\u7ba1\u7406<\/h3>\n<p>\u84dd\u7259\u534f\u8bae\u6808\u7684\u521d\u59cb\u5316\u770b\u4f3c\u7b80\u5355&#xff0c;\u4f46\u914d\u7f6e\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898&#xff0c;\u6bd4\u5982\u5185\u5b58\u6cc4\u6f0f\u3001\u8fde\u63a5\u4e0d\u7a33\u5b9a\u3001\u529f\u8017\u5f02\u5e38\u7b49\u3002\u4e0b\u9762\u662f\u6211\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u603b\u7ed3\u51fa\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p>\u9996\u5148\u5728main.c\u4e2d\u5305\u542b\u5fc5\u8981\u7684\u5934\u6587\u4ef6&#xff1a;<\/p>\n<p>#include &#034;esp_bt.h&#034;<br \/>\n#include &#034;esp_bt_main.h&#034;<br \/>\n#include &#034;esp_gap_ble_api.h&#034;<br \/>\n#include &#034;esp_gatts_api.h&#034;<br \/>\n#include &#034;esp_bt_device.h&#034;<br \/>\n#include &#034;esp_log.h&#034;<\/p>\n<p>\u5b9a\u4e49\u65e5\u5fd7\u6807\u7b7e&#xff0c;\u65b9\u4fbf\u8c03\u8bd5&#xff1a;<\/p>\n<p>static const char *TAG &#061; &#034;BLE_SERVER&#034;;<\/p>\n<p>\u521d\u59cb\u5316\u51fd\u6570\u5e94\u8be5\u6309\u7279\u5b9a\u987a\u5e8f\u8c03\u7528&#xff0c;\u8fd9\u4e2a\u987a\u5e8f\u5f88\u91cd\u8981&#xff1a;<\/p>\n<p>esp_err_t ble_init(void)<br \/>\n{<br \/>\n    \/\/ 1. \u521d\u59cb\u5316NVS&#xff08;\u975e\u6613\u5931\u6027\u5b58\u50a8&#xff09;<br \/>\n    esp_err_t ret &#061; nvs_flash_init();<br \/>\n    if (ret &#061;&#061; ESP_ERR_NVS_NO_FREE_PAGES || ret &#061;&#061; ESP_ERR_NVS_NEW_VERSION_FOUND) {<br \/>\n        ESP_ERROR_CHECK(nvs_flash_erase());<br \/>\n        ret &#061; nvs_flash_init();<br \/>\n    }<br \/>\n    ESP_ERROR_CHECK(ret);<\/p>\n<p>    \/\/ 2. \u521d\u59cb\u5316\u84dd\u7259\u63a7\u5236\u5668\u914d\u7f6e<br \/>\n    esp_bt_controller_config_t bt_cfg &#061; BT_CONTROLLER_INIT_CONFIG_DEFAULT();<\/p>\n<p>    \/\/ \u8c03\u6574\u5173\u952e\u53c2\u6570&#xff08;\u6839\u636e\u5b9e\u9645\u9700\u6c42&#xff09;<br \/>\n    bt_cfg.mode &#061; ESP_BT_MODE_BLE;          \/\/ \u4ec5BLE\u6a21\u5f0f<br \/>\n    bt_cfg.ble_max_conn &#061; 3;               \/\/ \u6700\u5927\u8fde\u63a5\u6570<br \/>\n    bt_cfg.ble_max_conn_params &#061; 3;         \/\/ \u8fde\u63a5\u53c2\u6570\u66f4\u65b0\u6b21\u6570<br \/>\n    bt_cfg.bt_max_acl_conn &#061; 3;            \/\/ ACL\u8fde\u63a5\u6570<br \/>\n    bt_cfg.bt_max_sync_conn &#061; 3;           \/\/ \u540c\u6b65\u8fde\u63a5\u6570<\/p>\n<p>    \/\/ 3. \u521d\u59cb\u5316\u84dd\u7259\u63a7\u5236\u5668<br \/>\n    ret &#061; esp_bt_controller_init(&amp;bt_cfg);<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;\u63a7\u5236\u5668\u521d\u59cb\u5316\u5931\u8d25: %s&#034;, esp_err_to_name(ret));<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u4f7f\u80fd\u84dd\u7259\u63a7\u5236\u5668<br \/>\n    ret &#061; esp_bt_controller_enable(ESP_BT_MODE_BLE);<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;\u63a7\u5236\u5668\u4f7f\u80fd\u5931\u8d25: %s&#034;, esp_err_to_name(ret));<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u521d\u59cb\u5316Bluedroid\u534f\u8bae\u6808<br \/>\n    ret &#061; esp_bluedroid_init();<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;Bluedroid\u521d\u59cb\u5316\u5931\u8d25: %s&#034;, esp_err_to_name(ret));<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u4f7f\u80fdBluedroid\u534f\u8bae\u6808<br \/>\n    ret &#061; esp_bluedroid_enable();<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;Bluedroid\u4f7f\u80fd\u5931\u8d25: %s&#034;, esp_err_to_name(ret));<br \/>\n        return ret;<br \/>\n    }<\/p>\n<p>    ESP_LOGI(TAG, &#034;\u84dd\u7259\u534f\u8bae\u6808\u521d\u59cb\u5316\u5b8c\u6210&#034;);<br \/>\n    return ESP_OK;<br \/>\n}<\/p>\n<p>\u63d0\u793a&#xff1a;\u5728\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u5efa\u8bae\u4e3a\u6bcf\u4e2a\u9519\u8bef\u68c0\u67e5\u6dfb\u52a0\u66f4\u8be6\u7ec6\u7684\u65e5\u5fd7&#xff0c;\u5e76\u8003\u8651\u9519\u8bef\u6062\u590d\u673a\u5236\u3002\u6bd4\u5982\u63a7\u5236\u5668\u521d\u59cb\u5316\u5931\u8d25\u540e&#xff0c;\u53ef\u4ee5\u5c1d\u8bd5\u5ef6\u8fdf\u91cd\u8bd5\u3002<\/p>\n<p>\u5185\u5b58\u7ba1\u7406\u662f\u84dd\u7259\u5f00\u53d1\u4e2d\u7684\u53e6\u4e00\u4e2a\u5173\u952e\u70b9\u3002ESP32\u7684\u5185\u5b58\u5728\u8fd0\u884c\u84dd\u7259\u534f\u8bae\u6808\u65f6\u76f8\u5bf9\u7d27\u5f20&#xff0c;\u7279\u522b\u662f\u5f53\u540c\u65f6\u8fd0\u884cWi-Fi\u548c\u5176\u4ed6\u529f\u80fd\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5185\u5b58\u4f18\u5316\u6280\u5de7&#xff1a;<\/p>\n<ul>\n<li>\u9759\u6001\u5206\u914d\u4f18\u5148&#xff1a;\u5c3d\u53ef\u80fd\u4f7f\u7528\u9759\u6001\u6570\u7ec4\u800c\u975e\u52a8\u6001\u5206\u914d<\/li>\n<li>\u5408\u7406\u8bbe\u7f6eMTU\u5927\u5c0f&#xff1a;\u9ed8\u8ba4MTU\u4e3a23\u5b57\u8282&#xff0c;\u4f46\u53ef\u4ee5\u901a\u8fc7\u534f\u5546\u589e\u52a0\u5230247\u5b57\u8282&#xff0c;\u51cf\u5c11\u5206\u5305\u6b21\u6570<\/li>\n<li>\u63a7\u5236\u8fde\u63a5\u6570&#xff1a;\u6bcf\u4e2a\u8fde\u63a5\u90fd\u4f1a\u5360\u7528\u5185\u5b58&#xff0c;\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8bbe\u7f6eble_max_conn<\/li>\n<\/ul>\n<p>\u521d\u59cb\u5316\u5b8c\u6210\u540e&#xff0c;\u9700\u8981\u6ce8\u518cGAP\u548cGATT\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u91cc\u6709\u4e2a\u5e38\u89c1\u8bef\u533a&#xff1a;\u5f88\u591a\u4eba\u628a\u8fd9\u4e24\u4e2a\u56de\u8c03\u6ce8\u518c\u653e\u5728\u540c\u4e00\u4e2a\u51fd\u6570\u91cc&#xff0c;\u4f46\u5b9e\u9645\u4e0a\u5b83\u4eec\u5e94\u8be5\u5206\u5f00\u7ba1\u7406&#xff0c;\u56e0\u4e3aGAP\u4e8b\u4ef6\u901a\u5e38\u4e0e\u8bbe\u5907\u53d1\u73b0\u548c\u8fde\u63a5\u7ba1\u7406\u76f8\u5173&#xff0c;\u800cGATT\u4e8b\u4ef6\u5219\u4e13\u6ce8\u4e8e\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<p>\/\/ \u6ce8\u518cGAP\u4e8b\u4ef6\u56de\u8c03<br \/>\nesp_ble_gap_register_callback(gap_event_handler);<\/p>\n<p>\/\/ \u6ce8\u518cGATT\u4e8b\u4ef6\u56de\u8c03<br \/>\nesp_ble_gatts_register_callback(gatts_event_handler);<\/p>\n<p>\u56de\u8c03\u51fd\u6570\u7684\u5b9e\u73b0\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u8be6\u7ec6\u8ba8\u8bba\u3002\u73b0\u5728&#xff0c;\u5148\u786e\u4fdd\u521d\u59cb\u5316\u6d41\u7a0b\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u5728app_main\u51fd\u6570\u4e2d\u8c03\u7528ble_init()&#xff0c;\u7136\u540e\u6dfb\u52a0\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u8f93\u51fa&#xff0c;\u9a8c\u8bc1\u521d\u59cb\u5316\u662f\u5426\u6210\u529f\u3002<\/p>\n<h3>3. GAP\u5c42\u914d\u7f6e\u4e0e\u5e7f\u64ad\u7b56\u7565\u4f18\u5316<\/h3>\n<p>GAP&#xff08;Generic Access Profile&#xff09;\u5c42\u8d1f\u8d23\u8bbe\u5907\u53d1\u73b0\u3001\u8fde\u63a5\u5efa\u7acb\u548c\u5b89\u5168\u63a7\u5236\u3002\u5f88\u591a\u5f00\u53d1\u8005\u53ea\u5173\u6ce8GATT\u670d\u52a1&#xff0c;\u5374\u5ffd\u7565\u4e86GAP\u914d\u7f6e\u7684\u91cd\u8981\u6027&#xff0c;\u7ed3\u679c\u5bfc\u81f4\u8bbe\u5907\u96be\u4ee5\u88ab\u53d1\u73b0\u3001\u8fde\u63a5\u4e0d\u7a33\u5b9a\u6216\u529f\u8017\u8fc7\u9ad8\u3002<\/p>\n<h4>3.1 \u5e7f\u64ad\u6570\u636e\u914d\u7f6e<\/h4>\n<p>\u5e7f\u64ad\u6570\u636e\u51b3\u5b9a\u4e86\u5916\u56f4\u8bbe\u5907\u5982\u4f55\u88ab\u4e2d\u5fc3\u8bbe\u5907&#xff08;\u5982\u624b\u673a&#xff09;\u53d1\u73b0\u548c\u8bc6\u522b\u3002ESP-IDF\u63d0\u4f9b\u4e86\u4e24\u79cd\u914d\u7f6e\u65b9\u5f0f&#xff1a;\u6807\u51c6\u6570\u636e\u7ed3\u6784\u548c\u539f\u59cb\u6570\u636e\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528&#xff0c;\u6211\u63a8\u8350\u4f7f\u7528\u539f\u59cb\u6570\u636e\u65b9\u5f0f&#xff0c;\u56e0\u4e3a\u5b83\u66f4\u7075\u6d3b\u3002<\/p>\n<p>\u9996\u5148\u5b9a\u4e49\u5e7f\u64ad\u6570\u636e&#xff1a;<\/p>\n<p>\/\/ \u5e7f\u64ad\u6570\u636e&#xff08;31\u5b57\u8282\u9650\u5236&#xff09;<br \/>\nstatic uint8_t raw_adv_data[] &#061; {<br \/>\n    \/\/ \u6807\u5fd7\u4f4d<br \/>\n    0x02, 0x01, 0x06,<br \/>\n    \/\/ \u5b8c\u6574\u8bbe\u5907\u540d<br \/>\n    0x0d, 0x09, &#039;E&#039;, &#039;S&#039;, &#039;P&#039;, &#039;3&#039;, &#039;2&#039;, &#039;_&#039;, &#039;G&#039;, &#039;A&#039;, &#039;T&#039;, &#039;T&#039;, &#039;_&#039;, &#039;S&#039;, &#039;V&#039;, &#039;R&#039;,<br \/>\n    \/\/ 128\u4f4d\u670d\u52a1UUID<br \/>\n    0x11, 0x07,<br \/>\n    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,<br \/>\n    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00<br \/>\n};<\/p>\n<p>\/\/ \u626b\u63cf\u54cd\u5e94\u6570\u636e<br \/>\nstatic uint8_t raw_scan_rsp_data[] &#061; {<br \/>\n    \/\/ \u5236\u9020\u5546\u7279\u5b9a\u6570\u636e<br \/>\n    0x05, 0xff, 0x4c, 0x00, 0x02, 0x15,<br \/>\n    \/\/ \u53d1\u5c04\u529f\u7387<br \/>\n    0x02, 0x0a, 0xeb<br \/>\n};<\/p>\n<p>\u5e7f\u64ad\u6570\u636e\u683c\u5f0f\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>\u7b2c1-3\u5b57\u8282&#xff1a;0x02, 0x01, 0x06 \u8868\u793a&#034;\u6807\u5fd7&#034;\u6570\u636e\u7c7b\u578b&#xff0c;\u957f\u5ea62&#xff0c;\u5185\u5bb90x06&#xff08;LE\u901a\u7528\u53d1\u73b0\u6a21\u5f0f&#043;\u4e0d\u652f\u6301\u4f20\u7edf\u84dd\u7259&#xff09;<\/li>\n<li>\u7b2c4-19\u5b57\u8282&#xff1a;\u8bbe\u5907\u540d\u79f0&#xff0c;0x0d\u8868\u793a\u540e\u9762\u670913\u4e2a\u5b57\u8282\u6570\u636e&#xff0c;0x09\u8868\u793a&#034;\u5b8c\u6574\u8bbe\u5907\u540d&#034;\u7c7b\u578b<\/li>\n<li>\u7b2c20-36\u5b57\u8282&#xff1a;\u670d\u52a1UUID&#xff0c;0x11\u8868\u793a\u540e\u9762\u670917\u4e2a\u5b57\u8282&#xff0c;0x07\u8868\u793a&#034;128\u4f4d\u670d\u52a1UUID&#034;\u7c7b\u578b<\/li>\n<\/ul>\n<p>\u6ce8\u610f&#xff1a;\u5e7f\u64ad\u6570\u636e\u603b\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc731\u5b57\u8282&#xff0c;\u8fd9\u662fBLE\u534f\u8bae\u7684\u9650\u5236\u3002\u5982\u679c\u6570\u636e\u8fc7\u591a&#xff0c;\u9700\u8981\u5408\u7406\u53d6\u820d\u6216\u4f7f\u7528\u626b\u63cf\u54cd\u5e94\u6570\u636e\u8865\u5145\u3002<\/p>\n<p>\u914d\u7f6e\u5e7f\u64ad\u53c2\u6570\u65f6&#xff0c;\u6709\u51e0\u4e2a\u5173\u952e\u8bbe\u7f6e\u4f1a\u5f71\u54cd\u8bbe\u5907\u884c\u4e3a&#xff1a;<\/p>\n<p>static esp_ble_adv_params_t adv_params &#061; {<br \/>\n    .adv_int_min        &#061; 0x20,     \/\/ \u6700\u5c0f\u5e7f\u64ad\u95f4\u9694&#xff1a;32*0.625ms &#061; 20ms<br \/>\n    .adv_int_max        &#061; 0x40,     \/\/ \u6700\u5927\u5e7f\u64ad\u95f4\u9694&#xff1a;64*0.625ms &#061; 40ms<br \/>\n    .adv_type           &#061; ADV_TYPE_IND,  \/\/ \u53ef\u8fde\u63a5\u7684\u975e\u5b9a\u5411\u5e7f\u64ad<br \/>\n    .own_addr_type      &#061; BLE_ADDR_TYPE_PUBLIC,<br \/>\n    .channel_map        &#061; ADV_CHNL_ALL,<br \/>\n    .adv_filter_policy &#061; ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,<br \/>\n};<\/p>\n<p>\u5e7f\u64ad\u95f4\u9694\u7684\u9009\u62e9\u9700\u8981\u5728\u529f\u8017\u548c\u54cd\u5e94\u901f\u5ea6\u4e4b\u95f4\u6743\u8861&#xff1a;<\/p>\n<ul>\n<li>\u5feb\u901f\u5e7f\u64ad&#xff08;20-40ms&#xff09;&#xff1a;\u8bbe\u5907\u5bb9\u6613\u88ab\u53d1\u73b0&#xff0c;\u4f46\u529f\u8017\u8f83\u9ad8<\/li>\n<li>\u6162\u901f\u5e7f\u64ad&#xff08;1-2s&#xff09;&#xff1a;\u529f\u8017\u4f4e&#xff0c;\u4f46\u8bbe\u5907\u53d1\u73b0\u5ef6\u8fdf\u5927<\/li>\n<\/ul>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u6211\u901a\u5e38\u91c7\u7528\u53cc\u9636\u6bb5\u5e7f\u64ad\u7b56\u7565&#xff1a;\u8bbe\u5907\u521a\u542f\u52a8\u65f6\u4f7f\u7528\u5feb\u901f\u5e7f\u64ad&#xff08;20-40ms&#xff09;&#xff0c;\u6301\u7eed30\u79d2&#xff1b;\u5982\u679c\u6ca1\u6709\u8fde\u63a5&#xff0c;\u5207\u6362\u5230\u6162\u901f\u5e7f\u64ad&#xff08;1-2s&#xff09;\u3002\u8fd9\u6837\u53ef\u4ee5\u517c\u987e\u5feb\u901f\u8fde\u63a5\u548c\u4f4e\u529f\u8017\u7684\u9700\u6c42\u3002<\/p>\n<h4>3.2 GAP\u4e8b\u4ef6\u5904\u7406<\/h4>\n<p>GAP\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u9700\u8981\u5904\u7406\u591a\u79cd\u4e8b\u4ef6&#xff0c;\u4ee5\u4e0b\u662f\u4e00\u4e2a\u76f8\u5bf9\u5b8c\u6574\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)<br \/>\n{<br \/>\n    switch (event) {<br \/>\n        case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;\u5e7f\u64ad\u6570\u636e\u8bbe\u7f6e\u5b8c\u6210&#034;);<br \/>\n            adv_config_done &amp;&#061; ~ADV_CONFIG_FLAG;<br \/>\n            if (adv_config_done &#061;&#061; 0) {<br \/>\n                esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;\u626b\u63cf\u54cd\u5e94\u6570\u636e\u8bbe\u7f6e\u5b8c\u6210&#034;);<br \/>\n            adv_config_done &amp;&#061; ~SCAN_RSP_CONFIG_FLAG;<br \/>\n            if (adv_config_done &#061;&#061; 0) {<br \/>\n                esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:<br \/>\n            if (param-&gt;adv_start_cmpl.status !&#061; ESP_BT_STATUS_SUCCESS) {<br \/>\n                ESP_LOGE(TAG, &#034;\u5e7f\u64ad\u542f\u52a8\u5931\u8d25&#034;);<br \/>\n                \/\/ \u8fd9\u91cc\u53ef\u4ee5\u6dfb\u52a0\u91cd\u8bd5\u903b\u8f91<br \/>\n                vTaskDelay(pdMS_TO_TICKS(1000));<br \/>\n                esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n            } else {<br \/>\n                ESP_LOGI(TAG, &#034;\u5e7f\u64ad\u5df2\u542f\u52a8&#034;);<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;\u5e7f\u64ad\u5df2\u505c\u6b62&#034;);<br \/>\n            break;<\/p>\n<p>        case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;\u8fde\u63a5\u53c2\u6570\u66f4\u65b0: interval&#061;%d, latency&#061;%d, timeout&#061;%d&#034;,<br \/>\n                    param-&gt;update_conn_params.conn_int,<br \/>\n                    param-&gt;update_conn_params.latency,<br \/>\n                    param-&gt;update_conn_params.timeout);<br \/>\n            break;<\/p>\n<p>        case ESP_GAP_BLE_SEC_REQ_EVT:<br \/>\n            \/\/ \u5b89\u5168\u8bf7\u6c42\u5904\u7406<br \/>\n            esp_ble_gap_security_rsp(param-&gt;ble_security.ble_req.bd_addr, true);<br \/>\n            break;<\/p>\n<p>        default:<br \/>\n            ESP_LOGD(TAG, &#034;\u672a\u5904\u7406\u7684GAP\u4e8b\u4ef6: %d&#034;, event);<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fde\u63a5\u53c2\u6570\u7ba1\u7406\u662f\u4fdd\u8bc1\u901a\u4fe1\u7a33\u5b9a\u6027\u7684\u5173\u952e\u3002\u5f53\u8bbe\u5907\u8fde\u63a5\u540e&#xff0c;\u5e94\u8be5\u6839\u636e\u5e94\u7528\u9700\u6c42\u66f4\u65b0\u8fde\u63a5\u53c2\u6570&#xff1a;<\/p>\n<p>static void update_connection_params(esp_bd_addr_t bd_addr)<br \/>\n{<br \/>\n    esp_ble_conn_update_params_t conn_params &#061; {<br \/>\n        .bda &#061; bd_addr,<br \/>\n        .min_int &#061; 0x10,      \/\/ 20ms<br \/>\n        .max_int &#061; 0x20,      \/\/ 40ms<br \/>\n        .latency &#061; 0,<br \/>\n        .timeout &#061; 400,       \/\/ 4000ms<br \/>\n    };<\/p>\n<p>    esp_ble_gap_update_conn_params(&amp;conn_params);<br \/>\n}<\/p>\n<p>\u5bf9\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f&#xff0c;\u8fde\u63a5\u53c2\u6570\u5e94\u8be5\u6709\u6240\u8c03\u6574&#xff1a;<\/p>\n<ul>\n<li>\u5b9e\u65f6\u6570\u636e\u4f20\u8f93&#xff08;\u5982\u4f20\u611f\u5668\u6d41&#xff09;&#xff1a;\u4f7f\u7528\u8f83\u77ed\u7684\u8fde\u63a5\u95f4\u9694&#xff08;20-40ms&#xff09;&#xff0c;\u4f4e\u5ef6\u8fdf<\/li>\n<li>\u95f4\u6b47\u6027\u6570\u636e\u4f20\u8f93&#xff08;\u5982\u6e29\u5ea6\u4e0a\u62a5&#xff09;&#xff1a;\u4f7f\u7528\u8f83\u957f\u7684\u8fde\u63a5\u95f4\u9694&#xff08;100-200ms&#xff09;&#xff0c;\u964d\u4f4e\u529f\u8017<\/li>\n<li>\u7535\u6c60\u4f9b\u7535\u8bbe\u5907&#xff1a;\u5c3d\u53ef\u80fd\u4f7f\u7528\u957f\u8fde\u63a5\u95f4\u9694&#xff0c;\u5e76\u5141\u8bb8\u4e00\u5b9a\u7684\u5ef6\u8fdf<\/li>\n<\/ul>\n<h3>4. GATT\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1\u4e0e\u5b9e\u73b0<\/h3>\n<p>GATT&#xff08;Generic Attribute Profile&#xff09;\u662fBLE\u6570\u636e\u4ea4\u6362\u7684\u6838\u5fc3\u3002\u8bbe\u8ba1\u826f\u597d\u7684GATT\u670d\u52a1\u67b6\u6784\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u5f00\u53d1\u6548\u7387&#xff0c;\u8fd8\u80fd\u786e\u4fdd\u7cfb\u7edf\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6269\u5c55\u6027\u3002<\/p>\n<h4>4.1 \u670d\u52a1\u8868\u5b9a\u4e49<\/h4>\n<p>GATT\u670d\u52a1\u901a\u8fc7\u5c5e\u6027\u8868&#xff08;Attribute Table&#xff09;\u5b9a\u4e49\u3002\u6bcf\u4e2a\u670d\u52a1\u5305\u542b\u591a\u4e2a\u7279\u5f81&#xff08;Characteristic&#xff09;&#xff0c;\u6bcf\u4e2a\u7279\u5f81\u53c8\u5305\u542b\u503c\u3001\u63cf\u8ff0\u7b26\u7b49\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u73af\u5883\u76d1\u6d4b\u670d\u52a1\u793a\u4f8b&#xff1a;<\/p>\n<p>#define GATT_DB_NUM 7  \/\/ \u5c5e\u6027\u603b\u6570<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49UUID&#xff08;128\u4f4d&#xff09;<br \/>\nstatic const uint8_t SERVICE_UUID_ENV[16] &#061; {<br \/>\n    0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78,<br \/>\n    0x9a, 0xbc, 0xde, 0xf0, 0x00, 0x00, 0x00, 0x01<br \/>\n};<\/p>\n<p>static const uint8_t CHAR_UUID_TEMP[16] &#061; {<br \/>\n    0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78,<br \/>\n    0x9a, 0xbc, 0xde, 0xf0, 0x00, 0x00, 0x00, 0x02<br \/>\n};<\/p>\n<p>static const uint8_t CHAR_UUID_HUMID[16] &#061; {<br \/>\n    0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78,<br \/>\n    0x9a, 0xbc, 0xde, 0xf0, 0x00, 0x00, 0x00, 0x03<br \/>\n};<\/p>\n<p>\/\/ \u5c5e\u6027\u8868\u5b9a\u4e49<br \/>\nstatic const esp_gatts_attr_db_t gatt_db[GATT_DB_NUM] &#061; {<br \/>\n    \/\/ [0] \u670d\u52a1\u58f0\u660e<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;primary_service_uuid,<br \/>\n         ESP_GATT_PERM_READ,<br \/>\n         sizeof(SERVICE_UUID_ENV), sizeof(SERVICE_UUID_ENV),<br \/>\n         (uint8_t *)SERVICE_UUID_ENV}<br \/>\n    },<\/p>\n<p>    \/\/ [1] \u6e29\u5ea6\u7279\u5f81\u58f0\u660e<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_declaration_uuid,<br \/>\n         ESP_GATT_PERM_READ,<br \/>\n         CHAR_PROP_BIT_READ | CHAR_PROP_BIT_NOTIFY,<br \/>\n         0, NULL}<br \/>\n    },<\/p>\n<p>    \/\/ [2] \u6e29\u5ea6\u7279\u5f81\u503c<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_128, (uint8_t *)CHAR_UUID_TEMP,<br \/>\n         ESP_GATT_PERM_READ,<br \/>\n         sizeof(float), 0, NULL}<br \/>\n    },<\/p>\n<p>    \/\/ [3] \u6e29\u5ea6CCCD&#xff08;\u5ba2\u6237\u7aef\u7279\u5f81\u914d\u7f6e\u63cf\u8ff0\u7b26&#xff09;<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_client_config_uuid,<br \/>\n         ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,<br \/>\n         sizeof(uint16_t), 0, NULL}<br \/>\n    },<\/p>\n<p>    \/\/ [4] \u6e7f\u5ea6\u7279\u5f81\u58f0\u660e<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_declaration_uuid,<br \/>\n         ESP_GATT_PERM_READ,<br \/>\n         CHAR_PROP_BIT_READ | CHAR_PROP_BIT_WRITE,<br \/>\n         0, NULL}<br \/>\n    },<\/p>\n<p>    \/\/ [5] \u6e7f\u5ea6\u7279\u5f81\u503c<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_128, (uint8_t *)CHAR_UUID_HUMID,<br \/>\n         ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,<br \/>\n         sizeof(float), 0, NULL}<br \/>\n    },<\/p>\n<p>    \/\/ [6] \u6e7f\u5ea6CCCD<br \/>\n    {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_client_config_uuid,<br \/>\n         ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,<br \/>\n         sizeof(uint16_t), 0, NULL}<br \/>\n    }<br \/>\n};<\/p>\n<p>\u5c5e\u6027\u8868\u7684\u5173\u952e\u5b57\u6bb5\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>UUID\u957f\u5ea6\u548c\u503c&#xff1a;16\u4f4d\u6807\u51c6UUID\u6216128\u4f4d\u81ea\u5b9a\u4e49UUID<\/li>\n<li>\u6743\u9650&#xff1a;ESP_GATT_PERM_READ\u3001ESP_GATT_PERM_WRITE\u7b49<\/li>\n<li>\u6700\u5927\u957f\u5ea6&#xff1a;\u7279\u5f81\u503c\u5141\u8bb8\u7684\u6700\u5927\u5b57\u8282\u6570<\/li>\n<li>\u521d\u59cb\u503c&#xff1a;\u7279\u5f81\u7684\u521d\u59cb\u6570\u636e&#xff0c;\u5982\u679c\u4e3a0\u6216NULL&#xff0c;\u5219\u9700\u8981\u5728\u521b\u5efa\u65f6\u52a8\u6001\u8bbe\u7f6e<\/li>\n<\/ul>\n<h4>4.2 GATT\u4e8b\u4ef6\u5904\u7406<\/h4>\n<p>GATT\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u662f\u670d\u52a1\u903b\u8f91\u7684\u6838\u5fc3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u652f\u6301\u591a\u670d\u52a1\u3001\u591a\u8fde\u63a5\u7684\u5904\u7406\u6846\u67b6&#xff1a;<\/p>\n<p>\/\/ \u8fde\u63a5\u4fe1\u606f\u7ed3\u6784<br \/>\ntypedef struct {<br \/>\n    uint16_t conn_id;<br \/>\n    esp_gatt_if_t gatts_if;<br \/>\n    uint16_t temp_ccc_value;<br \/>\n    uint16_t humid_ccc_value;<br \/>\n    bool connected;<br \/>\n} conn_info_t;<\/p>\n<p>static conn_info_t connections[MAX_CONNECTIONS];<\/p>\n<p>static void gatts_event_handler(esp_gatts_cb_event_t event,<br \/>\n                               esp_gatt_if_t gatts_if,<br \/>\n                               esp_ble_gatts_cb_param_t *param)<br \/>\n{<br \/>\n    \/\/ \u67e5\u627e\u6216\u521b\u5efa\u8fde\u63a5\u4fe1\u606f<br \/>\n    conn_info_t *conn &#061; find_connection(gatts_if, param-&gt;connect.conn_id);<\/p>\n<p>    switch (event) {<br \/>\n        case ESP_GATTS_REG_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;GATT\u5e94\u7528\u6ce8\u518c\u6210\u529f&#xff0c;\u63a5\u53e3ID: %d&#034;, gatts_if);<\/p>\n<p>            \/\/ \u8bbe\u7f6e\u8bbe\u5907\u540d\u79f0<br \/>\n            esp_ble_gap_set_device_name(&#034;ESP32_ENV_SENSOR&#034;);<\/p>\n<p>            \/\/ \u914d\u7f6e\u5e7f\u64ad\u6570\u636e<br \/>\n            esp_ble_gap_config_adv_data_raw(raw_adv_data,<br \/>\n                                           sizeof(raw_adv_data));<\/p>\n<p>            \/\/ \u521b\u5efa\u5c5e\u6027\u8868<br \/>\n            esp_ble_gatts_create_attr_tab(gatt_db, gatts_if,<br \/>\n                                         GATT_DB_NUM, 0);<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_CONNECT_EVT: {<br \/>\n            ESP_LOGI(TAG, &#034;\u8bbe\u5907\u8fde\u63a5&#xff0c;\u8fde\u63a5ID: %d&#034;, param-&gt;connect.conn_id);<\/p>\n<p>            if (conn &#061;&#061; NULL) {<br \/>\n                conn &#061; add_connection(gatts_if, param-&gt;connect.conn_id);<br \/>\n            }<\/p>\n<p>            conn-&gt;connected &#061; true;<br \/>\n            conn-&gt;conn_id &#061; param-&gt;connect.conn_id;<br \/>\n            conn-&gt;gatts_if &#061; gatts_if;<\/p>\n<p>            \/\/ \u66f4\u65b0\u8fde\u63a5\u53c2\u6570<br \/>\n            update_connection_params(param-&gt;connect.remote_bda);<\/p>\n<p>            \/\/ \u505c\u6b62\u5e7f\u64ad\u4ee5\u8282\u7701\u529f\u8017<br \/>\n            esp_ble_gap_stop_advertising();<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        case ESP_GATTS_DISCONNECT_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;\u8bbe\u5907\u65ad\u5f00\u8fde\u63a5&#xff0c;\u539f\u56e0: 0x%x&#034;,<br \/>\n                    param-&gt;disconnect.reason);<\/p>\n<p>            if (conn !&#061; NULL) {<br \/>\n                conn-&gt;connected &#061; false;<br \/>\n                remove_connection(conn);<br \/>\n            }<\/p>\n<p>            \/\/ \u91cd\u65b0\u5f00\u59cb\u5e7f\u64ad<br \/>\n            esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_CREAT_ATTR_TAB_EVT:<br \/>\n            if (param-&gt;add_attr_tab.status &#061;&#061; ESP_GATT_OK) {<br \/>\n                ESP_LOGI(TAG, &#034;\u5c5e\u6027\u8868\u521b\u5efa\u6210\u529f&#xff0c;\u53e5\u67c4\u6570: %d&#034;,<br \/>\n                        param-&gt;add_attr_tab.num_handle);<\/p>\n<p>                \/\/ \u4fdd\u5b58\u53e5\u67c4<br \/>\n                save_attribute_handles(param-&gt;add_attr_tab.handles,<br \/>\n                                      param-&gt;add_attr_tab.num_handle);<\/p>\n<p>                \/\/ \u542f\u52a8\u670d\u52a1<br \/>\n                esp_ble_gatts_start_service(<br \/>\n                    param-&gt;add_attr_tab.handles[0]);<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_READ_EVT:<br \/>\n            handle_read_event(gatts_if, conn, param);<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_WRITE_EVT:<br \/>\n            handle_write_event(gatts_if, conn, param);<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_MTU_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;MTU\u66f4\u65b0: %d&#034;, param-&gt;mtu.mtu);<br \/>\n            break;<\/p>\n<p>        default:<br \/>\n            ESP_LOGD(TAG, &#034;\u672a\u5904\u7406\u7684GATT\u4e8b\u4ef6: %d&#034;, event);<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8bfb\u5199\u4e8b\u4ef6\u7684\u5177\u4f53\u5904\u7406\u9700\u8981\u6839\u636e\u4e1a\u52a1\u903b\u8f91\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6e29\u5ea6\u8bfb\u53d6\u7684\u793a\u4f8b&#xff1a;<\/p>\n<p>static void handle_read_event(esp_gatt_if_t gatts_if,<br \/>\n                             conn_info_t *conn,<br \/>\n                             esp_ble_gatts_cb_param_t *param)<br \/>\n{<br \/>\n    uint16_t handle &#061; param-&gt;read.handle;<br \/>\n    esp_gatt_rsp_t rsp;<\/p>\n<p>    memset(&amp;rsp, 0, sizeof(esp_gatt_rsp_t));<\/p>\n<p>    \/\/ \u6839\u636e\u53e5\u67c4\u5224\u65ad\u8bfb\u53d6\u54ea\u4e2a\u7279\u5f81<br \/>\n    if (handle &#061;&#061; temp_value_handle) {<br \/>\n        \/\/ \u8bfb\u53d6\u6e29\u5ea6\u503c<br \/>\n        float current_temp &#061; read_temperature_sensor();<br \/>\n        memcpy(rsp.attr_value.value, &amp;current_temp, sizeof(float));<br \/>\n        rsp.attr_value.len &#061; sizeof(float);<\/p>\n<p>        ESP_LOGI(TAG, &#034;\u6e29\u5ea6\u8bfb\u53d6: %.2f\u00b0C&#034;, current_temp);<br \/>\n    }<br \/>\n    else if (handle &#061;&#061; humid_value_handle) {<br \/>\n        \/\/ \u8bfb\u53d6\u6e7f\u5ea6\u503c<br \/>\n        float current_humid &#061; read_humidity_sensor();<br \/>\n        memcpy(rsp.attr_value.value, &amp;current_humid, sizeof(float));<br \/>\n        rsp.attr_value.len &#061; sizeof(float);<\/p>\n<p>        ESP_LOGI(TAG, &#034;\u6e7f\u5ea6\u8bfb\u53d6: %.2f%%&#034;, current_humid);<br \/>\n    }<br \/>\n    else {<br \/>\n        \/\/ \u5176\u4ed6\u7279\u5f81\u6216\u63cf\u8ff0\u7b26<br \/>\n        rsp.attr_value.len &#061; 0;<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u54cd\u5e94<br \/>\n    esp_ble_gatts_send_response(gatts_if,<br \/>\n                               param-&gt;read.conn_id,<br \/>\n                               param-&gt;read.trans_id,<br \/>\n                               ESP_GATT_OK, &amp;rsp);<br \/>\n}<\/p>\n<p>\u5199\u4e8b\u4ef6\u5904\u7406&#xff0c;\u7279\u522b\u662fCCCD\u7684\u5199\u5165&#xff0c;\u9700\u8981\u7279\u522b\u6ce8\u610f&#xff1a;<\/p>\n<p>static void handle_write_event(esp_gatt_if_t gatts_if,<br \/>\n                              conn_info_t *conn,<br \/>\n                              esp_ble_gatts_cb_param_t *param)<br \/>\n{<br \/>\n    uint16_t handle &#061; param-&gt;write.handle;<\/p>\n<p>    if (handle &#061;&#061; temp_cccd_handle) {<br \/>\n        \/\/ \u6e29\u5ea6\u901a\u77e5\u914d\u7f6e\u66f4\u65b0<br \/>\n        uint16_t cccd_value &#061;<br \/>\n            *(uint16_t *)param-&gt;write.value;<\/p>\n<p>        conn-&gt;temp_ccc_value &#061; cccd_value;<\/p>\n<p>        if (cccd_value &#061;&#061; 0x0001) {<br \/>\n            ESP_LOGI(TAG, &#034;\u6e29\u5ea6\u901a\u77e5\u5df2\u542f\u7528&#034;);<br \/>\n            \/\/ \u542f\u52a8\u6e29\u5ea6\u5b9a\u65f6\u901a\u77e5\u4efb\u52a1<br \/>\n            start_temperature_notification(conn);<br \/>\n        } else {<br \/>\n            ESP_LOGI(TAG, &#034;\u6e29\u5ea6\u901a\u77e5\u5df2\u7981\u7528&#034;);<br \/>\n            \/\/ \u505c\u6b62\u6e29\u5ea6\u5b9a\u65f6\u901a\u77e5\u4efb\u52a1<br \/>\n            stop_temperature_notification(conn);<br \/>\n        }<br \/>\n    }<br \/>\n    else if (handle &#061;&#061; humid_value_handle) {<br \/>\n        \/\/ \u6e7f\u5ea6\u503c\u5199\u5165&#xff08;\u914d\u7f6e\u9608\u503c\u7b49&#xff09;<br \/>\n        float threshold &#061; *(float *)param-&gt;write.value;<br \/>\n        set_humidity_threshold(threshold);<\/p>\n<p>        ESP_LOGI(TAG, &#034;\u6e7f\u5ea6\u9608\u503c\u66f4\u65b0: %.2f%%&#034;, threshold);<br \/>\n    }<\/p>\n<p>    \/\/ \u5982\u679c\u9700\u8981\u54cd\u5e94&#xff08;\u975e\u81ea\u52a8\u54cd\u5e94&#xff09;<br \/>\n    if (!param-&gt;write.need_rsp) {<br \/>\n        esp_ble_gatts_send_response(gatts_if,<br \/>\n                                   param-&gt;write.conn_id,<br \/>\n                                   param-&gt;write.trans_id,<br \/>\n                                   ESP_GATT_OK, NULL);<br \/>\n    }<br \/>\n}<\/p>\n<h3>5. \u6570\u636e\u901a\u4fe1\u4f18\u5316\u4e0e\u529f\u8017\u7ba1\u7406<\/h3>\n<p>\u5728\u5b9e\u9645\u90e8\u7f72\u4e2d&#xff0c;\u901a\u4fe1\u6548\u7387\u548c\u529f\u8017\u5f80\u5f80\u662f\u51b3\u5b9a\u9879\u76ee\u6210\u8d25\u7684\u5173\u952e\u56e0\u7d20\u3002\u7ecf\u8fc7\u591a\u4e2a\u9879\u76ee\u7684\u5b9e\u8df5&#xff0c;\u6211\u603b\u7ed3\u4e86\u4e00\u4e9b\u6709\u6548\u7684\u4f18\u5316\u7b56\u7565\u3002<\/p>\n<h4>5.1 \u6570\u636e\u5206\u5305\u4e0e\u91cd\u7ec4<\/h4>\n<p>BLE\u534f\u8bae\u6bcf\u4e2a\u6570\u636e\u5305\u7684\u6709\u6548\u8f7d\u8377\u6709\u9650&#xff08;\u9ed8\u8ba423\u5b57\u8282&#xff0c;\u534f\u5546\u540e\u6700\u591a247\u5b57\u8282&#xff09;\u3002\u4f20\u8f93\u8f83\u5927\u6570\u636e\u65f6\u9700\u8981\u5206\u5305\u53d1\u9001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u53ef\u9760\u7684\u5206\u5305\u4f20\u8f93\u5b9e\u73b0&#xff1a;<\/p>\n<p>#define MAX_PACKET_SIZE 20  \/\/ \u9884\u75593\u5b57\u8282\u7ed9ATT\u5934<\/p>\n<p>typedef struct {<br \/>\n    uint8_t data[256];<br \/>\n    uint16_t total_len;<br \/>\n    uint16_t sent_len;<br \/>\n    uint16_t handle;<br \/>\n    uint8_t packet_seq;<br \/>\n} large_data_t;<\/p>\n<p>static void send_large_data(esp_gatt_if_t gatts_if,<br \/>\n                           uint16_t conn_id,<br \/>\n                           uint16_t handle,<br \/>\n                           uint8_t *data,<br \/>\n                           uint16_t len)<br \/>\n{<br \/>\n    large_data_t *ld &#061; malloc(sizeof(large_data_t));<\/p>\n<p>    memcpy(ld-&gt;data, data, len);<br \/>\n    ld-&gt;total_len &#061; len;<br \/>\n    ld-&gt;sent_len &#061; 0;<br \/>\n    ld-&gt;handle &#061; handle;<br \/>\n    ld-&gt;packet_seq &#061; 0;<\/p>\n<p>    \/\/ \u542f\u52a8\u5206\u6bb5\u53d1\u9001\u4efb\u52a1<br \/>\n    xTaskCreate(send_segmented_task,<br \/>\n                &#034;send_seg&#034;,<br \/>\n                4096,<br \/>\n                (void *)ld,<br \/>\n                5,<br \/>\n                NULL);<br \/>\n}<\/p>\n<p>static void send_segmented_task(void *arg)<br \/>\n{<br \/>\n    large_data_t *ld &#061; (large_data_t *)arg;<\/p>\n<p>    while (ld-&gt;sent_len &lt; ld-&gt;total_len) {<br \/>\n        uint16_t remaining &#061; ld-&gt;total_len &#8211; ld-&gt;sent_len;<br \/>\n        uint16_t chunk_size &#061; (remaining &gt; MAX_PACKET_SIZE) ?<br \/>\n                              MAX_PACKET_SIZE : remaining;<\/p>\n<p>        \/\/ \u6dfb\u52a0\u5e8f\u5217\u53f7&#xff08;\u53ef\u9009&#xff09;<br \/>\n        uint8_t packet[MAX_PACKET_SIZE &#043; 2];<br \/>\n        packet[0] &#061; ld-&gt;packet_seq&#043;&#043;;<br \/>\n        packet[1] &#061; (remaining &gt; MAX_PACKET_SIZE) ? 0 : 1;  \/\/ \u6700\u540e\u5305\u6807\u5fd7<\/p>\n<p>        memcpy(&amp;packet[2],<br \/>\n               &amp;ld-&gt;data[ld-&gt;sent_len],<br \/>\n               chunk_size);<\/p>\n<p>        \/\/ \u53d1\u9001\u6570\u636e\u5305<br \/>\n        esp_ble_gatts_send_indicate(ld-&gt;gatts_if,<br \/>\n                                   ld-&gt;conn_id,<br \/>\n                                   ld-&gt;handle,<br \/>\n                                   chunk_size &#043; 2,<br \/>\n                                   packet,<br \/>\n                                   false);  \/\/ \u4e0d\u9700\u8981\u786e\u8ba4<\/p>\n<p>        ld-&gt;sent_len &#043;&#061; chunk_size;<\/p>\n<p>        \/\/ \u7b49\u5f85ACK\u6216\u5ef6\u8fdf&#xff08;\u907f\u514d\u62e5\u585e&#xff09;<br \/>\n        vTaskDelay(pdMS_TO_TICKS(10));<br \/>\n    }<\/p>\n<p>    free(ld);<br \/>\n    vTaskDelete(NULL);<br \/>\n}<\/p>\n<h4>5.2 \u529f\u8017\u4f18\u5316\u7b56\u7565<\/h4>\n<p>\u5bf9\u4e8e\u7535\u6c60\u4f9b\u7535\u7684\u8bbe\u5907&#xff0c;\u529f\u8017\u4f18\u5316\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u6709\u6548\u65b9\u6cd5&#xff1a;<\/p>\n<p>\u8fde\u63a5\u53c2\u6570\u4f18\u5316\u8868&#xff1a;<\/p>\n<table>\n<tr>\u5e94\u7528\u573a\u666f\u8fde\u63a5\u95f4\u9694\u4ece\u673a\u5ef6\u8fdf\u76d1\u63a7\u8d85\u65f6\u9884\u8ba1\u529f\u8017<\/tr>\n<tbody>\n<tr>\n<td>\u5b9e\u65f6\u63a7\u5236<\/td>\n<td>20-30ms<\/td>\n<td>0<\/td>\n<td>2-4s<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>\u4f20\u611f\u5668\u4e0a\u62a5<\/td>\n<td>100-200ms<\/td>\n<td>0-2<\/td>\n<td>6-8s<\/td>\n<td>\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>\u4f4e\u529f\u8017\u5f85\u673a<\/td>\n<td>1-2s<\/td>\n<td>4-6<\/td>\n<td>20-30s<\/td>\n<td>\u4f4e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u52a8\u6001\u529f\u8017\u7ba1\u7406\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>typedef enum {<br \/>\n    POWER_MODE_HIGH &#061; 0,    \/\/ \u9ad8\u6027\u80fd\u6a21\u5f0f<br \/>\n    POWER_MODE_BALANCED,    \/\/ \u5e73\u8861\u6a21\u5f0f<br \/>\n    POWER_MODE_LOW,         \/\/ \u4f4e\u529f\u8017\u6a21\u5f0f<br \/>\n} power_mode_t;<\/p>\n<p>static power_mode_t current_power_mode &#061; POWER_MODE_BALANCED;<\/p>\n<p>static void adjust_power_mode(power_mode_t new_mode)<br \/>\n{<br \/>\n    if (new_mode &#061;&#061; current_power_mode) {<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    switch (new_mode) {<br \/>\n        case POWER_MODE_HIGH:<br \/>\n            \/\/ \u5feb\u901f\u5e7f\u64ad&#xff0c;\u77ed\u8fde\u63a5\u95f4\u9694<br \/>\n            adv_params.adv_int_min &#061; 0x20;  \/\/ 20ms<br \/>\n            adv_params.adv_int_max &#061; 0x30;  \/\/ 30ms<br \/>\n            set_connection_interval(0x10, 0x20);  \/\/ 20-40ms<br \/>\n            esp_bt_controller_disable();<br \/>\n            esp_bt_controller_enable(ESP_BT_MODE_BLE);<br \/>\n            break;<\/p>\n<p>        case POWER_MODE_BALANCED:<br \/>\n            adv_params.adv_int_min &#061; 0x80;  \/\/ 80ms<br \/>\n            adv_params.adv_int_max &#061; 0x100; \/\/ 160ms<br \/>\n            set_connection_interval(0x40, 0x80);  \/\/ 64-128ms<br \/>\n            break;<\/p>\n<p>        case POWER_MODE_LOW:<br \/>\n            \/\/ \u6162\u901f\u5e7f\u64ad&#xff0c;\u957f\u8fde\u63a5\u95f4\u9694<br \/>\n            adv_params.adv_int_min &#061; 0x200;  \/\/ 320ms<br \/>\n            adv_params.adv_int_max &#061; 0x400;  \/\/ 640ms<br \/>\n            set_connection_interval(0x200, 0x400);  \/\/ 512-1024ms<\/p>\n<p>            \/\/ \u964d\u4f4e\u53d1\u5c04\u529f\u7387<br \/>\n            esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT,<br \/>\n                                ESP_PWR_LVL_N12);<br \/>\n            break;<br \/>\n    }<\/p>\n<p>    current_power_mode &#061; new_mode;<br \/>\n    ESP_LOGI(TAG, &#034;\u529f\u8017\u6a21\u5f0f\u5207\u6362\u81f3: %d&#034;, new_mode);<\/p>\n<p>    \/\/ \u91cd\u65b0\u914d\u7f6e\u5e7f\u64ad<br \/>\n    if (is_advertising) {<br \/>\n        esp_ble_gap_stop_advertising();<br \/>\n        esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6839\u636e\u7535\u6c60\u7535\u91cf\u548c\u8fde\u63a5\u72b6\u6001\u81ea\u52a8\u8c03\u6574<br \/>\nstatic void auto_adjust_power_mode(void)<br \/>\n{<br \/>\n    float battery_level &#061; read_battery_level();<br \/>\n    uint8_t connected_devices &#061; count_connected_devices();<\/p>\n<p>    if (battery_level &lt; 20.0) {<br \/>\n        \/\/ \u4f4e\u7535\u91cf&#xff0c;\u5f3a\u5236\u4f4e\u529f\u8017\u6a21\u5f0f<br \/>\n        adjust_power_mode(POWER_MODE_LOW);<br \/>\n    }<br \/>\n    else if (connected_devices &#061;&#061; 0) {<br \/>\n        \/\/ \u65e0\u8fde\u63a5&#xff0c;\u4f7f\u7528\u5e73\u8861\u6216\u4f4e\u529f\u8017\u6a21\u5f0f<br \/>\n        adjust_power_mode(battery_level &lt; 50.0 ?<br \/>\n                         POWER_MODE_LOW : POWER_MODE_BALANCED);<br \/>\n    }<br \/>\n    else {<br \/>\n        \/\/ \u6709\u8fde\u63a5&#xff0c;\u6839\u636e\u6570\u636e\u9891\u7387\u8c03\u6574<br \/>\n        if (data_update_freq &gt; 10) {  \/\/ \u9ad8\u9891\u66f4\u65b0<br \/>\n            adjust_power_mode(POWER_MODE_HIGH);<br \/>\n        } else {<br \/>\n            adjust_power_mode(POWER_MODE_BALANCED);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>5.3 \u8fde\u63a5\u7ba1\u7406\u4e0e\u72b6\u6001\u540c\u6b65<\/h4>\n<p>\u5728\u591a\u8bbe\u5907\u8fde\u63a5\u573a\u666f\u4e2d&#xff0c;\u8fde\u63a5\u7ba1\u7406\u548c\u72b6\u6001\u540c\u6b65\u662f\u96be\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8fde\u63a5\u7ba1\u7406\u5b9e\u73b0&#xff1a;<\/p>\n<p>#define MAX_DEVICES 3<\/p>\n<p>typedef struct {<br \/>\n    esp_bd_addr_t addr;<br \/>\n    uint16_t conn_id;<br \/>\n    esp_gatt_if_t gatts_if;<br \/>\n    uint32_t last_activity;<br \/>\n    device_state_t state;<br \/>\n} connected_device_t;<\/p>\n<p>static connected_device_t connected_devices[MAX_DEVICES];<br \/>\nstatic uint8_t device_count &#061; 0;<\/p>\n<p>static connected_device_t *find_device_by_addr(esp_bd_addr_t addr)<br \/>\n{<br \/>\n    for (int i &#061; 0; i &lt; device_count; i&#043;&#043;) {<br \/>\n        if (memcmp(connected_devices[i].addr, addr, 6) &#061;&#061; 0) {<br \/>\n            return &amp;connected_devices[i];<br \/>\n        }<br \/>\n    }<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>static connected_device_t *add_device(esp_bd_addr_t addr,<br \/>\n                                     uint16_t conn_id,<br \/>\n                                     esp_gatt_if_t gatts_if)<br \/>\n{<br \/>\n    if (device_count &gt;&#061; MAX_DEVICES) {<br \/>\n        ESP_LOGW(TAG, &#034;\u8fde\u63a5\u6570\u5df2\u8fbe\u4e0a\u9650&#034;);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    connected_device_t *dev &#061; &amp;connected_devices[device_count&#043;&#043;];<br \/>\n    memcpy(dev-&gt;addr, addr, 6);<br \/>\n    dev-&gt;conn_id &#061; conn_id;<br \/>\n    dev-&gt;gatts_if &#061; gatts_if;<br \/>\n    dev-&gt;last_activity &#061; xTaskGetTickCount();<br \/>\n    dev-&gt;state &#061; DEVICE_CONNECTED;<\/p>\n<p>    ESP_LOGI(TAG, &#034;\u8bbe\u5907\u5df2\u6dfb\u52a0: %02X:%02X:%02X:%02X:%02X:%02X&#034;,<br \/>\n            addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);<\/p>\n<p>    return dev;<br \/>\n}<\/p>\n<p>static void remove_device(esp_bd_addr_t addr)<br \/>\n{<br \/>\n    for (int i &#061; 0; i &lt; device_count; i&#043;&#043;) {<br \/>\n        if (memcmp(connected_devices[i].addr, addr, 6) &#061;&#061; 0) {<br \/>\n            \/\/ \u5c06\u6700\u540e\u4e00\u4e2a\u8bbe\u5907\u79fb\u5230\u5f53\u524d\u4f4d\u7f6e<br \/>\n            if (i &lt; device_count &#8211; 1) {<br \/>\n                memcpy(&amp;connected_devices[i],<br \/>\n                       &amp;connected_devices[device_count &#8211; 1],<br \/>\n                       sizeof(connected_device_t));<br \/>\n            }<br \/>\n            device_count&#8211;;<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5b9a\u671f\u68c0\u67e5\u4e0d\u6d3b\u8dc3\u8fde\u63a5<br \/>\nstatic void check_inactive_connections(void)<br \/>\n{<br \/>\n    uint32_t current_time &#061; xTaskGetTickCount();<\/p>\n<p>    for (int i &#061; 0; i &lt; device_count; i&#043;&#043;) {<br \/>\n        uint32_t inactive_time &#061; current_time &#8211;<br \/>\n                                connected_devices[i].last_activity;<\/p>\n<p>        if (inactive_time &gt; pdMS_TO_TICKS(30000)) {  \/\/ 30\u79d2\u65e0\u6d3b\u52a8<br \/>\n            ESP_LOGW(TAG, &#034;\u8bbe\u5907\u4e0d\u6d3b\u8dc3&#xff0c;\u51c6\u5907\u65ad\u5f00: %02X:%02X:%02X:%02X:%02X:%02X&#034;,<br \/>\n                    connected_devices[i].addr[0],<br \/>\n                    connected_devices[i].addr[1],<br \/>\n                    connected_devices[i].addr[2],<br \/>\n                    connected_devices[i].addr[3],<br \/>\n                    connected_devices[i].addr[4],<br \/>\n                    connected_devices[i].addr[5]);<\/p>\n<p>            \/\/ \u53d1\u9001\u65ad\u5f00\u8bf7\u6c42<br \/>\n            esp_ble_gap_disconnect(connected_devices[i].addr);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h3>6. \u8c03\u8bd5\u6280\u5de7\u4e0e\u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<p>\u5373\u4f7f\u6309\u7167\u6700\u4f73\u5b9e\u8df5\u5f00\u53d1&#xff0c;\u5728\u5b9e\u9645\u90e8\u7f72\u4e2d\u4ecd\u7136\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u6211\u5728\u591a\u4e2a\u9879\u76ee\u4e2d\u79ef\u7d2f\u7684\u8c03\u8bd5\u7ecf\u9a8c\u548c\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h4>6.1 \u7cfb\u7edf\u65e5\u5fd7\u914d\u7f6e\u4f18\u5316<\/h4>\n<p>\u5408\u7406\u7684\u65e5\u5fd7\u914d\u7f6e\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898&#xff0c;\u540c\u65f6\u907f\u514d\u65e5\u5fd7\u8f93\u51fa\u5f71\u54cd\u7cfb\u7edf\u6027\u80fd&#xff1a;<\/p>\n<p>\/\/ \u5728app_main\u4e2d\u914d\u7f6e\u65e5\u5fd7\u7ea7\u522b<br \/>\nvoid app_main(void)<br \/>\n{<br \/>\n    \/\/ \u8bbe\u7f6e\u4e0d\u540c\u6a21\u5757\u7684\u65e5\u5fd7\u7ea7\u522b<br \/>\n    esp_log_level_set(&#034;*&#034;, ESP_LOG_WARN);      \/\/ \u9ed8\u8ba4\u8b66\u544a\u7ea7\u522b<br \/>\n    esp_log_level_set(&#034;BLE_SERVER&#034;, ESP_LOG_INFO);  \/\/ \u4e3b\u6a21\u5757\u4fe1\u606f\u7ea7\u522b<br \/>\n    esp_log_level_set(&#034;GATT&#034;, ESP_LOG_DEBUG);  \/\/ GATT\u6a21\u5757\u8c03\u8bd5\u7ea7\u522b<br \/>\n    esp_log_level_set(&#034;GAP&#034;, ESP_LOG_DEBUG);   \/\/ GAP\u6a21\u5757\u8c03\u8bd5\u7ea7\u522b<\/p>\n<p>    \/\/ \u542f\u7528\u6838\u5fc3\u8f6c\u50a8&#xff08;\u7528\u4e8e\u5206\u6790\u5d29\u6e83&#xff09;<br \/>\n    esp_core_dump_init();<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u84dd\u7259<br \/>\n    ble_init();<\/p>\n<p>    \/\/ &#8230; \u5176\u4ed6\u521d\u59cb\u5316\u4ee3\u7801<br \/>\n}<\/p>\n<h4>6.2 \u5e38\u89c1\u95ee\u9898\u6392\u67e5\u8868<\/h4>\n<table>\n<tr>\u95ee\u9898\u73b0\u8c61\u53ef\u80fd\u539f\u56e0\u6392\u67e5\u65b9\u6cd5\u89e3\u51b3\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>\u8bbe\u5907\u65e0\u6cd5\u88ab\u53d1\u73b0<\/td>\n<td>\u5e7f\u64ad\u672a\u542f\u52a8\u5e7f\u64ad\u53c2\u6570\u9519\u8bef\u5c04\u9891\u5e72\u6270<\/td>\n<td>1. \u68c0\u67e5\u5e7f\u64ad\u72b6\u60012. \u9a8c\u8bc1\u5e7f\u64ad\u6570\u636e3. \u66f4\u6362\u6d4b\u8bd5\u73af\u5883<\/td>\n<td>1. \u786e\u8ba4esp_ble_gap_start_advertising\u8c03\u75282. \u68c0\u67e5\u5e7f\u64ad\u6570\u636e\u957f\u5ea6\u548c\u5185\u5bb93. \u8c03\u6574\u5e7f\u64ad\u4fe1\u9053<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u9891\u7e41\u65ad\u5f00<\/td>\n<td>\u8fde\u63a5\u53c2\u6570\u4e0d\u5408\u9002\u4fe1\u53f7\u5f3a\u5ea6\u5f31\u5185\u5b58\u4e0d\u8db3<\/td>\n<td>1. \u76d1\u63a7\u8fde\u63a5\u53c2\u6570\u4e8b\u4ef62. \u6d4b\u8bd5RSSI\u503c3. \u68c0\u67e5\u5185\u5b58\u4f7f\u7528<\/td>\n<td>1. \u8c03\u6574\u8fde\u63a5\u95f4\u9694\u548c\u8d85\u65f62. \u4f18\u5316\u5929\u7ebf\u8bbe\u8ba1\u6216\u4f4d\u7f6e3. \u51cf\u5c11\u8fde\u63a5\u6570\u6216\u670d\u52a1\u590d\u6742\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u4f20\u8f93\u6162<\/td>\n<td>MTU\u592a\u5c0f\u8fde\u63a5\u95f4\u9694\u957f\u6570\u636e\u5206\u5305\u8fc7\u591a<\/td>\n<td>1. \u68c0\u67e5MTU\u534f\u5546\u7ed3\u679c2. \u76d1\u63a7\u5b9e\u9645\u8fde\u63a5\u95f4\u96943. \u5206\u6790\u6570\u636e\u5305\u6570\u91cf<\/td>\n<td>1. \u8bf7\u6c42\u66f4\u5927\u7684MTU2. \u7f29\u77ed\u8fde\u63a5\u95f4\u96943. \u4f18\u5316\u6570\u636e\u6253\u5305<\/td>\n<\/tr>\n<tr>\n<td>\u529f\u8017\u8fc7\u9ad8<\/td>\n<td>\u5e7f\u64ad\u95f4\u9694\u592a\u77ed\u8fde\u63a5\u53c2\u6570\u6fc0\u8fdb\u5c04\u9891\u529f\u7387\u8fc7\u9ad8<\/td>\n<td>1. \u6d4b\u91cf\u5e73\u5747\u7535\u6d412. \u5206\u6790\u5404\u72b6\u6001\u8017\u65f63. \u68c0\u67e5\u53d1\u5c04\u529f\u7387\u8bbe\u7f6e<\/td>\n<td>1. \u8c03\u6574\u5e7f\u64ad\u7b56\u75652. \u4f18\u5316\u8fde\u63a5\u53c2\u65703. \u964d\u4f4e\u53d1\u5c04\u529f\u7387<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u6cc4\u6f0f<\/td>\n<td>\u52a8\u6001\u5206\u914d\u672a\u91ca\u653e\u56de\u8c03\u51fd\u6570\u8d44\u6e90\u672a\u6e05\u7406\u4efb\u52a1\u5806\u6808\u4e0d\u8db3<\/td>\n<td>1. \u4f7f\u7528\u5806\u5185\u5b58\u76d1\u63a72. \u68c0\u67e5\u6240\u6709malloc\/free3. \u76d1\u63a7\u4efb\u52a1\u5806\u6808\u4f7f\u7528<\/td>\n<td>1. \u786e\u4fdd\u6210\u5bf9\u5206\u914d\u91ca\u653e2. \u4f7f\u7528\u9759\u6001\u5206\u914d\u4f18\u51483. \u8c03\u6574\u4efb\u52a1\u5806\u6808\u5927\u5c0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>6.3 \u6027\u80fd\u76d1\u63a7\u4e0e\u4f18\u5316<\/h4>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u90e8\u7f72\u524d&#xff0c;\u5efa\u8bae\u8fdb\u884c\u5168\u9762\u7684\u6027\u80fd\u6d4b\u8bd5&#xff1a;<\/p>\n<p>\/\/ \u6027\u80fd\u76d1\u63a7\u7ed3\u6784<br \/>\ntypedef struct {<br \/>\n    uint32_t connect_count;<br \/>\n    uint32_t disconnect_count;<br \/>\n    uint32_t read_ops;<br \/>\n    uint32_t write_ops;<br \/>\n    uint32_t notify_ops;<br \/>\n    uint32_t error_count;<br \/>\n    uint32_t total_memory;<br \/>\n    uint32_t free_memory;<br \/>\n    uint32_t min_free_memory;<br \/>\n} performance_stats_t;<\/p>\n<p>static performance_stats_t stats;<\/p>\n<p>\/\/ \u5b9a\u671f\u8f93\u51fa\u6027\u80fd\u62a5\u544a<br \/>\nstatic void print_performance_report(void)<br \/>\n{<br \/>\n    ESP_LOGI(&#034;PERF&#034;, &#034;&#061;&#061;&#061; \u6027\u80fd\u62a5\u544a &#061;&#061;&#061;&#034;);<br \/>\n    ESP_LOGI(&#034;PERF&#034;, &#034;\u8fde\u63a5\u7edf\u8ba1: \u6210\u529f&#061;%d, \u65ad\u5f00&#061;%d&#034;,<br \/>\n            stats.connect_count, stats.disconnect_count);<br \/>\n    ESP_LOGI(&#034;PERF&#034;, &#034;\u64cd\u4f5c\u7edf\u8ba1: \u8bfb&#061;%d, \u5199&#061;%d, \u901a\u77e5&#061;%d&#034;,<br \/>\n            stats.read_ops, stats.write_ops, stats.notify_ops);<br \/>\n    ESP_LOGI(&#034;PERF&#034;, &#034;\u9519\u8bef\u7edf\u8ba1: \u603b\u6570&#061;%d&#034;, stats.error_count);<br \/>\n    ESP_LOGI(&#034;PERF&#034;, &#034;\u5185\u5b58\u4f7f\u7528: \u603b\u91cf&#061;%d, \u5269\u4f59&#061;%d, \u6700\u4f4e\u5269\u4f59&#061;%d&#034;,<br \/>\n            stats.total_memory, stats.free_memory, stats.min_free_memory);<\/p>\n<p>    \/\/ \u8ba1\u7b97\u8fde\u63a5\u7a33\u5b9a\u6027<br \/>\n    if (stats.connect_count &gt; 0) {<br \/>\n        float stability &#061; 100.0 * (1.0 &#8211; (float)stats.disconnect_count \/<br \/>\n                                  (float)stats.connect_count);<br \/>\n        ESP_LOGI(&#034;PERF&#034;, &#034;\u8fde\u63a5\u7a33\u5b9a\u6027: %.1f%%&#034;, stability);<br \/>\n    }<\/p>\n<p>    \/\/ \u66f4\u65b0\u6700\u5c0f\u7a7a\u95f2\u5185\u5b58<br \/>\n    if (stats.free_memory &lt; stats.min_free_memory) {<br \/>\n        stats.min_free_memory &#061; stats.free_memory;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5185\u5b58\u76d1\u63a7\u4efb\u52a1<br \/>\nstatic void memory_monitor_task(void *arg)<br \/>\n{<br \/>\n    while (1) {<br \/>\n        multi_heap_info_t info;<br \/>\n        heap_caps_get_info(&amp;info, MALLOC_CAP_DEFAULT);<\/p>\n<p>        stats.total_memory &#061; info.total_free_bytes &#043; info.total_allocated_bytes;<br \/>\n        stats.free_memory &#061; info.total_free_bytes;<\/p>\n<p>        vTaskDelay(pdMS_TO_TICKS(5000));  \/\/ \u6bcf5\u79d2\u68c0\u67e5\u4e00\u6b21<br \/>\n    }<br \/>\n}<\/p>\n<h4>6.4 \u5b9e\u9645\u90e8\u7f72\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>\u5728\u5c06\u8bbe\u5907\u90e8\u7f72\u5230\u5b9e\u9645\u73af\u5883\u524d&#xff0c;\u6709\u51e0\u4e2a\u5173\u952e\u70b9\u9700\u8981\u9a8c\u8bc1&#xff1a;<\/p>\n<li>\u5c04\u9891\u6027\u80fd\u6d4b\u8bd5&#xff1a;\u5728\u4e0d\u540c\u8ddd\u79bb\u3001\u4e0d\u540c\u969c\u788d\u7269\u73af\u5883\u4e0b\u6d4b\u8bd5\u901a\u4fe1\u53ef\u9760\u6027<\/li>\n<li>\u591a\u8bbe\u5907\u5e72\u6270\u6d4b\u8bd5&#xff1a;\u5728\u591a\u4e2a\u8bbe\u5907\u540c\u65f6\u5de5\u4f5c\u7684\u573a\u666f\u4e0b\u6d4b\u8bd5\u6027\u80fd<\/li>\n<li>\u957f\u671f\u7a33\u5b9a\u6027\u6d4b\u8bd5&#xff1a;\u8fde\u7eed\u8fd0\u884c24-72\u5c0f\u65f6&#xff0c;\u76d1\u63a7\u5185\u5b58\u4f7f\u7528\u548c\u8fde\u63a5\u7a33\u5b9a\u6027<\/li>\n<li>\u529f\u8017\u9a8c\u8bc1&#xff1a;\u4f7f\u7528\u7535\u6d41\u8868\u6d4b\u91cf\u5404\u79cd\u6a21\u5f0f\u4e0b\u7684\u5b9e\u9645\u529f\u8017<\/li>\n<li>OTA\u66f4\u65b0\u6d4b\u8bd5&#xff1a;\u9a8c\u8bc1\u56fa\u4ef6\u65e0\u7ebf\u66f4\u65b0\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c<\/li>\n<p>\u6211\u5728\u4e00\u4e2a\u5de5\u4e1a\u73af\u5883\u76d1\u6d4b\u9879\u76ee\u4e2d\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u60c5\u51b5&#xff1a;\u8bbe\u5907\u5728\u5b9e\u9a8c\u5ba4\u6d4b\u8bd5\u4e00\u5207\u6b63\u5e38&#xff0c;\u4f46\u90e8\u7f72\u5230\u73b0\u573a\u540e\u9891\u7e41\u65ad\u5f00\u8fde\u63a5\u3002\u540e\u6765\u53d1\u73b0\u662f\u73b0\u573a\u6709\u5927\u91cf\u76842.4GHz\u5e72\u6270\u6e90&#xff08;Wi-Fi\u8def\u7531\u5668\u3001\u5fae\u6ce2\u7089\u7b49&#xff09;\u3002\u901a\u8fc7\u8c03\u6574\u5e7f\u64ad\u4fe1\u9053\u548c\u8fde\u63a5\u53c2\u6570&#xff0c;\u5e76\u6dfb\u52a0\u91cd\u8bd5\u673a\u5236&#xff0c;\u6700\u7ec8\u89e3\u51b3\u4e86\u95ee\u9898\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5185\u5b58\u788e\u7247\u5316\u5bfc\u81f4\u7684\u7cfb\u7edf\u4e0d\u7a33\u5b9a\u3002\u5bf9\u4e8e\u9700\u8981\u957f\u671f\u8fd0\u884c&#xff08;\u6570\u6708\u751a\u81f3\u6570\u5e74&#xff09;\u7684\u8bbe\u5907&#xff0c;\u5efa\u8bae&#xff1a;<\/p>\n<ul>\n<li>\u5c3d\u91cf\u907f\u514d\u9891\u7e41\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d<\/li>\n<li>\u5b9a\u671f\u91cd\u542f\u670d\u52a1\u4ee5\u6e05\u7406\u5185\u5b58\u788e\u7247<\/li>\n<li>\u76d1\u63a7\u5185\u5b58\u4f7f\u7528\u8d8b\u52bf&#xff0c;\u8bbe\u7f6e\u9884\u8b66\u9608\u503c<\/li>\n<\/ul>\n<p>\u84dd\u7259\u5f00\u53d1\u6700\u9ebb\u70e6\u7684\u5f80\u5f80\u4e0d\u662f\u4ee3\u7801\u672c\u8eab&#xff0c;\u800c\u662f\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u548c\u5f02\u5e38\u5904\u7406\u3002\u6bd4\u5982\u8bbe\u5907\u7a81\u7136\u65ad\u7535\u540e\u6062\u590d\u3001\u624b\u673a\u7aef\u5e94\u7528\u5f02\u5e38\u9000\u51fa\u3001\u591a\u4e2a\u8bbe\u5907\u540c\u65f6\u8fde\u63a5\u7ade\u4e89\u8d44\u6e90\u7b49\u3002\u597d\u7684\u9519\u8bef\u5904\u7406\u548c\u6062\u590d\u673a\u5236\u80fd\u8ba9\u4ea7\u54c1\u66f4\u52a0\u53ef\u9760\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u6df1\u5ea6\u5b9e\u6218&#xff1a;\u6784\u5efa\u7a33\u5b9a\u53ef\u9760\u7684\u4f4e\u529f\u8017\u7269\u8054\u7f51\u6570\u636e\u901a\u9053<br \/>\n\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4e2d\u63a7\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u4f5c\u4e3a\u6570\u636e\u67a2\u7ebd&#xff0c;\u901a\u8fc7\u84dd\u7259\u4e0e\u591a\u4e2a\u4f20\u611f\u5668\u8282\u70b9\u901a\u4fe1\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u6848\u4f8b&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565\u53ea\u8bb2\u4e2a\u201cHello World\u201d&#xff0c;\u8981\u4e48\u4ee3\u7801\u5806\u780c\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u6298\u817e\u4e86\u4e24\u5468&#xff0c;\u8e29\u4e86\u65e0\u6570\u5751\u4e4b\u540e&#xff0c;\u6211\u51b3\u5b9a\u628a\u6574\u4e2aESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u7684\u6784\u5efa\u8fc7\u7a0b\u91cd\u65b0\u68b3\u7406\u4e00\u904d&#xff0c;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6661,8222,179,220],"topic":[],"class_list":["post-76373","post","type-post","status-publish","format-standard","hentry","category-server","tag-gatt","tag-8222","tag-esp32","tag-220"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/76373.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"ESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u6df1\u5ea6\u5b9e\u6218&#xff1a;\u6784\u5efa\u7a33\u5b9a\u53ef\u9760\u7684\u4f4e\u529f\u8017\u7269\u8054\u7f51\u6570\u636e\u901a\u9053 \u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4e2d\u63a7\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u4f5c\u4e3a\u6570\u636e\u67a2\u7ebd&#xff0c;\u901a\u8fc7\u84dd\u7259\u4e0e\u591a\u4e2a\u4f20\u611f\u5668\u8282\u70b9\u901a\u4fe1\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u6848\u4f8b&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565\u53ea\u8bb2\u4e2a\u201cHello World\u201d&#xff0c;\u8981\u4e48\u4ee3\u7801\u5806\u780c\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u6298\u817e\u4e86\u4e24\u5468&#xff0c;\u8e29\u4e86\u65e0\u6570\u5751\u4e4b\u540e&#xff0c;\u6211\u51b3\u5b9a\u628a\u6574\u4e2aESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u7684\u6784\u5efa\u8fc7\u7a0b\u91cd\u65b0\u68b3\u7406\u4e00\u904d&#xff0c;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76373.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-22T02:41:38+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76373.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76373.html\",\"name\":\"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-22T02:41:38+00:00\",\"dateModified\":\"2026-02-22T02:41:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76373.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76373.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76373.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/76373.html","og_locale":"zh_CN","og_type":"article","og_title":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"ESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u6df1\u5ea6\u5b9e\u6218&#xff1a;\u6784\u5efa\u7a33\u5b9a\u53ef\u9760\u7684\u4f4e\u529f\u8017\u7269\u8054\u7f51\u6570\u636e\u901a\u9053 \u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4e2d\u63a7\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u4f5c\u4e3a\u6570\u636e\u67a2\u7ebd&#xff0c;\u901a\u8fc7\u84dd\u7259\u4e0e\u591a\u4e2a\u4f20\u611f\u5668\u8282\u70b9\u901a\u4fe1\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u6848\u4f8b&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565\u53ea\u8bb2\u4e2a\u201cHello World\u201d&#xff0c;\u8981\u4e48\u4ee3\u7801\u5806\u780c\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u6298\u817e\u4e86\u4e24\u5468&#xff0c;\u8e29\u4e86\u65e0\u6570\u5751\u4e4b\u540e&#xff0c;\u6211\u51b3\u5b9a\u628a\u6574\u4e2aESP32\u84dd\u7259GATT\u670d\u52a1\u5668\u7684\u6784\u5efa\u8fc7\u7a0b\u91cd\u65b0\u68b3\u7406\u4e00\u904d&#xff0c;","og_url":"https:\/\/www.wsisp.com\/helps\/76373.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-22T02:41:38+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/76373.html","url":"https:\/\/www.wsisp.com\/helps\/76373.html","name":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-22T02:41:38+00:00","dateModified":"2026-02-22T02:41:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76373.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76373.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76373.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4 + VSCode\u73af\u5883\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=76373"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76373\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76373"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}