{"id":78037,"date":"2026-02-25T23:42:02","date_gmt":"2026-02-25T15:42:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78037.html"},"modified":"2026-02-25T23:42:02","modified_gmt":"2026-02-25T15:42:02","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%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78037.html","title":{"rendered":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4\uff09"},"content":{"rendered":"<h2>ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218&#xff1a;\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668&#xff08;\u57fa\u4e8eESP-IDF 4.4&#xff09;<\/h2>\n<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4f20\u611f\u5668\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u901a\u8fc7\u84dd\u7259\u5411\u624b\u673aApp\u5b9e\u65f6\u4e0a\u62a5\u6e29\u6e7f\u5ea6\u6570\u636e\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u5e16\u5b50&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565&#xff0c;\u8981\u4e48\u76f4\u63a5\u6254\u51fa\u4e00\u6bb5\u770b\u4e0d\u61c2\u7684\u4f8b\u7a0b\u4ee3\u7801\u3002\u771f\u6b63\u8981\u4ece\u96f6\u5f00\u59cb&#xff0c;\u7406\u89e3GATT\u670d\u52a1\u5668\u7684\u6bcf\u4e2a\u73af\u8282\u5e76\u8dd1\u901a\u6570\u636e\u6d41&#xff0c;\u8fd8\u662f\u5f97\u81ea\u5df1\u52a8\u624b\u8d9f\u4e00\u904d\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c31\u662f\u6211\u8fd9\u8d9f\u201c\u8d9f\u5751\u4e4b\u65c5\u201d\u7684\u5b8c\u6574\u8bb0\u5f55&#xff0c;\u6211\u4f1a\u7528\u6700\u76f4\u767d\u7684\u8bed\u8a00&#xff0c;\u62c6\u89e3ESP-IDF 4.4\u6846\u67b6\u4e0b\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u7684GATT\u670d\u52a1\u5668\u7684\u5168\u8fc7\u7a0b&#xff0c;\u4e0d\u4ec5\u544a\u8bc9\u4f60\u600e\u4e48\u505a&#xff0c;\u66f4\u89e3\u91ca\u6e05\u695a\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e5f\u662f\u7269\u8054\u7f51\u5f00\u53d1\u8005\u6216\u786c\u4ef6\u5de5\u7a0b\u5e08&#xff0c;\u6b63\u9762\u4e34\u84dd\u7259\u6570\u636e\u4ea4\u4e92\u7684\u9700\u6c42&#xff0c;\u65e0\u8bba\u662f\u7535\u6c60\u4f9b\u7535\u7684\u4f20\u611f\u5668\u3001\u53ef\u7a7f\u6234\u8bbe\u5907&#xff0c;\u8fd8\u662f\u9700\u8981\u65e0\u7ebf\u914d\u7f6e\u7684\u667a\u80fd\u786c\u4ef6&#xff0c;\u8fd9\u5957\u4ece\u521d\u59cb\u5316\u5230\u6570\u636e\u6536\u53d1\u7684\u5b9e\u6218\u6d41\u7a0b&#xff0c;\u5e94\u8be5\u80fd\u5e2e\u4f60\u7701\u4e0b\u4e0d\u5c11\u6478\u7d22\u7684\u65f6\u95f4\u3002\u6211\u4eec\u4e0d\u6b62\u6b65\u4e8e\u8ba9\u706f\u95ea\u70c1&#xff0c;\u800c\u662f\u8981\u6784\u5efa\u4e00\u4e2a\u7a33\u5b9a\u3001\u53ef\u6269\u5c55\u7684\u84dd\u7259\u670d\u52a1\u6838\u5fc3\u3002<\/p>\n<h3>1. \u9879\u76ee\u6784\u601d\u4e0e\u84dd\u7259\u534f\u8bae\u6808\u6838\u5fc3\u8ba4\u77e5<\/h3>\n<p>\u5728\u52a8\u624b\u5199\u4ee3\u7801\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u5f97\u60f3\u6e05\u695a\u4e24\u4ef6\u4e8b&#xff1a;\u6211\u4eec\u7684\u8bbe\u5907\u8981\u626e\u6f14\u4ec0\u4e48\u89d2\u8272&#xff1f;\u84dd\u7259\u534f\u8bae\u6808\u91cc\u54ea\u4e9b\u90e8\u5206\u662f\u6211\u4eec\u5fc5\u987b\u6253\u4ea4\u9053\u7684&#xff1f;<\/p>\n<p>\u5728\u84dd\u7259\u4f4e\u529f\u8017&#xff08;BLE&#xff09;\u7684\u4e16\u754c\u91cc&#xff0c;\u901a\u4fe1\u53cc\u65b9\u6709\u660e\u786e\u7684\u89d2\u8272\u5212\u5206\u3002\u6700\u5e38\u89c1\u7684\u662f\u5916\u56f4\u8bbe\u5907&#xff08;Peripheral&#xff09; \u548c\u4e2d\u592e\u8bbe\u5907&#xff08;Central&#xff09;\u3002\u6211\u4eec\u7684ESP32\u5728\u5927\u591a\u6570\u7269\u8054\u7f51\u573a\u666f\u4e0b&#xff0c;\u90fd\u662f\u4f5c\u4e3a\u5916\u56f4\u8bbe\u5907\u5b58\u5728\u7684\u2014\u2014\u6bd4\u5982\u4e00\u4e2a\u6e29\u6e7f\u5ea6\u8ba1&#xff0c;\u5b83\u7535\u91cf\u6709\u9650&#xff0c;\u9700\u8981\u88ab\u52a8\u5730\u88ab\u624b\u673a&#xff08;\u4e2d\u592e\u8bbe\u5907&#xff09;\u53d1\u73b0\u3001\u8fde\u63a5&#xff0c;\u7136\u540e\u63d0\u4f9b\u670d\u52a1\u3002GATT\u670d\u52a1\u5668\u5c31\u662f\u8fd0\u884c\u5728\u5916\u56f4\u8bbe\u5907\u4e0a&#xff0c;\u7528\u4e8e\u5bf9\u5916\u63d0\u4f9b\u6570\u636e\u670d\u52a1\u7684\u90a3\u5957\u903b\u8f91\u3002\u76f8\u53cd&#xff0c;\u624b\u673a\u4e0a\u7684App\u5219\u4f5c\u4e3aGATT\u5ba2\u6237\u7aef&#xff0c;\u6765\u53d1\u73b0\u3001\u8bfb\u53d6\u548c\u5199\u5165\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p>\u7406\u89e3\u4e86\u8fd9\u4e2a\u89d2\u8272&#xff0c;\u6211\u4eec\u518d\u6765\u770bESP-IDF\u84dd\u7259\u534f\u8bae\u6808\u7684\u67b6\u6784\u3002\u5b83\u50cf\u662f\u4e00\u4e2a\u5206\u5c42\u6e05\u6670\u7684\u5de5\u5382&#xff1a;<\/p>\n<p>\u5e94\u7528\u5c42 (Your App)<br \/>\n    |<br \/>\nGATT API \/ GAP API (\u7ba1\u7406\u5c42)<br \/>\n    |<br \/>\nBLE Controller (\u63a7\u5236\u5668)<br \/>\n    |<br \/>\n\u7269\u7406\u5c42 (Radio)<\/p>\n<p>\u6211\u4eec\u5f00\u53d1\u8005\u4e3b\u8981\u5de5\u4f5c\u5728GATT API\u548cGAP API\u8fd9\u4e00\u5c42\u3002GAP&#xff08;\u901a\u7528\u8bbf\u95ee\u914d\u7f6e\u6587\u4ef6&#xff09;\u7ba1\u201c\u5916\u4ea4\u201d&#xff1a;\u8bbe\u5907\u600e\u4e48\u88ab\u770b\u89c1&#xff08;\u5e7f\u64ad&#xff09;\u3001\u600e\u4e48\u5efa\u7acb\u548c\u65ad\u5f00\u8fde\u63a5\u3002GATT&#xff08;\u901a\u7528\u5c5e\u6027\u914d\u7f6e\u6587\u4ef6&#xff09;\u7ba1\u201c\u5185\u653f\u201d&#xff1a;\u8bbe\u5907\u5185\u90e8\u6709\u54ea\u4e9b\u6570\u636e\u670d\u52a1&#xff08;Service&#xff09;&#xff0c;\u6bcf\u4e2a\u670d\u52a1\u91cc\u6709\u54ea\u4e9b\u7279\u5f81\u503c&#xff08;Characteristic&#xff09;&#xff0c;\u8fd9\u4e9b\u503c\u80fd\u4e0d\u80fd\u8bfb\u3001\u80fd\u4e0d\u80fd\u5199\u3001\u80fd\u4e0d\u80fd\u4e3b\u52a8\u901a\u77e5&#xff08;Notify&#xff09;\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u8bef\u89e3\u662f\u4ee5\u4e3aGATT\u670d\u52a1\u8868\u662f\u4e00\u6210\u4e0d\u53d8\u7684\u3002\u5b9e\u9645\u4e0a&#xff0c;\u5b83\u662f\u4e00\u4e2a\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6784\u5efa\u7684\u6570\u636e\u5e93\u3002\u6211\u4eec\u7684\u4ee3\u7801\u9700\u8981\u5b9a\u4e49\u8fd9\u4e2a\u6570\u636e\u5e93\u7684\u201c\u84dd\u56fe\u201d&#xff08;\u5c5e\u6027\u8868&#xff09;&#xff0c;\u7136\u540e\u5728\u84dd\u7259\u534f\u8bae\u6808\u521d\u59cb\u5316\u5b8c\u6210\u540e&#xff0c;\u547d\u4ee4\u63a7\u5236\u5668\u6839\u636e\u8fd9\u4e2a\u84dd\u56fe\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u51fa\u771f\u6b63\u7684\u670d\u52a1\u53e5\u67c4\u3002\u8fd9\u4e2a\u201c\u521b\u5efa\u201d\u52a8\u4f5c\u672c\u8eab&#xff0c;\u4e5f\u662f\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6&#xff0c;\u9700\u8981\u6211\u4eec\u5728\u56de\u8c03\u51fd\u6570\u91cc\u6355\u83b7\u5e76\u5904\u7406\u3002<\/p>\n<h3>2. \u5f00\u53d1\u73af\u5883\u642d\u5efa\u4e0e\u9879\u76ee\u521d\u59cb\u5316<\/h3>\n<p>\u5de5\u6b32\u5584\u5176\u4e8b&#xff0c;\u5fc5\u5148\u5229\u5176\u5668\u3002\u867d\u7136VSCode&#043;ESP-IDF\u63d2\u4ef6\u662f\u5b98\u65b9\u63a8\u8350\u7684\u9ad8\u6548\u7ec4\u5408&#xff0c;\u4f46\u8fd9\u91cc\u6211\u60f3\u5206\u4eab\u4e00\u4e9b\u80fd\u63d0\u5347\u4f53\u9a8c\u7684\u7ec6\u8282\u914d\u7f6e&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u84dd\u7259\u5f00\u53d1\u6765\u8bf4\u3002<\/p>\n<p>\u9996\u5148&#xff0c;\u786e\u4fdd\u4f60\u7684ESP-IDF\u7248\u672c\u57284.4\u6216\u4ee5\u4e0a\u3002\u84dd\u7259\u534f\u8bae\u6808\u7684API\u57284.0\u4e4b\u540e\u6709\u8fc7\u4e00\u6b21\u8f83\u5927\u7684\u4f18\u5316&#xff0c;4.4\u7248\u672c\u5df2\u7ecf\u975e\u5e38\u7a33\u5b9a\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7idf.py &#8211;version\u6765\u67e5\u770b\u3002\u5982\u679c\u8fd8\u6ca1\u5b89\u88c5&#xff0c;\u4ece\u4e50\u946b\u5b98\u65b9GitHub\u4ed3\u5e93\u62c9\u53d6\u6307\u5b9a\u7248\u672c\u662f\u66f4\u7a33\u59a5\u7684\u505a\u6cd5\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\u65f6&#xff0c;\u4e0d\u8981\u4ece\u7a7a\u9879\u76ee\u5f00\u59cb\u3002ESP-IDF\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u793a\u4f8b&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4ecegatt_server_service_table\u8fd9\u4e2a\u4f8b\u7a0b\u7684\u526f\u672c\u5165\u624b\u3002\u4f46\u6211\u7684\u5efa\u8bae\u662f&#xff0c;\u5728\u590d\u5236\u540e&#xff0c;\u7acb\u5373\u6267\u884c\u4ee5\u4e0b\u51e0\u6b65\u201c\u6e05\u6d01\u5de5\u4f5c\u201d&#xff1a;<\/p>\n<li>\u91cd\u547d\u540d\u5e76\u7406\u89e3\u6587\u4ef6\u7ed3\u6784&#xff1a;\u5c06\u793a\u4f8b\u4e2d\u53ef\u80fd\u5b58\u5728\u7684main\/gatt_server_service_table.c\u76f4\u63a5\u6539\u540d\u4e3amain\/ble_gatt_server.c\u3002\u540c\u65f6&#xff0c;\u5728main\/CMakeLists.txt\u4e2d\u540c\u6b65\u4fee\u6539\u6e90\u6587\u4ef6\u540d\u3002\u8fd9\u8ba9\u4f60\u4ece\u5fc3\u7406\u4e0a\u89c9\u5f97\u8fd9\u662f\u201c\u81ea\u5df1\u7684\u9879\u76ee\u201d&#xff0c;\u800c\u4e0d\u662f\u5728\u4fee\u6539\u522b\u4eba\u7684\u4ee3\u7801\u3002<\/li>\n<li>\u5ba1\u67e5\u5e76\u7cbe\u7b80sdkconfig&#xff1a;\u8fd0\u884cidf.py menuconfig&#xff0c;\u91cd\u70b9\u68c0\u67e5\u4e24\u4e2a\u5730\u65b9&#xff1a;\n<ul>\n<li>Component config -&gt; Bluetooth -&gt; Bluedroid Enable&#xff1a;\u786e\u4fdd\u5df2\u542f\u7528\u3002\u8fd9\u662fESP32\u7ecf\u5178\u7684\u84dd\u7259\u534f\u8bae\u6808\u5b9e\u73b0\u3002<\/li>\n<li>Component config -&gt; Bluetooth -&gt; Bluetooth controller -&gt; BLE only&#xff1a;\u5bf9\u4e8e\u53ea\u9700BLE\u7684\u9879\u76ee&#xff0c;\u9009\u62e9\u6b64\u9879\u53ef\u4ee5\u8282\u7701\u4e00\u4e9b\u5185\u5b58\u548c\u529f\u8017\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u4e00\u4e2a\u5e72\u51c0\u7684sdkconfig.defaults\u6587\u4ef6\u53ef\u4ee5\u56fa\u5316\u8fd9\u4e9b\u914d\u7f6e&#xff0c;\u65b9\u4fbf\u56e2\u961f\u534f\u4f5c&#xff1a;<\/p>\n<p># \u84dd\u7259\u57fa\u7840\u914d\u7f6e<br \/>\nCONFIG_BT_ENABLED&#061;y<br \/>\nCONFIG_BT_BLUEDROID_ENABLED&#061;y<br \/>\nCONFIG_BT_DM_SCAN_DUPL&#061;y<br \/>\nCONFIG_BT_BLE_ENABLED&#061;y<br \/>\nCONFIG_BT_GATTS_ENABLE&#061;y<br \/>\nCONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MODE&#061;0<\/p>\n<p># \u4f18\u5316\u5185\u5b58\u4e0e\u65e5\u5fd7<br \/>\nCONFIG_BT_BTU_TASK_STACK_SIZE&#061;4096<br \/>\nCONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT&#061;30<br \/>\nCONFIG_LOG_DEFAULT_LEVEL_INFO&#061;y<\/p>\n<p>\u6ce8\u610f&#xff1a;\u84dd\u7259\u534f\u8bae\u6808\u4efb\u52a1&#xff08;\u5982BTU\u3001HCI&#xff09;\u4f1a\u6d88\u8017\u4e0d\u5c11\u5185\u5b58&#xff08;\u901a\u5e38\u9700\u89818KB\u4ee5\u4e0a\u6808\u7a7a\u95f4&#xff09;\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u8fd8\u6709\u5176\u4ed6\u7e41\u91cd\u4efb\u52a1&#xff08;\u5982Wi-Fi\u3001\u97f3\u9891\u5904\u7406&#xff09;&#xff0c;\u52a1\u5fc5\u5728menuconfig\u4e2d\u9002\u5f53\u8c03\u5927\u76f8\u5173\u4efb\u52a1\u7684\u6808\u5927\u5c0f&#xff0c;\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u6392\u67e5\u7684\u5d29\u6e83\u3002<\/p>\n<li>\u51c6\u5907\u8c03\u8bd5\u5de5\u5177&#xff1a;\u9664\u4e86\u4e32\u53e3\u65e5\u5fd7&#xff0c;\u51c6\u5907\u4e00\u4e2a\u624b\u673a\u7aef\u7684BLE\u8c03\u8bd5App\u81f3\u5173\u91cd\u8981\u3002\u6211\u5e38\u7528\u7684\u662fnRF Connect\u548cLightBlue\u3002\u5b83\u4eec\u80fd\u76f4\u89c2\u5730\u5c55\u793a\u8bbe\u5907\u7684\u5e7f\u64ad\u6570\u636e\u3001\u626b\u63cf\u5230\u7684\u670d\u52a1\u5217\u8868&#xff0c;\u5e76\u5141\u8bb8\u4f60\u624b\u52a8\u8bfb\u5199\u7279\u5f81\u503c&#xff0c;\u662f\u5f00\u53d1\u548c\u6d4b\u8bd5\u9636\u6bb5\u4e0d\u53ef\u6216\u7f3a\u7684\u201c\u53e6\u4e00\u53ea\u773c\u775b\u201d\u3002<\/li>\n<h3>3. \u84dd\u7259\u534f\u8bae\u6808\u521d\u59cb\u5316\u4e0e\u4e8b\u4ef6\u56de\u8c03\u6846\u67b6<\/h3>\n<p>\u4e00\u5207\u5c31\u4eceapp_main()\u51fd\u6570\u5f00\u59cb\u3002\u8fd9\u91cc\u7684\u521d\u59cb\u5316\u6d41\u7a0b\u50cf\u542f\u52a8\u4e00\u53f0\u7cbe\u5bc6\u4eea\u5668&#xff0c;\u987a\u5e8f\u9519\u4e86\u5c31\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<h4>3.1 \u63a7\u5236\u5668\u4e0e\u534f\u8bae\u6808\u521d\u59cb\u5316<\/h4>\n<p>\u9996\u5148&#xff0c;\u6211\u4eec\u9700\u8981\u914d\u7f6e\u5e76\u542f\u52a8\u84dd\u7259\u63a7\u5236\u5668\u3002ESP-IDF\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u914d\u7f6e\u5b8f&#xff0c;\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u6765\u8bf4&#xff0c;\u76f4\u63a5\u4f7f\u7528\u5b83\u662f\u4e2a\u5b89\u5168\u7684\u8d77\u70b9\u3002<\/p>\n<p>void ble_init(void) {<br \/>\n    \/\/ 1. \u521d\u59cb\u5316\u63a7\u5236\u5668\u914d\u7f6e&#xff08;\u4f7f\u7528\u9ed8\u8ba4\u503c&#xff09;<br \/>\n    esp_bt_controller_config_t bt_cfg &#061; BT_CONTROLLER_INIT_CONFIG_DEFAULT();<br \/>\n    \/\/ \u4f60\u53ef\u4ee5\u5728\u6b64\u8986\u76d6\u9ed8\u8ba4\u503c&#xff0c;\u4f8b\u5982\u4fee\u6539\u84dd\u7259\u6a21\u5f0f\u6216TX\u529f\u7387<br \/>\n    \/\/ bt_cfg.ble_max_conn &#061; 3; \/\/ \u6700\u5927\u8fde\u63a5\u6570<br \/>\n    \/\/ bt_cfg.controller_task_stack_size &#061; 4096; \/\/ \u63a7\u5236\u5668\u4efb\u52a1\u6808\u5927\u5c0f<\/p>\n<p>    \/\/ 2. \u521d\u59cb\u5316\u84dd\u7259\u63a7\u5236\u5668<br \/>\n    esp_err_t ret &#061; esp_bt_controller_init(&amp;bt_cfg);<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;Bluetooth controller initialize failed: %s&#034;, esp_err_to_name(ret));<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u4f7f\u80fd\u63a7\u5236\u5668&#xff08;\u9009\u62e9BLE\u6a21\u5f0f&#xff09;<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;Bluetooth controller enable failed: %s&#034;, esp_err_to_name(ret));<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u521d\u59cb\u5316\u5e76\u542f\u7528Bluedroid\u534f\u8bae\u6808<br \/>\n    ret &#061; esp_bluedroid_init();<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;Bluedroid initialize failed: %s&#034;, esp_err_to_name(ret));<br \/>\n        return;<br \/>\n    }<br \/>\n    ret &#061; esp_bluedroid_enable();<br \/>\n    if (ret !&#061; ESP_OK) {<br \/>\n        ESP_LOGE(TAG, &#034;Bluedroid enable failed: %s&#034;, esp_err_to_name(ret));<br \/>\n    }<br \/>\n}<\/p>\n<p>\u63d0\u793a&#xff1a;ESP_BT_MODE_BLE\u662f\u529f\u8017\u6700\u4f4e\u7684\u6a21\u5f0f\u3002\u5982\u679c\u4f60\u7684\u8bbe\u5907\u9700\u8981\u540c\u65f6\u652f\u6301\u7ecf\u5178\u84dd\u7259&#xff08;\u5982A2DP\u97f3\u9891&#xff09;&#xff0c;\u5219\u9700\u8981\u9009\u62e9ESP_BT_MODE_BTDM&#xff08;\u53cc\u6a21&#xff09;\u3002<\/p>\n<h4>3.2 \u7406\u89e3\u5e76\u6ce8\u518cGAP\u4e0eGATT\u4e8b\u4ef6\u56de\u8c03<\/h4>\n<p>\u534f\u8bae\u6808\u8fd0\u884c\u8d77\u6765\u540e&#xff0c;\u5b83\u5982\u4f55\u628a\u201c\u8fde\u63a5\u5efa\u7acb\u4e86\u201d\u3001\u201c\u6709\u6570\u636e\u5199\u8fdb\u6765\u4e86\u201d\u8fd9\u4e9b\u6d88\u606f\u544a\u8bc9\u6211\u4eec&#xff1f;\u7b54\u6848\u5c31\u662f\u56de\u8c03\u51fd\u6570\u3002\u6211\u4eec\u9700\u8981\u4e3aGAP\u548cGATT\u5206\u522b\u6ce8\u518c\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u5173\u952e\u6982\u5ff5&#xff1a;GATT\u63a5\u53e3&#xff08;GATT_IF&#xff09;\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u7406\u89e3\u4e3a\u4e00\u4e2a\u865a\u62df\u7684\u201c\u670d\u52a1\u7aef\u53e3\u201d\u3002\u4e00\u4e2aGATT\u5e94\u7528&#xff08;Application&#xff09;\u5bf9\u5e94\u4e00\u4e2aGATT_IF\u3002\u4e00\u4e2a\u7269\u7406\u8bbe\u5907&#xff08;ESP32&#xff09;\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2aGATT\u5e94\u7528&#xff0c;\u6bcf\u4e2a\u5e94\u7528\u7ba1\u7406\u81ea\u5df1\u7684\u4e00\u5957\u670d\u52a1\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5355\u670d\u52a1\u8bbe\u5907&#xff0c;\u6ce8\u518c\u4e00\u4e2a\u5c31\u591f\u4e86\u3002<\/p>\n<p>\/\/ GAP\u4e8b\u4ef6\u5904\u7406\u5668 &#8211; \u5904\u7406\u5e7f\u64ad\u3001\u8fde\u63a5\u7b49\u201c\u5916\u4ea4\u201d\u4e8b\u4ef6<br \/>\nstatic void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {<br \/>\n    switch (event) {<br \/>\n        \/\/ \u5f53\u539f\u59cb\u5e7f\u64ad\u6570\u636e\u8bbe\u7f6e\u5b8c\u6210\u65f6\u89e6\u53d1<br \/>\n        case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;Broadcast data set complete.&#034;);<br \/>\n            \/\/ \u901a\u5e38\u5728\u8fd9\u91cc\u68c0\u67e5\u6240\u6709\u5e7f\u64ad\u53c2\u6570\u662f\u5426\u5c31\u7eea&#xff0c;\u7136\u540e\u5f00\u59cb\u5e7f\u64ad<br \/>\n            break;<br \/>\n        \/\/ \u5f53\u5e7f\u64ad\u542f\u52a8\u5b8c\u6210\u65f6\u89e6\u53d1&#xff08;\u6210\u529f\u6216\u5931\u8d25&#xff09;<br \/>\n        case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:<br \/>\n            if (param-&gt;adv_start_cmpl.status &#061;&#061; ESP_BT_STATUS_SUCCESS) {<br \/>\n                ESP_LOGI(TAG, &#034;Broadcast started successfully.&#034;);<br \/>\n            } else {<br \/>\n                ESP_LOGE(TAG, &#034;Failed to start broadcast.&#034;);<br \/>\n            }<br \/>\n            break;<br \/>\n        \/\/ \u5f53\u6709\u8bbe\u5907\u8fde\u63a5\u4e0a\u6765\u65f6\u89e6\u53d1<br \/>\n        case ESP_GAP_BLE_CONNECT_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;Device connected.&#034;);<br \/>\n            \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u4fdd\u5b58\u8fde\u63a5\u53e5\u67c4\u548c\u5bf9\u65b9\u5730\u5740&#xff0c;\u7528\u4e8e\u540e\u7eed\u6570\u636e\u901a\u4fe1<br \/>\n            memcpy(connected_addr, param-&gt;connect.remote_bda, 6);<br \/>\n            break;<br \/>\n        \/\/ \u5f53\u8fde\u63a5\u65ad\u5f00\u65f6\u89e6\u53d1<br \/>\n        case ESP_GAP_BLE_DISCONNECT_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;Device disconnected, reason: 0x%x&#034;, param-&gt;disconnect.reason);<br \/>\n            \/\/ \u65ad\u5f00\u540e&#xff0c;\u901a\u5e38\u9700\u8981\u91cd\u65b0\u5f00\u59cb\u5e7f\u64ad\u4ee5\u7b49\u5f85\u4e0b\u4e00\u6b21\u8fde\u63a5<br \/>\n            esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n            break;<br \/>\n        default:<br \/>\n            \/\/ \u5176\u4ed6\u4e8b\u4ef6&#xff0c;\u5982\u8fde\u63a5\u53c2\u6570\u66f4\u65b0\u7b49&#xff0c;\u53ef\u6839\u636e\u9700\u8981\u5904\u7406<br \/>\n            break;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ GATT\u4e8b\u4ef6\u5904\u7406\u5668 &#8211; \u5904\u7406\u6570\u636e\u8bfb\u5199\u7b49\u201c\u5185\u653f\u201d\u4e8b\u4ef6<br \/>\nstatic void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {<br \/>\n    \/\/ \u8fd9\u4e2agatts_if\u53c2\u6570\u5f88\u91cd\u8981&#xff0c;\u5b83\u6307\u660e\u4e86\u662f\u54ea\u4e2aGATT\u5e94\u7528\u89e6\u53d1\u7684\u4e8b\u4ef6<br \/>\n    if (event &#061;&#061; ESP_GATTS_REG_EVT) {<br \/>\n        \/\/ \u5f53GATT\u5e94\u7528\u6ce8\u518c\u6210\u529f\u65f6&#xff0c;\u6211\u4eec\u62ff\u5230\u4e86\u5b83\u7684\u63a5\u53e3\u53f7(gatts_if)<br \/>\n        ESP_LOGI(TAG, &#034;GATT application registered, interface: %d&#034;, gatts_if);<br \/>\n        \/\/ \u4fdd\u5b58\u8fd9\u4e2a\u63a5\u53e3\u53f7&#xff0c;\u540e\u7eed\u521b\u5efa\u670d\u52a1\u3001\u53d1\u9001\u6570\u636e\u90fd\u8981\u7528\u5230\u5b83<br \/>\n        gl_profile_tab[PROFILE_A].gatts_if &#061; gatts_if;<br \/>\n    }<br \/>\n    \/\/ \u5c06\u4e8b\u4ef6\u5206\u53d1\u5230\u5177\u4f53\u7684Profile&#xff08;\u670d\u52a1\u7ec4&#xff09;\u5904\u7406\u51fd\u6570<br \/>\n    if (gatts_if &#061;&#061; ESP_GATT_IF_NONE || gatts_if &#061;&#061; gl_profile_tab[PROFILE_A].gatts_if) {<br \/>\n        profile_a_event_handler(event, gatts_if, param);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6ce8\u518c\u56de\u8c03\u7684\u4ee3\u7801\u653e\u5728\u534f\u8bae\u6808\u542f\u7528\u4e4b\u540e&#xff1a;<\/p>\n<p>\/\/ \u6ce8\u518cGAP\u548cGATT\u5168\u5c40\u56de\u8c03\u51fd\u6570<br \/>\nesp_ble_gap_register_callback(gap_event_handler);<br \/>\nesp_ble_gatts_register_callback(gatts_event_handler);<\/p>\n<p>\/\/ \u6ce8\u518c\u4e00\u4e2aGATT\u5e94\u7528&#xff08;\u4f1a\u89e6\u53d1ESP_GATTS_REG_EVT\u4e8b\u4ef6&#xff09;<br \/>\nesp_ble_gatts_app_register(PROFILE_A_APP_ID);<\/p>\n<p>\u8fd9\u4e2a\u9636\u6bb5\u6700\u5bb9\u6613\u9047\u5230\u7684\u5751\u662f\u4e8b\u4ef6\u987a\u5e8f\u3002\u4f60\u5fc5\u987b\u786e\u4fdd\u5728ESP_GATTS_REG_EVT\u4e8b\u4ef6\u53d1\u751f\u540e&#xff0c;\u62ff\u5230\u4e86\u6709\u6548\u7684gatts_if&#xff0c;\u624d\u80fd\u8fdb\u884c\u540e\u7eed\u7684\u670d\u52a1\u521b\u5efa\u548c\u5e7f\u64ad\u542f\u52a8\u64cd\u4f5c\u3002\u5426\u5219&#xff0c;\u51fd\u6570\u8c03\u7528\u4f1a\u5931\u8d25\u3002<\/p>\n<h3>4. \u6784\u5efaGATT\u670d\u52a1\u8868&#xff1a;\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u84dd\u56fe<\/h3>\n<p>\u8fd9\u662fGATT\u670d\u52a1\u5668\u7684\u6838\u5fc3\u3002\u4f60\u9700\u8981\u50cf\u8bbe\u8ba1\u6570\u636e\u5e93\u8868\u7ed3\u6784\u4e00\u6837&#xff0c;\u4ed4\u7ec6\u5b9a\u4e49\u4f60\u7684\u670d\u52a1\u3002\u4e00\u4e2a\u5b8c\u6574\u7684\u670d\u52a1\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206&#xff1a;<\/p>\n<table>\n<tr>\u5143\u7d20\u4f5c\u7528\u7c7b\u6bd4<\/tr>\n<tbody>\n<tr>\n<td style=\"text-align:left\">\u670d\u52a1&#xff08;Service&#xff09;<\/td>\n<td style=\"text-align:left\">\u6570\u636e\u529f\u80fd\u7684\u5bb9\u5668&#xff0c;\u5982\u201c\u7535\u6c60\u670d\u52a1\u201d\u3001\u201c\u6e29\u5ea6\u670d\u52a1\u201d\u3002<\/td>\n<td style=\"text-align:left\">\u4e00\u4e2a\u6570\u636e\u5e93&#xff08;Database&#xff09;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u7279\u5f81&#xff08;Characteristic&#xff09;<\/td>\n<td style=\"text-align:left\">\u670d\u52a1\u5185\u7684\u5177\u4f53\u6570\u636e\u70b9&#xff0c;\u5982\u201c\u7535\u6c60\u7535\u91cf\u201d\u3001\u201c\u6e29\u5ea6\u503c\u201d\u3002<\/td>\n<td style=\"text-align:left\">\u6570\u636e\u5e93\u91cc\u7684\u4e00\u5f20\u8868&#xff08;Table&#xff09;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u7279\u5f81\u503c&#xff08;Characteristic Value&#xff09;<\/td>\n<td style=\"text-align:left\">\u7279\u5f81\u7684\u5b9e\u9645\u6570\u636e\u5b58\u50a8\u4f4d\u7f6e\u3002<\/td>\n<td style=\"text-align:left\">\u8868\u91cc\u7684\u5177\u4f53\u6570\u636e\u884c&#xff08;Row&#xff09;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u63cf\u8ff0\u7b26&#xff08;Descriptor&#xff09;<\/td>\n<td style=\"text-align:left\">\u63cf\u8ff0\u6216\u914d\u7f6e\u7279\u5f81\u7684\u5143\u6570\u636e&#xff0c;\u6700\u5e38\u7528\u7684\u662fCCCD\u3002<\/td>\n<td style=\"text-align:left\">\u8868\u7684\u7d22\u5f15\u6216\u89e6\u53d1\u5668&#xff08;Index\/Trigger&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8ba9\u6211\u4eec\u7528\u4ee3\u7801\u6765\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u201c\u73af\u5883\u4f20\u611f\u5668\u670d\u52a1\u201d&#xff0c;\u5b83\u5305\u542b\u4e00\u4e2a\u53ef\u8bfb\u7684\u6e29\u5ea6\u7279\u5f81\u548c\u4e00\u4e2a\u53ef\u5199\u3001\u53ef\u901a\u77e5\u7684\u6e7f\u5ea6\u7279\u5f81\u3002<\/p>\n<h4>4.1 \u5b9a\u4e49UUID<\/h4>\n<p>UUID\u662f\u670d\u52a1\u548c\u7279\u5f81\u7684\u552f\u4e00\u8eab\u4efd\u8bc1\u300216\u4f4d\u7684\u77edUUID\u5e38\u7528\u4e8e\u6807\u51c6\u670d\u52a1&#xff08;\u59820x180F\u662f\u7535\u6c60\u670d\u52a1&#xff09;&#xff0c;128\u4f4d\u7684\u957fUUID\u7528\u4e8e\u81ea\u5b9a\u4e49\u670d\u52a1\u3002\u4e3a\u4e86\u8282\u7701\u7a7a\u95f4&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u752816\u4f4dUUID&#xff0c;\u4f46\u8981\u5728\u524d\u9762\u52a0\u4e0a\u84dd\u7259\u57fa\u7840UUID\u3002<\/p>\n<p>\/\/ \u5b9a\u4e4916\u4f4dUUID&#xff08;\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49&#xff0c;\u4f46\u907f\u514d\u4e0e\u6807\u51c6UUID\u51b2\u7a81&#xff09;<br \/>\n#define ESP_SENSOR_SERVICE_UUID        0xA001<br \/>\n#define ESP_CHAR_TEMP_READ_UUID        0xA002<br \/>\n#define ESP_CHAR_HUMI_RW_NOTIFY_UUID   0xA003<\/p>\n<p>\/\/ \u58f0\u660eUUID\u53d8\u91cf&#xff08;\u5b83\u4eec\u4f1a\u88ab\u586b\u5145\u4e3a\u5b8c\u6574\u7684128\u4f4dUUID&#xff09;<br \/>\nstatic uint16_t sensor_service_uuid &#061; ESP_SENSOR_SERVICE_UUID;<br \/>\nstatic uint16_t char_temp_read_uuid &#061; ESP_CHAR_TEMP_READ_UUID;<br \/>\nstatic uint16_t char_humi_rw_notify_uuid &#061; ESP_CHAR_HUMI_RW_NOTIFY_UUID;<\/p>\n<p>\/\/ \u5ba2\u6237\u7aef\u7279\u5f81\u914d\u7f6e\u63cf\u8ff0\u7b26&#xff08;CCCD&#xff09;\u7684UUID\u662f\u6807\u51c6\u7684<br \/>\nstatic uint16_t char_client_config_uuid &#061; ESP_GATT_UUID_CHAR_CLIENT_CONFIG;<\/p>\n<h4>4.2 \u6784\u5efa\u5c5e\u6027\u6570\u636e\u5e93<\/h4>\n<p>\u5c5e\u6027\u6570\u636e\u5e93\u662f\u4e00\u4e2aesp_gatts_attr_db_t\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94GATT\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u5c5e\u6027\u53e5\u67c4\u3002\u521b\u5efa\u7684\u987a\u5e8f\u5c31\u662f\u7d22\u5f15\u7684\u987a\u5e8f\u3002<\/p>\n<p>\/\/ \u5b9a\u4e49\u6570\u636e\u5e93\u5c5e\u6027\u6570\u91cf&#xff1a;1\u4e2a\u670d\u52a1\u58f0\u660e &#043; (2\u4e2a\u7279\u5f81 * (1\u58f0\u660e&#043;1\u503c)) &#043; 1\u4e2aCCCD &#061; 6\u4e2a\u5c5e\u6027<br \/>\n#define SENSOR_DB_NUM  6<\/p>\n<p>\/\/ \u7279\u5f81\u503c\u5c5e\u6027\u6743\u9650\u5b8f\u7ec4\u5408&#xff08;\u66f4\u6e05\u6670&#xff09;<br \/>\n#define ESP_GATT_PERM_READ_ENCRYPTED (ESP_GATT_PERM_READ | ESP_GATT_PERM_READ_ENCRYPTED)<br \/>\n#define ESP_GATT_PERM_WRITE_ENCRYPTED (ESP_GATT_PERM_WRITE | ESP_GATT_PERM_WRITE_ENCRYPTED)<\/p>\n<p>\/\/ \u7279\u5f81\u5c5e\u6027&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u901a\u77e5\u7b49&#xff09;<br \/>\nstatic uint8_t char_prop_read &#061; ESP_GATT_CHAR_PROP_BIT_READ;<br \/>\nstatic uint8_t char_prop_write &#061; ESP_GATT_CHAR_PROP_BIT_WRITE;<br \/>\nstatic uint8_t char_prop_notify &#061; ESP_GATT_CHAR_PROP_BIT_NOTIFY;<br \/>\n\/\/ \u7ec4\u5408\u5c5e\u6027&#xff1a;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u901a\u77e5<br \/>\nstatic uint8_t char_prop_rw_notify &#061; ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;<\/p>\n<p>\/\/ \u5b9e\u9645\u5b58\u50a8\u7279\u5f81\u503c\u7684\u5185\u5b58<br \/>\nstatic uint8_t temp_value[2] &#061; {0x00, 0x00}; \/\/ \u5047\u8bbe\u6e29\u5ea6\u662f16\u4f4d\u6574\u6570&#xff0c;\u5355\u4f4d0.01\u6444\u6c0f\u5ea6<br \/>\nstatic uint8_t humi_value[2] &#061; {0x00, 0x00}; \/\/ \u6e7f\u5ea6\u503c<br \/>\nstatic uint8_t humi_cccd[2] &#061; {0x00, 0x00};  \/\/ CCCD\u503c&#xff0c;\u9ed8\u8ba4\u4e3a0&#xff08;\u901a\u77e5\u5173\u95ed&#xff09;<\/p>\n<p>static const esp_gatts_attr_db_t sensor_gatt_db[SENSOR_DB_NUM] &#061; {<br \/>\n    \/\/ \u7d22\u5f150: \u4e3b\u670d\u52a1\u58f0\u660e<br \/>\n    [IDX_SVC] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP}, \/\/ \u81ea\u52a8\u54cd\u5e94\u6807\u5fd7<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;primary_service_uuid, ESP_GATT_PERM_READ,<br \/>\n         sizeof(uint16_t), sizeof(sensor_service_uuid), (uint8_t *)&amp;sensor_service_uuid}<br \/>\n    },<\/p>\n<p>    \/\/ &#8212; \u6e29\u5ea6\u7279\u5f81&#xff08;\u53ea\u8bfb&#xff09;&#8212;<br \/>\n    \/\/ \u7d22\u5f151: \u6e29\u5ea6\u7279\u5f81\u58f0\u660e<br \/>\n    [IDX_CHAR_TEMP_DECL] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_declaration_uuid, ESP_GATT_PERM_READ,<br \/>\n         sizeof(uint8_t), sizeof(char_prop_read), (uint8_t *)&amp;char_prop_read}<br \/>\n    },<br \/>\n    \/\/ \u7d22\u5f152: \u6e29\u5ea6\u7279\u5f81\u503c<br \/>\n    [IDX_CHAR_TEMP_VAL] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;char_temp_read_uuid, ESP_GATT_PERM_READ,<br \/>\n         sizeof(temp_value), sizeof(temp_value), (uint8_t *)temp_value}<br \/>\n    },<\/p>\n<p>    \/\/ &#8212; \u6e7f\u5ea6\u7279\u5f81&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u901a\u77e5&#xff09;&#8212;<br \/>\n    \/\/ \u7d22\u5f153: \u6e7f\u5ea6\u7279\u5f81\u58f0\u660e<br \/>\n    [IDX_CHAR_HUMI_DECL] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;character_declaration_uuid, ESP_GATT_PERM_READ,<br \/>\n         sizeof(uint8_t), sizeof(char_prop_rw_notify), (uint8_t *)&amp;char_prop_rw_notify}<br \/>\n    },<br \/>\n    \/\/ \u7d22\u5f154: \u6e7f\u5ea6\u7279\u5f81\u503c<br \/>\n    [IDX_CHAR_HUMI_VAL] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;char_humi_rw_notify_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,<br \/>\n         sizeof(humi_value), sizeof(humi_value), (uint8_t *)humi_value}<br \/>\n    },<br \/>\n    \/\/ \u7d22\u5f155: \u6e7f\u5ea6\u7279\u5f81\u7684CCCD&#xff08;\u7528\u4e8e\u5f00\u5173\u901a\u77e5&#xff09;<br \/>\n    [IDX_CHAR_HUMI_CCCD] &#061; {<br \/>\n        {ESP_GATT_AUTO_RSP},<br \/>\n        {ESP_UUID_LEN_16, (uint8_t *)&amp;char_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,<br \/>\n         sizeof(uint16_t), sizeof(humi_cccd), (uint8_t *)humi_cccd}<br \/>\n    },<br \/>\n};<\/p>\n<p>\u5173\u952e\u70b9\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>ESP_GATT_AUTO_RSP&#xff1a;\u8fd9\u662f\u9b54\u6cd5\u6240\u5728\u3002\u8bbe\u7f6e\u4e3a\u5b83&#xff0c;\u534f\u8bae\u6808\u4f1a\u81ea\u52a8\u5904\u7406\u8be5\u5c5e\u6027\u7684\u8bfb\/\u5199\u54cd\u5e94\u3002\u5982\u679c\u8bbe\u7f6e\u4e3aESP_GATT_RSP_BY_APP&#xff0c;\u5219\u9700\u8981\u4f60\u5728\u4e8b\u4ef6\u56de\u8c03\u4e2d\u624b\u52a8\u8c03\u7528esp_ble_gatts_send_response\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8&#xff0c;\u7528AUTO_RSP\u66f4\u7701\u5fc3\u3002<\/li>\n<li>\u6743\u9650&#xff08;Perm&#xff09;&#xff1a;\u5b9a\u4e49\u4e86\u5ba2\u6237\u7aef\u80fd\u5bf9\u8fd9\u4e2a\u5c5e\u6027\u505a\u4ec0\u4e48\u3002ESP_GATT_PERM_READ\u548cESP_GATT_PERM_WRITE\u662f\u6700\u57fa\u672c\u7684\u3002\u5982\u679c\u9700\u8981\u52a0\u5bc6\u8fde\u63a5\u540e\u624d\u80fd\u8bbf\u95ee&#xff0c;\u53ef\u4ee5\u52a0\u4e0aESP_GATT_PERM_READ_ENCRYPTED\u7b49\u3002<\/li>\n<li>\u7279\u5f81\u58f0\u660e&#xff08;Characteristic Declaration&#xff09;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5c5e\u6027&#xff0c;\u5b83\u7684\u201c\u503c\u201d\u90e8\u5206\u5b58\u653e\u7684\u4e0d\u662f\u7528\u6237\u6570\u636e&#xff0c;\u800c\u662f\u8be5\u7279\u5f81\u7684\u5c5e\u6027\u4f4d&#xff08;Properties&#xff09;&#xff08;\u5373\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u901a\u77e5\u7b49&#xff09;\u3002\u8fd9\u4e2a\u4f4d\u56fe\u5fc5\u987b\u4e0e\u540e\u9762\u7279\u5f81\u503c\u5c5e\u6027\u7684\u5b9e\u9645\u6743\u9650\u5339\u914d&#xff0c;\u5426\u5219\u5ba2\u6237\u7aef\u4f1a\u56f0\u60d1\u3002<\/li>\n<li>CCCD&#xff1a;\u5b83\u662f\u4e00\u4e2a16\u4f4d\u7684\u503c\u3002\u5f53\u5ba2\u6237\u7aef\u5199\u51650x0001\u65f6&#xff0c;\u8868\u793a\u542f\u7528\u901a\u77e5&#xff08;Notification&#xff09;&#xff1b;\u5199\u51650x0002&#xff0c;\u8868\u793a\u542f\u7528\u6307\u793a&#xff08;Indication&#xff0c;\u5e26\u786e\u8ba4\u7684\u901a\u77e5&#xff09;&#xff1b;\u5199\u51650x0000\u5219\u5173\u95ed\u3002\u670d\u52a1\u5668\u9700\u8981\u68c0\u67e5\u8fd9\u4e2a\u503c\u6765\u51b3\u5b9a\u662f\u5426\u4e3b\u52a8\u53d1\u9001\u6570\u636e\u3002<\/li>\n<\/ul>\n<h4>4.3 \u521b\u5efa\u670d\u52a1\u5e76\u542f\u52a8\u5e7f\u64ad<\/h4>\n<p>\u5c5e\u6027\u8868\u5b9a\u4e49\u597d\u540e&#xff0c;\u9700\u8981\u5728ESP_GATTS_REG_EVT\u4e8b\u4ef6\u4e2d\u521b\u5efa\u5b83\u3002<\/p>\n<p>static void profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {<br \/>\n    switch (event) {<br \/>\n        case ESP_GATTS_REG_EVT:<br \/>\n            ESP_LOGI(TAG, &#034;Creating attribute table&#8230;&#034;);<br \/>\n            \/\/ \u4f7f\u7528\u6ce8\u518c\u5f97\u5230\u7684gatts_if\u6765\u521b\u5efa\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u5c5e\u6027\u8868<br \/>\n            esp_err_t create_ret &#061; esp_ble_gatts_create_attr_tab(sensor_gatt_db, gatts_if, SENSOR_DB_NUM, 0);<br \/>\n            if (create_ret) {<br \/>\n                ESP_LOGE(TAG, &#034;Create attribute table failed, error&#061;0x%x&#034;, create_ret);<br \/>\n            }<br \/>\n            break;<\/p>\n<p>        case ESP_GATTS_CREAT_ATTR_TAB_EVT:<br \/>\n            \/\/ \u5c5e\u6027\u8868\u521b\u5efa\u5b8c\u6210\u4e8b\u4ef6<br \/>\n            if (param-&gt;add_attr_tab.status &#061;&#061; ESP_GATT_OK) {<br \/>\n                ESP_LOGI(TAG, &#034;Attribute table created, number of handles: %d&#034;, param-&gt;add_attr_tab.num_handle);<br \/>\n                \/\/ \u4fdd\u5b58\u8fd4\u56de\u7684\u53e5\u67c4\u6570\u7ec4&#xff01;\u540e\u7eed\u64cd\u4f5c\u5168\u9760\u8fd9\u4e9b\u53e5\u67c4\u3002<br \/>\n                memcpy(handle_table, param-&gt;add_attr_tab.handles, param-&gt;add_attr_tab.num_handle * sizeof(uint16_t));<br \/>\n                \/\/ \u542f\u52a8\u670d\u52a1&#xff1a;\u53c2\u6570\u662f\u670d\u52a1\u58f0\u660e\u7684\u53e5\u67c4&#xff08;\u901a\u5e38\u662f\u7b2c\u4e00\u4e2a\u53e5\u67c4&#xff09;<br \/>\n                esp_ble_gatts_start_service(handle_table[IDX_SVC]);<\/p>\n<p>                \/\/ \u670d\u52a1\u542f\u52a8\u540e&#xff0c;\u53ef\u4ee5\u5f00\u59cb\u5e7f\u64ad\u4e86<br \/>\n                start_advertising(gatts_if);<br \/>\n            }<br \/>\n            break;<br \/>\n        \/\/ &#8230; \u5176\u4ed6\u4e8b\u4ef6\u5904\u7406<br \/>\n    }<br \/>\n}<\/p>\n<p>start_advertising\u51fd\u6570\u8d1f\u8d23\u914d\u7f6e\u5e76\u542f\u52a8\u5e7f\u64ad&#xff0c;\u8ba9\u624b\u673a\u80fd\u53d1\u73b0\u6211\u4eec\u7684\u8bbe\u5907\u3002<\/p>\n<p>static void start_advertising(esp_gatt_if_t gatts_if) {<br \/>\n    \/\/ 1. \u8bbe\u7f6e\u8bbe\u5907\u540d\u79f0&#xff08;\u4f1a\u51fa\u73b0\u5728\u624b\u673a\u626b\u63cf\u5217\u8868\u4e2d&#xff09;<br \/>\n    esp_ble_gap_set_device_name(&#034;ESP32_SENSOR&#034;);<\/p>\n<p>    \/\/ 2. \u914d\u7f6e\u5e7f\u64ad\u53c2\u6570<br \/>\n    esp_ble_adv_params_t adv_params &#061; {<br \/>\n        .adv_int_min        &#061; 0x20,   \/\/ \u6700\u5c0f\u5e7f\u64ad\u95f4\u9694&#xff1a;0x20 * 0.625ms &#061; 40ms<br \/>\n        .adv_int_max        &#061; 0x40,   \/\/ \u6700\u5927\u5e7f\u64ad\u95f4\u9694&#xff1a;0x40 * 0.625ms &#061; 64ms<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, \/\/ \u5730\u5740\u7c7b\u578b<br \/>\n        .channel_map        &#061; ADV_CHNL_ALL, \/\/ \u5728\u6240\u67093\u4e2a\u5e7f\u64ad\u4fe1\u9053\u4e0a\u5e7f\u64ad<br \/>\n        .adv_filter_policy &#061; ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, \/\/ \u5141\u8bb8\u4efb\u4f55\u8bbe\u5907\u626b\u63cf\u548c\u8fde\u63a5<br \/>\n    };<\/p>\n<p>    \/\/ 3. \u914d\u7f6e\u5e7f\u64ad\u6570\u636e\u5305&#xff08;\u5fc5\u987b\u5305\u542bFlags\u548c\u90e8\u5206\u6216\u5168\u90e8UUID&#xff09;<br \/>\n    uint8_t adv_data[31] &#061; {<br \/>\n        \/\/ Flags: \u666e\u901a\u53d1\u73b0\u6a21\u5f0f&#xff0c;\u4e0d\u652f\u6301\u7ecf\u5178\u84dd\u7259<br \/>\n        0x02, 0x01, 0x06,<br \/>\n        \/\/ \u5b8c\u6574\u768416\u4f4d\u670d\u52a1UUID\u5217\u8868<br \/>\n        0x03, 0x03, 0x01, 0xA0, \/\/ \u4f4e\u5b57\u8282\u5728\u524d&#xff1a;0xA001 -&gt; 0x01, 0xA0<br \/>\n        \/\/ \u8bbe\u5907\u540d\u79f0&#xff08;\u7f29\u77ed\u7248&#xff09;<br \/>\n        0x0A, 0x09, &#039;E&#039;,&#039;S&#039;,&#039;P&#039;,&#039;3&#039;,&#039;2&#039;,&#039;_&#039;,&#039;S&#039;,&#039;e&#039;,&#039;n&#039;<br \/>\n    };<br \/>\n    esp_ble_gap_config_adv_data_raw(adv_data, sizeof(adv_data));<\/p>\n<p>    \/\/ 4. \u542f\u52a8\u5e7f\u64ad<br \/>\n    esp_ble_gap_start_advertising(&amp;adv_params);<br \/>\n}<\/p>\n<p>\u5e7f\u64ad\u542f\u52a8\u540e&#xff0c;\u7528\u624b\u673aBLE\u8c03\u8bd5App\u5e94\u8be5\u5c31\u80fd\u641c\u5230\u540d\u4e3a\u201cESP32_SENSOR\u201d\u7684\u8bbe\u5907\u4e86\u3002\u8fde\u63a5\u540e&#xff0c;\u80fd\u770b\u5230\u6211\u4eec\u5b9a\u4e49\u7684\u670d\u52a1\u548c\u7279\u5f81\u3002<\/p>\n<h3>5. \u5b9e\u73b0\u6570\u636e\u4ea4\u4e92&#xff1a;\u8bfb\u3001\u5199\u4e0e\u4e3b\u52a8\u901a\u77e5<\/h3>\n<p>\u670d\u52a1\u5efa\u597d&#xff0c;\u8fde\u63a5\u5efa\u7acb&#xff0c;\u63a5\u4e0b\u6765\u5c31\u662f\u771f\u6b63\u7684\u6570\u636e\u4ea4\u6362\u3002\u6240\u6709\u4ea4\u4e92\u90fd\u901a\u8fc7GATT\u4e8b\u4ef6\u56de\u8c03\u6765\u9a71\u52a8\u3002<\/p>\n<h4>5.1 \u5904\u7406\u8bfb\u53d6\u8bf7\u6c42<\/h4>\n<p>\u5f53\u5ba2\u6237\u7aef\u8bfb\u53d6\u4e00\u4e2a\u7279\u5f81\u503c\u65f6&#xff0c;\u5982\u679c\u8be5\u5c5e\u6027\u662fESP_GATT_AUTO_RSP&#xff0c;\u534f\u8bae\u6808\u4f1a\u81ea\u52a8\u5c06\u6211\u4eec\u5b58\u50a8\u5728\u5c5e\u6027\u8868\u4e2d\u7684\u5f53\u524d\u503c\u8fd4\u56de\u3002\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u52a8\u6001\u751f\u6210\u503c&#xff08;\u6bd4\u5982\u8bfb\u53d6\u5b9e\u65f6\u4f20\u611f\u5668\u6570\u636e&#xff09;&#xff0c;\u8fd9\u65f6\u5c31\u9700\u8981\u5728ESP_GATTS_READ_EVT\u4e8b\u4ef6\u4e2d\u5904\u7406\u3002<\/p>\n<p>case ESP_GATTS_READ_EVT: {<br \/>\n    ESP_LOGI(TAG, &#034;Read event, handle&#061;%d&#034;, param-&gt;read.handle);<br \/>\n    \/\/ \u5224\u65ad\u8bfb\u53d6\u7684\u662f\u54ea\u4e2a\u7279\u5f81<br \/>\n    if (param-&gt;read.handle &#061;&#061; handle_table[IDX_CHAR_TEMP_VAL]) {<br \/>\n        \/\/ \u5047\u8bbe\u4ece\u4f20\u611f\u5668\u8bfb\u53d6\u6700\u65b0\u6e29\u5ea6&#xff08;\u8fd9\u91cc\u662f\u6a21\u62df&#xff09;<br \/>\n        int16_t temp_actual &#061; read_temperature_sensor(); \/\/ \u5355\u4f4d0.01\u00b0C<br \/>\n        temp_value[0] &#061; temp_actual &amp; 0xFF;<br \/>\n        temp_value[1] &#061; (temp_actual &gt;&gt; 8) &amp; 0xFF;<br \/>\n        ESP_LOGI(TAG, &#034;Dynamic temp read: %d.%02d \u00b0C&#034;, temp_actual\/100, temp_actual%100);<br \/>\n        \/\/ \u56e0\u4e3a\u662fAUTO_RSP&#xff0c;\u66f4\u65b0\u4e86temp_value\u6570\u7ec4\u540e&#xff0c;\u534f\u8bae\u6808\u4f1a\u81ea\u52a8\u8fd4\u56de\u65b0\u503c\u3002<br \/>\n        \/\/ \u5982\u679c\u9700\u8981\u590d\u6742\u5904\u7406&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u5c5e\u6027\u4e3aRSP_BY_APP&#xff0c;\u5e76\u5728\u6b64\u8c03\u7528esp_ble_gatts_send_response\u3002<br \/>\n    }<br \/>\n    break;<br \/>\n}<\/p>\n<h4>5.2 \u5904\u7406\u5199\u5165\u8bf7\u6c42<\/h4>\n<p>\u5ba2\u6237\u7aef\u5199\u5165\u6570\u636e\u65f6&#xff0c;\u89e6\u53d1ESP_GATTS_WRITE_EVT\u3002\u8fd9\u662f\u63a5\u6536\u5ba2\u6237\u7aef\u6307\u4ee4\u7684\u4e3b\u8981\u65b9\u5f0f\u3002<\/p>\n<p>case ESP_GATTS_WRITE_EVT: {<br \/>\n    ESP_LOGI(TAG, &#034;Write event, handle&#061;%d, len&#061;%d&#034;, param-&gt;write.handle, param-&gt;write.len);<br \/>\n    \/\/ \u5224\u65ad\u5199\u5165\u4f4d\u7f6e<br \/>\n    if (param-&gt;write.handle &#061;&#061; handle_table[IDX_CHAR_HUMI_VAL]) {<br \/>\n        \/\/ \u5ba2\u6237\u7aef\u5199\u6e7f\u5ea6\u7279\u5f81\u503c&#xff08;\u4f8b\u5982&#xff0c;\u8bbe\u7f6e\u6e7f\u5ea6\u62a5\u8b66\u9608\u503c&#xff09;<br \/>\n        if (param-&gt;write.len &lt;&#061; sizeof(humi_value)) {<br \/>\n            memcpy(humi_value, param-&gt;write.value, param-&gt;write.len);<br \/>\n            ESP_LOGI(TAG, &#034;Humidity threshold set to new value.&#034;);<br \/>\n            \/\/ \u8fd9\u91cc\u53ef\u4ee5\u89e6\u53d1\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u53bb\u66f4\u65b0\u5b9e\u9645\u7684\u4f20\u611f\u5668\u62a5\u8b66\u903b\u8f91<br \/>\n        }<br \/>\n    }<br \/>\n    else if (param-&gt;write.handle &#061;&#061; handle_table[IDX_CHAR_HUMI_CCCD]) {<br \/>\n        \/\/ \u5ba2\u6237\u7aef\u5199CCCD&#xff0c;\u7528\u4e8e\u5f00\u542f\/\u5173\u95ed\u901a\u77e5<br \/>\n        uint16_t cccd_value &#061; param-&gt;write.value[0] | (param-&gt;write.value[1] &lt;&lt; 8);<br \/>\n        if (param-&gt;write.len &#061;&#061; 2) {<br \/>\n            if (cccd_value &#061;&#061; 0x0001) {<br \/>\n                ESP_LOGI(TAG, &#034;Notification ENABLED for humidity.&#034;);<br \/>\n                is_notification_enabled &#061; true;<br \/>\n            } else if (cccd_value &#061;&#061; 0x0000) {<br \/>\n                ESP_LOGI(TAG, &#034;Notification DISABLED for humidity.&#034;);<br \/>\n                is_notification_enabled &#061; false;<br \/>\n            }<br \/>\n            \/\/ \u66f4\u65b0\u672c\u5730CCCD\u503c&#xff0c;\u4ee5\u4fbf\u540e\u7eed\u8bfb\u53d6\u80fd\u8fd4\u56de\u6b63\u786e\u72b6\u6001<br \/>\n            humi_cccd[0] &#061; param-&gt;write.value[0];<br \/>\n            humi_cccd[1] &#061; param-&gt;write.value[1];<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5982\u679c\u9700\u8981\u54cd\u5e94&#xff08;\u975eAUTO_RSP&#xff09;&#xff0c;\u5728\u8fd9\u91cc\u53d1\u9001\u54cd\u5e94<br \/>\n    \/\/ if (!param-&gt;write.need_rsp) { &#8230; }<br \/>\n    break;<br \/>\n}<\/p>\n<h4>5.3 \u4e3b\u52a8\u53d1\u9001\u901a\u77e5&#xff08;Notification&#xff09;<\/h4>\n<p>\u8fd9\u662f\u670d\u52a1\u5668\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6570\u636e\u7684\u673a\u5236&#xff0c;\u975e\u5e38\u9002\u5408\u4f20\u611f\u5668\u6570\u636e\u5468\u671f\u6027\u4e0a\u62a5\u3002<\/p>\n<p>\/\/ \u5047\u8bbe\u5728\u4e00\u4e2a\u5b9a\u65f6\u5668\u56de\u8c03\u6216\u4f20\u611f\u5668\u8bfb\u53d6\u4efb\u52a1\u4e2d<br \/>\nvoid sensor_task(void *arg) {<br \/>\n    while(1) {<br \/>\n        vTaskDelay(pdMS_TO_TICKS(2000)); \/\/ \u6bcf2\u79d2\u4e00\u6b21<br \/>\n        if (is_connected &amp;&amp; is_notification_enabled) {<br \/>\n            \/\/ 1. \u8bfb\u53d6\u6700\u65b0\u7684\u4f20\u611f\u5668\u6570\u636e&#xff08;\u4f8b\u5982\u6e7f\u5ea6&#xff09;<br \/>\n            uint16_t latest_humi &#061; read_humidity_sensor();<br \/>\n            uint8_t send_data[2];<br \/>\n            send_data[0] &#061; latest_humi &amp; 0xFF;<br \/>\n            send_data[1] &#061; (latest_humi &gt;&gt; 8) &amp; 0xFF;<\/p>\n<p>            \/\/ 2. \u66f4\u65b0\u672c\u5730\u7279\u5f81\u503c&#xff08;\u53ef\u9009&#xff0c;\u4f46\u4fdd\u6301\u540c\u6b65\u662f\u597d\u7684\u505a\u6cd5&#xff09;<br \/>\n            memcpy(humi_value, send_data, 2);<\/p>\n<p>            \/\/ 3. \u53d1\u9001\u901a\u77e5<br \/>\n            esp_err_t send_ret &#061; esp_ble_gatts_send_indicate(<br \/>\n                gatts_if,           \/\/ GATT\u63a5\u53e3<br \/>\n                conn_id,            \/\/ \u8fde\u63a5ID&#xff08;\u4eceESP_GATTS_CONNECT_EVT\u83b7\u53d6&#xff09;<br \/>\n                handle_table[IDX_CHAR_HUMI_VAL], \/\/ \u6e7f\u5ea6\u7279\u5f81\u503c\u7684\u53e5\u67c4<br \/>\n                sizeof(send_data),  \/\/ \u6570\u636e\u957f\u5ea6<br \/>\n                send_data,          \/\/ \u6570\u636e\u6307\u9488<br \/>\n                false               \/\/ \u662f\u5426\u662fIndication&#xff08;\u9700\u8981\u786e\u8ba4&#xff09;&#xff1f;false\u8868\u793aNotification<br \/>\n            );<br \/>\n            if (send_ret !&#061; ESP_OK) {<br \/>\n                ESP_LOGE(TAG, &#034;Send notification failed: %s&#034;, esp_err_to_name(send_ret));<br \/>\n                \/\/ \u53d1\u9001\u5931\u8d25\u53ef\u80fd\u56e0\u4e3a\u8fde\u63a5\u5df2\u65ad\u5f00&#xff0c;\u53ef\u4ee5\u5728\u6b64\u68c0\u67e5\u5e76\u66f4\u65b0\u72b6\u6001<br \/>\n            } else {<br \/>\n                ESP_LOGI(TAG, &#034;Notification sent, humidity: %d&#034;, latest_humi);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>Notification vs Indication&#xff1a;\u4e24\u8005\u90fd\u7528\u4e8e\u670d\u52a1\u5668\u4e3b\u52a8\u53d1\u9001\u6570\u636e\u3002\u533a\u522b\u5728\u4e8eIndication\u8981\u6c42\u5ba2\u6237\u7aef\u6536\u5230\u540e\u5fc5\u987b\u56de\u590d\u4e00\u4e2a\u786e\u8ba4&#xff08;Confirmation&#xff09;&#xff0c;\u534f\u8bae\u6808\u4f1a\u56e0\u6b64\u89e6\u53d1\u4e00\u4e2aESP_GATTS_CONF_EVT\u4e8b\u4ef6\u3002Indication\u66f4\u53ef\u9760&#xff0c;\u4f46\u5ef6\u8fdf\u7a0d\u9ad8&#xff1b;Notification\u66f4\u8f7b\u5feb&#xff0c;\u4f46\u53ef\u80fd\u4e22\u5305\u3002\u6839\u636e\u6570\u636e\u91cd\u8981\u6027\u9009\u62e9\u3002<\/p>\n<h3>6. \u8fde\u63a5\u7ba1\u7406\u3001\u5b89\u5168\u4e0e\u529f\u8017\u4f18\u5316<\/h3>\n<p>\u4e00\u4e2a\u5065\u58ee\u7684GATT\u670d\u52a1\u5668\u4e0d\u80fd\u53ea\u7ba1\u529f\u80fd&#xff0c;\u8fd8\u5f97\u8003\u8651\u7a33\u5b9a\u6027\u3001\u5b89\u5168\u6027\u548c\u7eed\u822a\u3002<\/p>\n<h4>6.1 \u8fde\u63a5\u53c2\u6570\u534f\u5546<\/h4>\n<p>\u8fde\u63a5\u5efa\u7acb\u540e&#xff08;ESP_GATTS_CONNECT_EVT&#xff09;&#xff0c;\u4e2d\u592e\u8bbe\u5907&#xff08;\u624b\u673a&#xff09;\u4f1a\u63d0\u8bae\u4e00\u5957\u8fde\u63a5\u53c2\u6570&#xff08;\u95f4\u9694\u3001\u5ef6\u8fdf\u3001\u8d85\u65f6&#xff09;\u3002\u5916\u56f4\u8bbe\u5907\u53ef\u4ee5\u63a5\u53d7&#xff0c;\u4e5f\u53ef\u4ee5\u53d1\u8d77\u66f4\u65b0\u8bf7\u6c42\u4ee5\u83b7\u5f97\u66f4\u4f18\u7684\u529f\u8017\u6216\u541e\u5410\u91cf\u3002<\/p>\n<p>case ESP_GATTS_CONNECT_EVT: {<br \/>\n    conn_id &#061; param-&gt;connect.conn_id;<br \/>\n    memcpy(connected_addr, param-&gt;connect.remote_bda, 6);<br \/>\n    is_connected &#061; true;<\/p>\n<p>    \/\/ \u53d1\u8d77\u8fde\u63a5\u53c2\u6570\u66f4\u65b0\u8bf7\u6c42&#xff08;\u66f4\u7701\u7535\u7684\u53c2\u6570&#xff09;<br \/>\n    esp_ble_conn_update_params_t conn_params &#061; {0};<br \/>\n    memcpy(conn_params.bda, connected_addr, sizeof(esp_bd_addr_t));<br \/>\n    conn_params.latency &#061; 0;       \/\/ \u4ece\u673a\u5ef6\u8fdf<br \/>\n    conn_params.max_int &#061; 0x18;    \/\/ \u6700\u5927\u8fde\u63a5\u95f4\u9694&#xff1a;0x18 * 1.25ms &#061; 30ms<br \/>\n    conn_params.min_int &#061; 0x0C;    \/\/ \u6700\u5c0f\u8fde\u63a5\u95f4\u9694&#xff1a;0x0C * 1.25ms &#061; 15ms<br \/>\n    conn_params.timeout &#061; 600;     \/\/ \u76d1\u63a7\u8d85\u65f6&#xff1a;600 * 10ms &#061; 6s<\/p>\n<p>    esp_ble_gap_update_conn_params(&amp;conn_params);<br \/>\n    break;<br \/>\n}<\/p>\n<p>\u66f4\u5c0f\u7684\u8fde\u63a5\u95f4\u9694\u610f\u5473\u7740\u66f4\u5feb\u7684\u54cd\u5e94\u901f\u5ea6&#xff0c;\u4f46\u529f\u8017\u66f4\u9ad8\u3002\u9700\u8981\u6839\u636e\u5e94\u7528\u573a\u666f&#xff08;\u662f\u9891\u7e41\u4ea4\u4e92\u7684\u9065\u63a7\u5668&#xff0c;\u8fd8\u662f\u51e0\u5206\u949f\u4e0a\u62a5\u4e00\u6b21\u7684\u4f20\u611f\u5668&#xff09;\u6765\u6743\u8861\u3002<\/p>\n<h4>6.2 \u5b9e\u73b0\u7b80\u5355\u7684\u914d\u5bf9\u4e0e\u7ed1\u5b9a<\/h4>\n<p>\u5bf9\u4e8e\u9700\u8981\u9632\u6b62\u6570\u636e\u88ab\u7a83\u542c\u6216\u7be1\u6539\u7684\u5e94\u7528&#xff0c;\u53ef\u4ee5\u542f\u7528BLE\u5b89\u5168\u914d\u5bf9\u3002ESP-IDF\u652f\u6301Just Works\u3001Passkey Entry\u7b49\u591a\u79cd\u914d\u5bf9\u65b9\u5f0f\u3002<\/p>\n<p>\/\/ \u5728\u5e7f\u64ad\u542f\u52a8\u524d&#xff0c;\u8bbe\u7f6e\u5b89\u5168\u53c2\u6570<br \/>\nstatic void set_security(void) {<br \/>\n    esp_ble_auth_req_t auth_req &#061; ESP_LE_AUTH_REQ_SC_MITM_BOND; \/\/ \u8981\u6c42\u5b89\u5168\u8fde\u63a5\u3001MITM\u4fdd\u62a4\u3001\u7ed1\u5b9a<br \/>\n    esp_ble_io_cap_t iocap &#061; ESP_IO_CAP_NONE; \/\/ \u65e0\u8f93\u5165\u8f93\u51fa\u80fd\u529b&#xff08;Just Works\u914d\u5bf9&#xff09;<br \/>\n    uint8_t init_key &#061; ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;<br \/>\n    uint8_t rsp_key &#061; ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;<\/p>\n<p>    esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &amp;auth_req, sizeof(uint8_t));<br \/>\n    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &amp;iocap, sizeof(uint8_t));<br \/>\n    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &amp;init_key, sizeof(uint8_t));<br \/>\n    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &amp;rsp_key, sizeof(uint8_t));<br \/>\n}<\/p>\n<p>\u542f\u7528\u5b89\u5168\u540e&#xff0c;\u7279\u5f81\u503c\u7684\u6743\u9650\u53ef\u80fd\u9700\u8981\u52a0\u4e0aESP_GATT_PERM_READ_ENCRYPTED\u6216ESP_GATT_PERM_WRITE_ENCRYPTED&#xff0c;\u786e\u4fdd\u53ea\u6709\u5728\u52a0\u5bc6\u8fde\u63a5\u4e0b\u624d\u80fd\u8bbf\u95ee\u3002<\/p>\n<h4>6.3 \u6df1\u5ea6\u7761\u7720\u4e0e\u5e7f\u64ad\u4f18\u5316<\/h4>\n<p>\u5bf9\u4e8e\u7535\u6c60\u4f9b\u7535\u7684\u8bbe\u5907&#xff0c;\u529f\u8017\u662f\u751f\u547d\u7ebf\u3002ESP32\u7684\u84dd\u7259\u63a7\u5236\u5668\u5728\u6df1\u5ea6\u7761\u7720\u65f6\u53ef\u4ee5\u88ab\u5524\u9192&#xff0c;\u4f46\u534f\u8bae\u6808\u72b6\u6001\u4f1a\u4e22\u5931&#xff0c;\u9700\u8981\u91cd\u65b0\u521d\u59cb\u5316\u3002\u4e00\u4e2a\u66f4\u5b9e\u7528\u7684\u4f4e\u529f\u8017\u7b56\u7565\u662f\u4f18\u5316\u5e7f\u64ad\u548c\u8fde\u63a5\u53c2\u6570\u3002<\/p>\n<ul>\n<li>\u964d\u4f4e\u5e7f\u64ad\u9891\u7387&#xff1a;\u589e\u5927adv_int_min\u548cadv_int_max\u3002\u727a\u7272\u88ab\u53d1\u73b0\u7684\u901f\u5ea6&#xff0c;\u6362\u53d6\u66f4\u4f4e\u7684\u5f85\u673a\u529f\u8017\u3002<\/li>\n<li>\u4f7f\u7528\u5b9a\u5411\u5e7f\u64ad&#xff1a;\u5982\u679c\u53ea\u4e0e\u7279\u5b9a\u4e2d\u592e\u8bbe\u5907\u8fde\u63a5&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u5b9a\u5411\u5e7f\u64ad&#xff0c;\u4f46\u9650\u5236\u8f83\u591a\u3002<\/li>\n<li>\u5feb\u901f\u65ad\u5f00\u4e0e\u7761\u7720&#xff1a;\u5728\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u540e&#xff0c;\u53ef\u4ee5\u4e3b\u52a8\u65ad\u5f00\u8fde\u63a5&#xff08;esp_ble_gap_disconnect&#xff09;&#xff0c;\u7136\u540e\u8ba9ESP32\u8fdb\u5165\u8f7b\u7761\u7720\u6216\u6df1\u5ea6\u7761\u7720&#xff0c;\u5b9a\u65f6\u9192\u6765\u5e7f\u64ad\u4e00\u5c0f\u6bb5\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>\/\/ \u4e00\u4e2a\u7b80\u5355\u7684\u8282\u80fd\u7b56\u7565\u793a\u4f8b<br \/>\nstatic void enter_low_power_mode(void) {<br \/>\n    if (is_connected) {<br \/>\n        \/\/ \u5982\u679c\u8fd8\u8fde\u7740&#xff0c;\u5148\u53d1\u5b8c\u6700\u540e\u7684\u6570\u636e\u518d\u8003\u8651\u65ad\u5f00<br \/>\n        return;<br \/>\n    }<br \/>\n    \/\/ \u505c\u6b62\u5e7f\u64ad<br \/>\n    esp_ble_gap_stop_advertising();<br \/>\n    \/\/ \u8bbe\u7f6e\u66f4\u7a00\u758f\u7684\u5e7f\u64ad\u53c2\u6570&#xff08;\u4ec5\u5f53\u9700\u8981\u88ab\u8fde\u63a5\u65f6&#xff09;<br \/>\n    \/\/ \u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u8fd9\u91cc\u53ef\u80fd\u4f1a\u89e6\u53d1\u4e00\u4e2a\u5b9a\u65f6\u5668&#xff0c;\u4f11\u7720\u4e00\u6bb5\u65f6\u95f4\u540e\u518d\u5524\u9192\u5e7f\u64ad<br \/>\n    ESP_LOGI(TAG, &#034;Entering low power state.&#034;);<br \/>\n}<\/p>\n<p>\u8c03\u8bd5\u529f\u8017\u65f6&#xff0c;\u4e00\u5b9a\u8981\u7528\u5b9e\u9645\u7684\u4e07\u7528\u8868\u6216\u529f\u8017\u5206\u6790\u4eea\u6d4b\u91cf&#xff0c;\u8f6f\u4ef6\u4f30\u7b97\u5f80\u5f80\u4e0d\u51c6\u786e\u3002\u5173\u6ce8\u5e7f\u64ad\u671f\u95f4\u3001\u8fde\u63a5\u671f\u95f4\u3001\u4ee5\u53ca\u7761\u7720\u671f\u95f4\u7684\u7535\u6d41\u66f2\u7ebf\u3002<\/p>\n<h3>7. \u8c03\u8bd5\u6280\u5de7\u4e0e\u5e38\u89c1\u95ee\u9898\u6392\u96f7<\/h3>\n<p>\u5373\u4f7f\u6309\u7167\u6b65\u9aa4\u6765&#xff0c;\u4e5f\u96be\u514d\u4f1a\u9047\u5230\u5404\u79cd\u5947\u602a\u7684\u95ee\u9898\u3002\u8fd9\u91cc\u5206\u4eab\u51e0\u4e2a\u6211\u8e29\u8fc7\u7684\u5751\u548c\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u95ee\u98981&#xff1a;\u624b\u673a\u641c\u4e0d\u5230\u8bbe\u5907<\/p>\n<ul>\n<li>\u68c0\u67e5\u5e7f\u64ad\u6570\u636e&#xff1a;\u5e7f\u64ad\u6570\u636e\u5305\u7ed3\u6784\u5fc5\u987b\u6b63\u786e\u3002\u81f3\u5c11\u5305\u542bFlags&#xff08;0x01&#xff09;\u548cComplete List of 16-bit Service UUIDs&#xff08;0x03&#xff09;\u6216Shortened Local Name&#xff08;0x08&#xff09;\u7b49\u5b57\u6bb5\u3002\u7528esp_ble_gap_config_adv_data_raw\u914d\u7f6e\u540e&#xff0c;\u67e5\u770bESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT\u4e8b\u4ef6\u662f\u5426\u6210\u529f\u3002<\/li>\n<li>\u68c0\u67e5\u7269\u7406\u5c42&#xff1a;\u786e\u4fdd\u5929\u7ebf\u8fde\u63a5\u6b63\u5e38&#xff0c;\u6ca1\u6709\u91d1\u5c5e\u5916\u58f3\u4e25\u91cd\u5c4f\u853d\u4fe1\u53f7\u3002\u5c1d\u8bd5\u589e\u52a0tx_power\u3002<\/li>\n<li>\u68c0\u67e5\u624b\u673aApp&#xff1a;\u6709\u4e9b\u624b\u673a\u7cfb\u7edf\u5bf9BLE\u626b\u63cf\u6709\u540e\u53f0\u9650\u5236&#xff0c;\u786e\u4fddApp\u5728\u524d\u53f0&#xff0c;\u5e76\u5c1d\u8bd5\u91cd\u542f\u624b\u673a\u84dd\u7259\u3002<\/li>\n<\/ul>\n<p>\u95ee\u98982&#xff1a;\u8fde\u63a5\u540e\u7acb\u5373\u65ad\u5f00<\/p>\n<ul>\n<li>\u67e5\u770b\u65ad\u5f00\u539f\u56e0\u7801&#xff1a;\u5728ESP_GAP_BLE_DISCONNECT_EVT\u4e8b\u4ef6\u4e2d&#xff0c;param-&gt;disconnect.reason\u4f1a\u7ed9\u51fa\u539f\u56e0\u30020x08\u901a\u5e38\u662f\u8fde\u63a5\u8d85\u65f6&#xff0c;0x3E\u53ef\u80fd\u662f\u672c\u5730\u4e3b\u673a\u7ec8\u6b62\u3002\u68c0\u67e5\u8fde\u63a5\u53c2\u6570\u662f\u5426\u8fc7\u4e8e\u6781\u7aef&#xff08;\u95f4\u9694\u592a\u77ed\u6216\u8d85\u65f6\u592a\u957f&#xff09;\u3002<\/li>\n<li>MTU\u95ee\u9898&#xff1a;\u9ed8\u8ba4MTU\u662f23\u5b57\u8282\u3002\u5982\u679c\u7b2c\u4e00\u6b21\u6570\u636e\u4ea4\u6362\u5c31\u5c1d\u8bd5\u53d1\u9001\u8d85\u8fc7\u8fd9\u4e2a\u957f\u5ea6\u7684\u6570\u636e&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u3002\u53ef\u4ee5\u5728\u8fde\u63a5\u540e\u534f\u5546\u66f4\u5927\u7684MTU&#xff08;esp_ble_gattc_send_mtu_req&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u95ee\u98983&#xff1a;\u901a\u77e5\u53d1\u9001\u5931\u8d25&#xff0c;\u8fd4\u56de\u9519\u8bef\u78010x85&#xff08;ESP_GATT_NOT_FOUND&#xff09;<\/p>\n<ul>\n<li>\u53e5\u67c4\u9519\u8bef&#xff1a;\u786e\u4fddesp_ble_gatts_send_indicate\u4e2d\u4f7f\u7528\u7684\u7279\u5f81\u503c\u53e5\u67c4\u662f\u6b63\u786e\u7684&#xff0c;\u5e76\u4e14\u662f\u8be5\u7279\u5f81\u503c\u7684\u53e5\u67c4&#xff0c;\u4e0d\u662f\u7279\u5f81\u58f0\u660e\u7684\u53e5\u67c4\u3002<\/li>\n<li>CCCD\u672a\u542f\u7528&#xff1a;\u786e\u8ba4\u5ba2\u6237\u7aef\u5df2\u7ecf\u6210\u529f\u5199\u5165\u4e860x0001\u5230CCCD&#xff0c;\u5e76\u4e14\u4f60\u7684\u4ee3\u7801\u6b63\u786e\u8bbe\u7f6e\u4e86is_notification_enabled\u6807\u5fd7\u3002<\/li>\n<li>\u8fde\u63a5\u5df2\u5931\u6548&#xff1a;\u53d1\u9001\u524d\u68c0\u67e5is_connected\u6807\u5fd7&#xff0c;\u5e76\u786e\u4fddconn_id\u662f\u5f53\u524d\u6709\u6548\u8fde\u63a5\u7684ID\u3002\u8fde\u63a5\u65ad\u5f00\u540e&#xff0c;\u65e7\u7684conn_id\u4f1a\u5931\u6548\u3002<\/li>\n<\/ul>\n<p>\u95ee\u98984&#xff1a;\u5185\u5b58\u4e0d\u8db3\u6216\u4efb\u52a1\u6808\u6ea2\u51fa<br \/>\n\u84dd\u7259\u534f\u8bae\u6808\u4efb\u52a1\u9700\u8981\u8db3\u591f\u7684\u6808\u7a7a\u95f4\u3002\u5982\u679c\u51fa\u73b0***ERROR*** A stack overflow in task\u4e4b\u7c7b\u7684\u9519\u8bef&#xff0c;\u6216\u8005\u7cfb\u7edf\u4e0d\u7a33\u5b9a&#xff0c;\u8bf7\u589e\u5927\u76f8\u5173\u4efb\u52a1\u7684\u6808\u914d\u7f6e\u3002<\/p>\n<p># \u5728menuconfig\u4e2d\u8c03\u6574<br \/>\nComponent config -&gt; Bluetooth -&gt; Bluedroid options -&gt; GATT task stack size (\u5efa\u8bae &gt;&#061; 3072)<br \/>\nComponent config -&gt; Bluetooth -&gt; Bluetooth controller -&gt; BLE task stack size (\u5efa\u8bae &gt;&#061; 2048)<\/p>\n<p>\u6700\u6709\u6548\u7684\u8c03\u8bd5\u65b9\u6cd5&#xff1a;\u5206\u5c42\u65e5\u5fd7\u4e0e\u5bf9\u6bd4\u9a8c\u8bc1<\/p>\n<li>\u6253\u5f00\u8be6\u7ec6\u65e5\u5fd7&#xff1a;\u5728sdkconfig\u4e2d\u8bbe\u7f6eCONFIG_LOG_DEFAULT_LEVEL_DEBUG&#xff0c;\u67e5\u770b\u534f\u8bae\u6808\u7684\u6bcf\u4e00\u6b65\u4ea4\u4e92\u3002<\/li>\n<li>\u4f7f\u7528\u624b\u673aApp\u5bf9\u6bd4&#xff1a;\u7528nRF Connect\u8fde\u63a5\u4e00\u4e2a\u5df2\u77e5\u6b63\u5e38\u7684BLE\u8bbe\u5907&#xff08;\u6bd4\u5982\u5c0f\u7c73\u624b\u73af&#xff09;&#xff0c;\u89c2\u5bdf\u5b83\u7684\u670d\u52a1\u7ed3\u6784\u3001\u5e7f\u64ad\u6570\u636e&#xff0c;\u4e0e\u4f60\u7684\u8bbe\u5907\u5bf9\u6bd4\u3002\u8fd9\u662f\u53d1\u73b0\u914d\u7f6e\u5dee\u5f02\u7684\u6700\u5feb\u65b9\u5f0f\u3002<\/li>\n<li>\u7b80\u5316\u518d\u590d\u6742\u5316&#xff1a;\u5f53\u529f\u80fd\u4e0d\u6b63\u5e38\u65f6&#xff0c;\u9000\u56de\u6700\u7b80\u5355\u7684\u4f8b\u7a0b&#xff08;\u53ea\u5305\u542b\u4e00\u4e2a\u53ef\u8bfb\u7279\u5f81&#xff09;&#xff0c;\u786e\u4fdd\u5b83\u80fd\u5de5\u4f5c\u3002\u7136\u540e&#xff0c;\u50cf\u642d\u79ef\u6728\u4e00\u6837&#xff0c;\u4e00\u4e2a\u4e00\u4e2a\u5730\u6dfb\u52a0\u7279\u5f81\u3001\u63cf\u8ff0\u7b26\u3001\u901a\u77e5\u529f\u80fd&#xff0c;\u6bcf\u52a0\u4e00\u6b65\u90fd\u6d4b\u8bd5\u9a8c\u8bc1\u3002<\/li>\n<p>\u6700\u540e&#xff0c;\u4ee3\u7801\u7684\u5065\u58ee\u6027\u79bb\u4e0d\u5f00\u5f02\u5e38\u5904\u7406\u3002\u6bcf\u4e00\u4e2aesp_ble_\u5f00\u5934\u7684\u51fd\u6570\u8c03\u7528&#xff0c;\u90fd\u5e94\u8be5\u68c0\u67e5\u5176\u8fd4\u56de\u503c\u3002\u91cd\u8981\u7684\u5168\u5c40\u72b6\u6001&#xff08;\u5982\u8fde\u63a5\u72b6\u6001\u3001CCCD\u72b6\u6001&#xff09;\u8981\u8003\u8651\u591a\u4efb\u52a1\u8bbf\u95ee\u7684\u4e92\u65a5\u4fdd\u62a4&#xff0c;\u867d\u7136\u5728\u8fd9\u4e2a\u7b80\u5355\u670d\u52a1\u5668\u4e2d\u53ef\u80fd\u4e0d\u5fc5\u8981&#xff0c;\u4f46\u5728\u590d\u6742\u7684\u3001\u5305\u542bWi-Fi\u7b49\u529f\u80fd\u7684\u9879\u76ee\u4e2d&#xff0c;\u8fd9\u80fd\u907f\u514d\u8bb8\u591a\u7384\u5b66\u95ee\u9898\u3002<\/p>\n<p>\u6298\u817eESP32\u84dd\u7259\u5927\u534a\u5e74&#xff0c;\u6700\u5927\u7684\u4f53\u4f1a\u662f&#xff1a;\u7406\u89e3\u534f\u8bae\u672c\u8eab\u6bd4\u6b7b\u8bb0\u4ee3\u7801\u66f4\u91cd\u8981\u3002\u5f53\u4f60\u660e\u767d\u4e86GATT\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u5c5e\u6027\u8868&#xff0c;\u901a\u77e5\u662fCCCD\u8fd9\u4e2a\u201c\u5f00\u5173\u201d\u63a7\u5236\u7684&#xff0c;\u5f88\u591a\u95ee\u9898\u5c31\u81ea\u7136\u6709\u4e86\u6392\u67e5\u65b9\u5411\u3002\u5e0c\u671b\u8fd9\u4efd\u7ed3\u5408\u4e86\u5b9e\u6218\u548c\u539f\u7406\u7684\u68b3\u7406&#xff0c;\u80fd\u8ba9\u4f60\u5728\u4e0b\u6b21\u70b9\u4eaeESP32\u84dd\u7259\u65f6&#xff0c;\u5fc3\u91cc\u66f4\u6709\u5e95\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218&#xff1a;\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668&#xff08;\u57fa\u4e8eESP-IDF 4.4&#xff09;<br \/>\n\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4f20\u611f\u5668\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u901a\u8fc7\u84dd\u7259\u5411\u624b\u673aApp\u5b9e\u65f6\u4e0a\u62a5\u6e29\u6e7f\u5ea6\u6570\u636e\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u5e16\u5b50&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565&#xff0c;\u8981\u4e48\u76f4\u63a5\u6254\u51fa\u4e00\u6bb5\u770b\u4e0d\u61c2\u7684\u4f8b\u7a0b\u4ee3\u7801\u3002\u771f\u6b63\u8981\u4ece\u96f6\u5f00\u59cb&#xff0c;\u7406\u89e3GATT\u670d\u52a1\u5668\u7684\u6bcf\u4e2a\u73af\u8282\u5e76\u8dd1\u901a\u6570\u636e\u6d41&#xff0c;\u8fd8\u662f\u5f97\u81ea\u5df1\u52a8\u624b\u8d9f\u4e00\u904d\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c31\u662f\u6211\u8fd9\u8d9f\u201c\u8d9f\u5751\u4e4b\u65c5\u201d\u7684\u5b8c\u6574<\/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":[8580,6661,8222,179],"topic":[],"class_list":["post-78037","post","type-post","status-publish","format-standard","hentry","category-server","tag-esp-idf","tag-gatt","tag-8222","tag-esp32"],"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\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\/78037.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\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218&#xff1a;\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668&#xff08;\u57fa\u4e8eESP-IDF 4.4&#xff09; \u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4f20\u611f\u5668\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u901a\u8fc7\u84dd\u7259\u5411\u624b\u673aApp\u5b9e\u65f6\u4e0a\u62a5\u6e29\u6e7f\u5ea6\u6570\u636e\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u5e16\u5b50&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565&#xff0c;\u8981\u4e48\u76f4\u63a5\u6254\u51fa\u4e00\u6bb5\u770b\u4e0d\u61c2\u7684\u4f8b\u7a0b\u4ee3\u7801\u3002\u771f\u6b63\u8981\u4ece\u96f6\u5f00\u59cb&#xff0c;\u7406\u89e3GATT\u670d\u52a1\u5668\u7684\u6bcf\u4e2a\u73af\u8282\u5e76\u8dd1\u901a\u6570\u636e\u6d41&#xff0c;\u8fd8\u662f\u5f97\u81ea\u5df1\u52a8\u624b\u8d9f\u4e00\u904d\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c31\u662f\u6211\u8fd9\u8d9f\u201c\u8d9f\u5751\u4e4b\u65c5\u201d\u7684\u5b8c\u6574\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78037.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-25T15:42:02+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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78037.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78037.html\",\"name\":\"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-25T15:42:02+00:00\",\"dateModified\":\"2026-02-25T15:42:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78037.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78037.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78037.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\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\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\/78037.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\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218&#xff1a;\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668&#xff08;\u57fa\u4e8eESP-IDF 4.4&#xff09; \u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u667a\u80fd\u5bb6\u5c45\u4f20\u611f\u5668\u9879\u76ee&#xff0c;\u9700\u8981\u8ba9ESP32\u901a\u8fc7\u84dd\u7259\u5411\u624b\u673aApp\u5b9e\u65f6\u4e0a\u62a5\u6e29\u6e7f\u5ea6\u6570\u636e\u3002\u7ffb\u904d\u4e86\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u5e16\u5b50&#xff0c;\u53d1\u73b0\u5f88\u591a\u6559\u7a0b\u8981\u4e48\u8fc7\u4e8e\u7b80\u7565&#xff0c;\u8981\u4e48\u76f4\u63a5\u6254\u51fa\u4e00\u6bb5\u770b\u4e0d\u61c2\u7684\u4f8b\u7a0b\u4ee3\u7801\u3002\u771f\u6b63\u8981\u4ece\u96f6\u5f00\u59cb&#xff0c;\u7406\u89e3GATT\u670d\u52a1\u5668\u7684\u6bcf\u4e2a\u73af\u8282\u5e76\u8dd1\u901a\u6570\u636e\u6d41&#xff0c;\u8fd8\u662f\u5f97\u81ea\u5df1\u52a8\u624b\u8d9f\u4e00\u904d\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c31\u662f\u6211\u8fd9\u8d9f\u201c\u8d9f\u5751\u4e4b\u65c5\u201d\u7684\u5b8c\u6574","og_url":"https:\/\/www.wsisp.com\/helps\/78037.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-25T15:42:02+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78037.html","url":"https:\/\/www.wsisp.com\/helps\/78037.html","name":"ESP32\u84dd\u7259\u5f00\u53d1\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efaGATT\u670d\u52a1\u5668\uff08\u57fa\u4e8eESP-IDF 4.4\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-25T15:42:02+00:00","dateModified":"2026-02-25T15:42:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78037.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78037.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78037.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\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\/78037","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=78037"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78037\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78037"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}