{"id":25067,"date":"2026-07-26T19:24:20","date_gmt":"2026-07-26T11:24:20","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=25067"},"modified":"2026-07-26T19:39:05","modified_gmt":"2026-07-26T11:39:05","slug":"%e5%9f%ba%e4%ba%8e-esp32-wroom-32-%e5%ae%9e%e8%b7%b5-ble-%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%bb%e5%8a%a8%e9%80%9a%e7%9f%a5-platformio","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=25067","title":{"rendered":"\u57fa\u4e8e ESP32-WROOM-32 \u5b9e\u8df5 BLE \u670d\u52a1\u5668\u4e3b\u52a8\u901a\u77e5 Notify &#8211; PlatformIO"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=25067\/#%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\" >\u6d4b\u8bd5\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=25067\/#%E5%AE%9E%E8%B7%B5\" >\u5b9e\u8df5<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83\"><\/span>\u6d4b\u8bd5\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>ESP32-WROOM-32<\/li>\n<li>PlatformIO IDE 3.3.4<\/li>\n<li>PlatformIO Core 6.1.19<\/li>\n<li>pioarduino platform-espressif32 55.03.311\uff08Arduino Release v3.3.11 based on ESP-IDF v5.5.5\uff09<\/li>\n<\/ul>\n<p>platformio.ini<\/p>\n<pre><code class=\"language-ini\">[env:esp32dev]\nplatform = https:\/\/github.com\/pioarduino\/platform-espressif32\/releases\/download\/55.03.311\/platform-espressif32.zip\nboard = esp32dev\nframework = arduino\nmonitor_speed = 115200<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E8%B7%B5\"><\/span>\u5b9e\u8df5<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u6e90\u7801\u53c2\u8003\u6765\u6e90\uff1a<a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/3.3.11\/libraries\/BLE\/examples\/Notify\/Notify.ino\">https:\/\/github.com\/espressif\/arduino-esp32\/blob\/3.3.11\/libraries\/BLE\/examples\/Notify\/Notify.ino<\/a><\/p>\n<pre><code class=\"language-cpp\">\/*\n    \u529f\u80fd\u6982\u8ff0:\n    \u521b\u5efa\u4e00\u4e2a BLE \u670d\u52a1\u5668\uff0c\u5f53\u6709\u624b\u673a\/\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u5b9a\u671f\uff08\u6bcf 0.5 \u79d2\uff09\u4e3b\u52a8\u5411\u624b\u673a\u63a8\u9001\u9012\u589e\u6570\u636e\u5305 (Notify)\u3002\n    \u670d\u52a1\u7684\u5e7f\u64ad UUID \u4e3a: 4fafc201-1fb5-459e-8fcc-c5c9c331914b\n    \u7279\u5f81\u503c UUID \u4e3a: beb5483e-36e1-4688-b7f5-ea07361b26a8\n\n    \u67b6\u6784\u6b65\u9aa4:\n    1. \u521b\u5efa BLE Server\uff08\u670d\u52a1\u5668\uff09\n    2. \u521b\u5efa BLE Service\uff08\u670d\u52a1\uff09\n    3. \u5728\u670d\u52a1\u4e0b\u521b\u5efa BLE Characteristic\uff08\u7279\u5f81\u503c\uff09\n    4. \u4e3a\u7279\u5f81\u503c\u6dfb\u52a0 BLE Descriptor\uff08\u63cf\u8ff0\u7b26\uff09\n    5. \u542f\u52a8\u670d\u52a1\n    6. \u5f00\u542f\u5e7f\u64ad\n\n    \u5f53\u4e0e\u670d\u52a1\u5668\u5173\u8054\u7684\u8fde\u63a5\u56de\u8c03\u51fd\u6570\u76d1\u6d4b\u5230\u8fde\u63a5\u65f6\uff0c\u5c06\u914d\u5408\u4e3b\u5faa\u73af\u5b9a\u671f\u89e6\u53d1\u6570\u636e\u901a\u77e5\u3002\n*\/\n\n#include &lt;Arduino.h&gt;\n#include &lt;BLEDevice.h&gt;\n#include &lt;BLEServer.h&gt;\n#include &lt;BLEUtils.h&gt;\n#include &lt;BLE2902.h&gt; \/\/ \u63d0\u4f9b 0x2902 \u63cf\u8ff0\u7b26\uff08\u5ba2\u6237\u7aef\u7279\u5f81\u914d\u7f6e\u63cf\u8ff0\u7b26 CCCD\uff0c\u7528\u4e8e\u4f7f\u80fd Notify\/Indicate\uff09\n#include &lt;BLE2901.h&gt; \/\/ \u63d0\u4f9b 0x2901 \u63cf\u8ff0\u7b26\uff08\u7279\u5f81\u503c\u7528\u6237\u63cf\u8ff0\u7b26\uff0c\u7528\u4e8e\u663e\u793a\u6587\u672c\u540d\u79f0\uff09\n\n\/\/ \u5168\u5c40\u6307\u9488\u4e0e\u53d8\u91cf\u5b9a\u4e49\nBLEServer *pServer = NULL;\nBLECharacteristic *pCharacteristic = NULL;\nBLE2901 *descriptor_2901 = NULL;\n\nbool deviceConnected = false;    \/\/ \u5f53\u524d\u624b\u673a\u662f\u5426\u5904\u4e8e\u8fde\u63a5\u72b6\u6001\nbool oldDeviceConnected = false; \/\/ \u4e0a\u4e00\u6b21\u5faa\u73af\u65f6\u7684\u8fde\u63a5\u72b6\u6001\uff08\u7528\u4e8e\u8fb9\u7f18\u89e6\u53d1\u5224\u5b9a\uff09\nuint32_t value = 0;              \/\/ \u5b9a\u65f6\u63a8\u9001\u7684 32 \u4f4d\u9012\u589e\u6570\u503c\n\n\/\/ \u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee https:\/\/www.uuidgenerator.net\/ \u751f\u6210\u4f60\u81ea\u5df1\u7684 UUID\n#define SERVICE_UUID &quot;4fafc201-1fb5-459e-8fcc-c5c9c331914b&quot;\n#define CHARACTERISTIC_UUID &quot;beb5483e-36e1-4688-b7f5-ea07361b26a8&quot;\n\n\/**\n * @brief BLE \u670d\u52a1\u5668\u8fde\u63a5\u72b6\u6001\u76d1\u542c\u56de\u8c03\u7c7b\n *\/\nclass MyServerCallbacks : public BLEServerCallbacks\n{\n    \/\/ \u5f53\u5ba2\u6237\u7aef\u6210\u529f\u8fde\u63a5\u5230 ESP32 \u65f6\u89e6\u53d1\n    void onConnect(BLEServer *pServer) override\n    {\n        deviceConnected = true;\n    };\n\n    \/\/ \u5f53\u5ba2\u6237\u7aef\u4e0e ESP32 \u65ad\u5f00\u8fde\u63a5\u65f6\u89e6\u53d1\n    void onDisconnect(BLEServer *pServer) override\n    {\n        deviceConnected = false;\n    }\n};\n\nvoid setup()\n{\n    Serial.begin(115200);\n\n    \/\/ 1. \u521d\u59cb\u5316 BLE \u8bbe\u5907\u5e76\u8bbe\u7f6e\u5e7f\u64ad\u540d\u79f0\n    BLEDevice::init(&quot;ESP32&quot;);\n\n    \/\/ 2. \u521b\u5efa BLE \u670d\u52a1\u5668\uff0c\u5e76\u7ed1\u5b9a\u8fde\u63a5\/\u65ad\u5f00\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\n    pServer = BLEDevice::createServer();\n    pServer-&gt;setCallbacks(new MyServerCallbacks());\n\n    \/\/ 3. \u5728\u670d\u52a1\u5668\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49 GATT \u670d\u52a1\n    BLEService *pService = pServer-&gt;createService(SERVICE_UUID);\n\n    \/\/ 4. \u5728\u670d\u52a1\u4e2d\u521b\u5efa\u7279\u5f81\u503c\uff0c\u5e76\u5f00\u542f\u591a\u79cd\u6743\u9650\uff1a\n    \/\/ \u53ef\u8bfb (READ) | \u53ef\u5199 (WRITE) | \u53ef\u901a\u77e5 (NOTIFY) | \u53ef\u6307\u793a (INDICATE)\n    pCharacteristic = pService-&gt;createCharacteristic(\n        CHARACTERISTIC_UUID,\n        BLECharacteristic::PROPERTY_READ |\n        BLECharacteristic::PROPERTY_WRITE |\n        BLECharacteristic::PROPERTY_NOTIFY |\n        BLECharacteristic::PROPERTY_INDICATE);\n\n    \/\/ 5. \u6dfb\u52a0 0x2902 \u63cf\u8ff0\u7b26 (Client Characteristic Configuration Descriptor - CCCD)\n    \/\/ \u8fd9\u662f BLE \u6807\u51c6\u534f\u8bae\u8981\u6c42\u7684\uff1a\u624b\u673a\u7aef\u5fc5\u987b\u901a\u8fc7\u5199\u5165 0x2902 \u63cf\u8ff0\u7b26\u6765\u5f00\u542f\/\u5173\u95ed ESP32 \u7684 Notify \u63a8\u9001\u6743\u9650\n    \/\/ \u6ce8\uff1a\u5982\u679c\u672a\u6765\u6539\u7528 NimBLE \u5e93\uff0cNimBLE \u4f1a\u6839\u636e\u7279\u5f81\u503c\u5c5e\u6027\u81ea\u52a8\u6dfb\u52a0\u6b64\u63cf\u8ff0\u7b26\uff0c\u65e0\u9700\u624b\u52a8 new\n    pCharacteristic-&gt;addDescriptor(new BLE2902());\n\n    \/\/ 6. \u6dfb\u52a0 0x2901 \u63cf\u8ff0\u7b26 (Characteristic User Description)\n    \/\/ \u7528\u4e8e\u7ed9\u8fd9\u4e2a\u7279\u5f81\u503c\u9644\u5e26\u4e00\u6bb5\u4eba\u6027\u5316\u7684\u6587\u672c\u8bf4\u660e\uff0c\u624b\u673a\u8c03\u8bd5 App \u6253\u5f00\u540e\u53ef\u4ee5\u76f4\u63a5\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\n    descriptor_2901 = new BLE2901();\n    descriptor_2901-&gt;setDescription(&quot;My own description for this characteristic.&quot;);\n    descriptor_2901-&gt;setAccessPermissions(ESP_GATT_PERM_READ); \/\/ \u8bbe\u7f6e\u8be5\u63cf\u8ff0\u7b26\u4e3a\u53ea\u8bfb\uff08\u9ed8\u8ba4\u662f\u53ef\u8bfb\u5199\uff09\n    pCharacteristic-&gt;addDescriptor(descriptor_2901);\n\n    \/\/ 7. \u542f\u52a8 GATT \u670d\u52a1\n    pService-&gt;start();\n\n    \/\/ 8. \u914d\u7f6e\u5e76\u5f00\u542f\u5e7f\u64ad\n    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();\n    pAdvertising-&gt;addServiceUUID(SERVICE_UUID);\n    pAdvertising-&gt;setScanResponse(false);\n    pAdvertising-&gt;setMinPreferred(0x0); \/\/ \u8bbe\u7f6e\u4e3a 0x00 \u8868\u793a\u4e0d\u5728\u5e7f\u64ad\u5305\u4e2d\u6307\u5b9a\u63a8\u8350\u8fde\u63a5\u95f4\u9694\u53c2\u6570\n\n    BLEDevice::startAdvertising();\n    Serial.println(&quot;Waiting a client connection to notify...&quot;);\n}\n\nvoid loop()\n{\n    \/\/ -------------------------------------------------------------\n    \/\/ \u72b6\u6001 1\uff1a\u624b\u673a\u5f53\u524d\u5904\u4e8e\u5df2\u8fde\u63a5\u72b6\u6001 -&gt; \u6301\u7eed\u63a8\u9001\u9012\u589e\u6570\u636e\n    \/\/ -------------------------------------------------------------\n    if (deviceConnected)\n    {\n        \/\/ \u5c06 uint32_t \u7c7b\u578b\u7684\u53d8\u91cf value \u8f6c\u6362\u4e3a\u65e0\u7b26\u53f7 8 \u4f4d\u6307\u9488\u683c\u5f0f\uff0c\u63a8\u9001 4 \u4e2a\u5b57\u8282\u957f\u5ea6\u7684\u6570\u636e\n        pCharacteristic-&gt;setValue((uint8_t *)&amp;value, 4);\n\n        \/\/ \u5411\u5df2\u8ba2\u9605 notification \u7684\u5ba2\u6237\u7aef\u4e3b\u52a8\u53d1\u9001\u6570\u636e\u53d8\u66f4\u901a\u77e5\n        pCharacteristic-&gt;notify();\n\n        value++;    \/\/ \u6570\u636e\u9012\u589e\n        delay(500); \/\/ \u6bcf 500 \u6beb\u79d2 (0.5\u79d2) \u63a8\u9001\u4e00\u6b21\n    }\n\n    \/\/ -------------------------------------------------------------\n    \/\/ \u72b6\u6001 2\uff1a\u68c0\u6d4b\u5230\u201c\u521a\u65ad\u5f00\u8fde\u63a5\u201d\u7684\u77ac\u95f4\uff08\u4e0b\u964d\u6cbf\u89e6\u53d1\uff09\n    \/\/ -------------------------------------------------------------\n    if (!deviceConnected &amp;&amp; oldDeviceConnected)\n    {\n        delay(500);                  \/\/ \u7ed9 ESP32 \u84dd\u7259\u534f\u8bae\u6808\u7559\u51fa\u5904\u7406\u5e95\u5c42\u8d44\u6e90\u91ca\u653e\u7684\u7f13\u51b2\u65f6\u95f4\n        pServer-&gt;startAdvertising(); \/\/ \u91cd\u65b0\u5f00\u542f\u5e7f\u64ad\uff0c\u65b9\u4fbf\u5176\u4ed6\u6216\u539f\u8bbe\u5907\u518d\u6b21\u8fde\u63a5\n        Serial.println(&quot;start advertising&quot;);\n\n        \/\/ \u540c\u6b65\u72b6\u6001\uff0c\u9632\u6b62\u6b64 if \u5206\u652f\u5728\u4e0b\u4e00\u6b21 loop \u4e2d\u88ab\u91cd\u590d\u6267\u884c\n        oldDeviceConnected = deviceConnected;\n    }\n\n    \/\/ -------------------------------------------------------------\n    \/\/ \u72b6\u6001 3\uff1a\u68c0\u6d4b\u5230\u201c\u521a\u5efa\u7acb\u8fde\u63a5\u201d\u7684\u77ac\u95f4\uff08\u4e0a\u5347\u6cbf\u89e6\u53d1\uff09\n    \/\/ -------------------------------------------------------------\n    if (deviceConnected &amp;&amp; !oldDeviceConnected)\n    {\n        \/\/ \u8fde\u63a5\u5efa\u7acb\u7684\u77ac\u95f4\u53ef\u4ee5\u5728\u8fd9\u91cc\u7f16\u5199\u4e00\u6b21\u6027\u7684\u521d\u59cb\u5316\u903b\u8f91\uff08\u5982\u590d\u4f4d\u8ba1\u6570\u5668\u3001\u53d1\u9001\u6b22\u8fce\u5305\u7b49\uff09\n\n        \/\/ \u540c\u6b65\u72b6\u6001\uff0c\u9632\u6b62\u6b64 if \u5206\u652f\u5728\u4e0b\u4e00\u6b21 loop \u4e2d\u88ab\u91cd\u590d\u6267\u884c\n        oldDeviceConnected = deviceConnected;\n    }\n}<\/code><\/pre>\n<p>\u867d\u7136\u670d\u52a1\u5668\u5f00\u542f\u4e86\u4e3b\u52a8\u901a\u77e5\uff0c\u4f46\u662f\u5ba2\u6237\u7aef\u5982\u679c\u4e0d\u8ba2\u9605\u5c31\u4e0d\u4f1a\u5f3a\u63a8\uff0c\u8981\u70b9\u8fdb\u53bb\u5f00\u542f Notify<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/07\/image-1785064527352.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 945px; --smush-placeholder-aspect-ratio: 945\/266;\" \/><\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u770b\u5230\u503c\u5b9e\u65f6\u66f4\u65b0\u4e86<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/07\/20260726_192220.gif\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 936px; --smush-placeholder-aspect-ratio: 936\/226;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u73af\u5883 ESP32-WROOM-32 PlatformIO IDE 3.3.4 PlatformIO Cor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":null,"zakra_menu_item_active_color":null,"zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,603,589,604,647,599,2199],"tags":[1503,1467,2200,1468],"class_list":["post-25067","post","type-post","status-publish","format-standard","hentry","category-all","category-arduino","category-c-c","category-esp32","category-platformio","category-599","category-2199","tag-ble","tag-esp32","tag-notify","tag-1468"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25067"}],"version-history":[{"count":4,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25067\/revisions"}],"predecessor-version":[{"id":25078,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25067\/revisions\/25078"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}