{"id":25062,"date":"2026-07-26T17:30:33","date_gmt":"2026-07-26T09:30:33","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=25062"},"modified":"2026-07-26T19:38:39","modified_gmt":"2026-07-26T11:38:39","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%e6%98%be%e7%a4%ba%e8%a2%ab%e4%bf%ae%e6%94%b9%e7%9a%84%e5%86%85%e5%ae%b9-platformio","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=25062","title":{"rendered":"\u57fa\u4e8e ESP32-WROOM-32 \u5b9e\u8df5 BLE \u670d\u52a1\u5668\u76d1\u542c\u5185\u5bb9\u5199\u5165 Write &#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=25062\/#%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=25062\/#%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\/Write\/Write.ino\">https:\/\/github.com\/espressif\/arduino-esp32\/blob\/3.3.11\/libraries\/BLE\/examples\/Write\/Write.ino<\/a><\/p>\n<pre><code class=\"language-cpp\">#include &lt;Arduino.h&gt;\n#include &lt;BLEDevice.h&gt; \/\/ ESP32 BLE \u6838\u5fc3\u57fa\u7840\u5e93\n#include &lt;BLEUtils.h&gt;  \/\/ BLE \u5b9e\u7528\u5de5\u5177\u51fd\u6570\u5e93\n#include &lt;BLEServer.h&gt; \/\/ BLE \u670d\u52a1\u5668\u76f8\u5173\u7c7b\n\n\/\/ \u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee https:\/\/www.uuidgenerator.net\/ \u5728\u7ebf\u751f\u6210\u4f60\u81ea\u5df1\u7684\u552f\u4e00 UUID\n\/\/ \u5b9a\u4e49\u81ea\u5b9a\u4e49 GATT \u670d\u52a1\u7684 UUID\n#define SERVICE_UUID &quot;4fafc201-1fb5-459e-8fcc-c5c9c331914b&quot;\n\/\/ \u5b9a\u4e49\u81ea\u5b9a\u4e49\u7279\u5f81\u503c (Characteristic) \u7684 UUID\n#define CHARACTERISTIC_UUID &quot;beb5483e-36e1-4688-b7f5-ea07361b26a8&quot;\n\n\/**\n * @brief \u81ea\u5b9a\u4e49\u7279\u5f81\u503c\u56de\u8c03\u7c7b\n * \u7ee7\u627f\u81ea BLECharacteristicCallbacks\uff0c\u7528\u4e8e\u91cd\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\n *\/\nclass MyCallbacks : public BLECharacteristicCallbacks\n{\n    \/**\n     * @brief \u5f53\u5ba2\u6237\u7aef\uff08\u5982\u624b\u673a\uff09\u5411\u6b64\u7279\u5f81\u503c\u5199\u5165\u6570\u636e\u65f6\uff0c\u5e95\u5c42\u7684 BLE \u534f\u8bae\u6808\u4f1a\u81ea\u52a8\u56de\u8c03\u6b64\u51fd\u6570\n     * @param pCharacteristic \u89e6\u53d1\u8be5\u4e8b\u4ef6\u7684\u7279\u5f81\u503c\u5bf9\u8c61\u7684\u6307\u9488\n     *\/\n    void onWrite(BLECharacteristic *pCharacteristic) override\n    {\n        \/\/ \u4ece\u7279\u5f81\u503c\u4e2d\u83b7\u53d6\u5ba2\u6237\u7aef\u5199\u5165\u7684\u6570\u636e\uff08\u4ee5 String \u7c7b\u578b\u63a5\u6536\uff09\n        String value = pCharacteristic-&gt;getValue();\n\n        \/\/ \u5224\u65ad\u63a5\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6\u662f\u5426\u5927\u4e8e 0\n        if (value.length() &gt; 0)\n        {\n            Serial.println(&quot;*********&quot;);\n            Serial.print(&quot;New value: &quot;);\n\n            \/\/ \u9010\u4e2a\u5b57\u8282\u8bfb\u53d6\u5e76\u6253\u5370\u624b\u673a\u53d1\u9001\u8fc7\u6765\u7684\u5b57\u7b26\n            for (int i = 0; i &lt; value.length(); i++)\n            {\n                Serial.print(value[i]);\n            }\n\n            Serial.println();\n            Serial.println(&quot;*********&quot;);\n        }\n    }\n};\n\nvoid setup()\n{\n    Serial.begin(115200);\n\n    \/\/ \u4e32\u53e3\u6253\u5370\u6d4b\u8bd5\u6307\u5f15\uff0c\u63d0\u793a\u7528\u6237\u5982\u4f55\u5728\u624b\u673a\u7aef\u64cd\u4f5c\n    Serial.println(&quot;1- Download and install an BLE scanner app in your phone&quot;);\n    Serial.println(&quot;2- Scan for BLE devices in the app&quot;);\n    Serial.println(&quot;3- Connect to MyESP32&quot;);\n    Serial.println(&quot;4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something&quot;);\n    Serial.println(&quot;5- See the magic =)&quot;);\n\n    \/\/ 1. \u521d\u59cb\u5316 BLE \u8bbe\u5907\uff0c\u5e76\u6307\u5b9a\u5e7f\u64ad\u540d\u79f0\u4e3a &quot;MyESP32&quot;\n    BLEDevice::init(&quot;MyESP32&quot;);\n\n    \/\/ 2. \u521b\u5efa BLE \u670d\u52a1\u5668\u5bf9\u8c61\n    BLEServer *pServer = BLEDevice::createServer();\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\u8bbe\u7f6e\u6743\u9650\u4e3a\uff1a\u53ef\u8bfb (PROPERTY_READ) \u548c \u53ef\u5199 (PROPERTY_WRITE)\n    BLECharacteristic *pCharacteristic =\n        pService-&gt;createCharacteristic(CHARACTERISTIC_UUID,\n                                       BLECharacteristic::PROPERTY_READ |\n                                           BLECharacteristic::PROPERTY_WRITE);\n\n    \/\/ 5. \u5c06\u524d\u9762\u5b9e\u4f8b\u5316\u7684\u56de\u8c03\u7c7b\u7ed1\u5b9a\u5230\u8be5\u7279\u5f81\u503c\u4e0a\n    \/\/ \u5f53\u624b\u673a\u6267\u884c\u5199\u5165\u52a8\u4f5c\u65f6\uff0c\u5c31\u4f1a\u81ea\u52a8\u89e6\u53d1 MyCallbacks::onWrite\n    pCharacteristic-&gt;setCallbacks(new MyCallbacks());\n\n    \/\/ 6. \u4e3a\u7279\u5f81\u503c\u8bbe\u7f6e\u521d\u59cb\u9ed8\u8ba4\u5185\u5bb9\uff08\u624b\u673a\u8bfb\u53d6\u65f6\u80fd\u770b\u5230\u7684\u5b57\u7b26\u4e32\uff09\n    pCharacteristic-&gt;setValue(&quot;Hello World&quot;);\n\n    \/\/ 7. \u542f\u52a8\u8be5 GATT \u670d\u52a1\n    pService-&gt;start();\n\n    \/\/ 8. \u83b7\u53d6\u5e7f\u64ad\u63a7\u5236\u5bf9\u8c61\u5e76\u76f4\u63a5\u5f00\u542f\u5e7f\u64ad\n    BLEAdvertising *pAdvertising = pServer-&gt;getAdvertising();\n    pAdvertising-&gt;start();\n}\n\nvoid loop()\n{\n    \/\/ BLE \u7684\u4e8b\u4ef6\u54cd\u5e94\uff08\u5305\u62ec\u8fde\u63a5\u3001\u6570\u636e\u63a5\u6536\uff09\u5747\u7531\u540e\u53f0\u84dd\u7259\u4efb\u52a1\u81ea\u52a8\u5904\u7406\n    \/\/ \u56e0\u6b64 loop() \u5185\u4e0d\u9700\u8981\u8f6e\u8be2 BLE \u903b\u8f91\uff0c\u53ea\u9700\u4fdd\u6301\u4e3b\u7ebf\u7a0b\u4f11\u7720\u6216\u6267\u884c\u5176\u4ed6\u65e5\u5e38\u4efb\u52a1\n    delay(2000);\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6848\u4f8b\u53ef\u4ee5\u628a\u88ab\u4fee\u6539\u7684\u5185\u5bb9\u663e\u793a\u51fa\u6765\uff0c\u53ef\u4ee5\u5b66\u4e60\u5982\u4f55\u76d1\u542c\u5c5e\u6027\u503c\u88ab\u4fee\u6539\u4e86<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/07\/image-1785058089185.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1020px; --smush-placeholder-aspect-ratio: 1020\/337;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/07\/image-1785058104158.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 528px; --smush-placeholder-aspect-ratio: 528\/398;\" \/><\/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],"tags":[1503,1467,1468],"class_list":["post-25062","post","type-post","status-publish","format-standard","hentry","category-all","category-arduino","category-c-c","category-esp32","category-platformio","category-599","tag-ble","tag-esp32","tag-1468"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25062","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=25062"}],"version-history":[{"count":4,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25062\/revisions"}],"predecessor-version":[{"id":25077,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25062\/revisions\/25077"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}