{"id":25139,"date":"2026-08-09T20:29:48","date_gmt":"2026-08-09T12:29:48","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=25139"},"modified":"2026-08-09T23:07:39","modified_gmt":"2026-08-09T15:07:39","slug":"%e5%9f%ba%e4%ba%8e-esp32-wroom-32-%e5%ae%9e%e8%b7%b5-beacon-%e6%89%ab%e6%8f%8f-platformio","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=25139","title":{"rendered":"\u57fa\u4e8e ESP32-WROOM-32 \u5b9e\u8df5 Beacon \u626b\u63cf \u2013 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=25139\/#%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=25139\/#%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\/Beacon_Scanner\/Beacon_Scanner.ino\">https:\/\/github.com\/espressif\/arduino-esp32\/blob\/3.3.11\/libraries\/BLE\/examples\/Beacon_Scanner\/Beacon_Scanner.ino<\/a><\/p>\n<pre><code class=\"language-cpp\">#include &lt;Arduino.h&gt;\n\n\/\/ \u5f15\u5165 ESP32 BLE \u76f8\u5173\u7684\u6838\u5fc3\u5934\u6587\u4ef6\n#include &lt;BLEDevice.h&gt;\n#include &lt;BLEUtils.h&gt;\n#include &lt;BLEScan.h&gt;\n#include &lt;BLEAdvertisedDevice.h&gt;\n#include &lt;BLEEddystoneURL.h&gt;\n#include &lt;BLEEddystoneTLM.h&gt;\n#include &lt;BLEBeacon.h&gt;\n\nint scanTime = 5;  \/\/ \u6bcf\u6b21\u626b\u63cf\u6301\u7eed\u7684\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\nBLEScan *pBLEScan; \/\/ \u58f0\u660e BLE \u626b\u63cf\u5bf9\u8c61\u7684\u6307\u9488\n\n\/\/ \u81ea\u5b9a\u4e49\u56de\u8c03\u7c7b\uff1a\u5904\u7406\u626b\u63cf\u8fc7\u7a0b\u4e2d\u63a5\u6536\u5230\u7684\u6240\u6709 BLE \u5e7f\u64ad\u8bbe\u5907\nclass MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks\n{\n    void onResult(BLEAdvertisedDevice advertisedDevice)\n    {\n        Serial.println(&quot;==================================================&quot;);\n\n        \/\/ 1. \u8bbe\u5907\u540d\u79f0 &amp; MAC \u5730\u5740 &amp; \u4fe1\u53f7\u5f3a\u5ea6 (RSSI)\n        String devName = advertisedDevice.haveName() ? advertisedDevice.getName().c_str() : &quot;N\/A&quot;;\n        Serial.printf(&quot;%s\\n&quot;, devName.c_str());\n        Serial.printf(&quot;%s\\n&quot;, advertisedDevice.getAddress().toString().c_str());\n        Serial.printf(&quot;RSSI: %d dBm\\n&quot;, advertisedDevice.getRSSI());\n\n        \/\/ 2. \u5e7f\u64ad\u6807\u5fd7\u4f4d (Flags) \u68c0\u67e5\n        if (advertisedDevice.haveAppearance()) {\n            Serial.printf(&quot;Appearance: 0x%04X\\n&quot;, advertisedDevice.getAppearance());\n        }\n\n        \/\/ 3. \u670d\u52a1 UUID (Service UUID)\n        if (advertisedDevice.haveServiceUUID()) {\n            Serial.printf(&quot;Service UUID: %s\\n&quot;, advertisedDevice.getServiceUUID().toString().c_str());\n        }\n\n        \/\/ 4. \u89e3\u6790 \u5382\u5546\u81ea\u5b9a\u4e49\u6570\u636e (Manufacturer Data)\n        if (advertisedDevice.haveManufacturerData())\n        {\n            String mData = advertisedDevice.getManufacturerData();\n            size_t dataLen = mData.length();\n\n            if (dataLen &gt;= 2)\n            {\n                \/\/ \u63d0\u53d6\u524d\u4e24\u4e2a\u5b57\u8282\u4f5c\u4e3a Company ID (BLE \u4e2d\u4f7f\u7528\u5c0f\u7aef\u5e8f)\n                uint16_t companyId = (uint8_t)mData[1] &lt;&lt; 8 | (uint8_t)mData[0];\n\n                Serial.printf(&quot;Manufacturer data:\\n&quot;);\n                Serial.printf(&quot;  Company ID: 0x%04X &quot;, companyId);\n\n                \/\/ \u5224\u65ad\u5e38\u89c1\u5382\u5546 (\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u884c\u8865\u5145)\n                if (companyId == 0x06A8) {\n                    Serial.print(&quot;(GD Midea Air-Conditioning Equipment Co., Ltd.)\\n&quot;);\n                } else if (companyId == 0x004C) {\n                    Serial.print(&quot;(Apple Inc.)\\n&quot;);\n                } else if (companyId == 0x00E0) {\n                    Serial.print(&quot;(Google)\\n&quot;);\n                } else {\n                    Serial.print(&quot;(Unknown)\\n&quot;);\n                }\n\n                \/\/ \u4ee5 16 \u8fdb\u5236\u5b57\u7b26\u4e32\u683c\u5f0f\u8f93\u51fa payload \u5185\u5bb9\n                Serial.print(&quot;  Payload: 0x&quot;);\n                for (size_t i = 0; i &lt; dataLen; i++) {\n                    Serial.printf(&quot;%02X&quot;, (uint8_t)mData[i]);\n                }\n                Serial.println();\n            }\n        }\n\n        \/\/ 5. \u5224\u65ad Eddystone URL \/ TLM \u534f\u8bae\n        if (advertisedDevice.getFrameType() == BLE_EDDYSTONE_URL_FRAME) {\n            BLEEddystoneURL eddystoneURL(&amp;advertisedDevice);\n            Serial.printf(&quot;Eddystone URL: %s\\n&quot;, eddystoneURL.getDecodedURL().c_str());\n        }\n\n        if (advertisedDevice.getFrameType() == BLE_EDDYSTONE_TLM_FRAME) {\n            BLEEddystoneTLM eddystoneTLM(&amp;advertisedDevice);\n            Serial.printf(&quot;Eddystone TLM Temp: %.2f\u00b0C, Batt: %umV\\n&quot;, eddystoneTLM.getTemp(), eddystoneTLM.getVolt());\n        }\n\n        Serial.println(&quot;==================================================\\n&quot;);\n    }\n};\n\nvoid setup()\n{\n    \/\/ \u521d\u59cb\u5316\u4e32\u53e3\u901a\u4fe1\uff0c\u6ce2\u7279\u7387\u8bbe\u7f6e\u4e3a 115200\n    Serial.begin(115200);\n    Serial.println(&quot;Scanning...&quot;);\n\n    \/\/ \u521d\u59cb\u5316 ESP32 BLE \u8bbe\u5907\uff08\u4f20\u5165\u7684\u8bbe\u5907\u540d\u79f0\u53c2\u6570\u53ef\u7559\u7a7a\uff09\n    BLEDevice::init(&quot;&quot;);\n\n    \/\/ \u83b7\u53d6 BLE \u626b\u63cf\u5668\u5355\u4f8b\u6307\u9488\n    pBLEScan = BLEDevice::getScan();\n\n    \/\/ \u8bbe\u7f6e\u8bbe\u5907\u626b\u63cf\u56de\u8c03\u51fd\u6570\uff08\u5f53\u53d1\u73b0\u65b0\u8bbe\u5907\u65f6\u8c03\u7528MyAdvertisedDeviceCallbacks\uff09\n    pBLEScan-&gt;setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());\n\n    \/\/ \u5f00\u542f\u4e3b\u52a8\u626b\u63cf (Active Scan):\n    \/\/ \u4e3b\u52a8\u6a21\u5f0f\u4e0b\u626b\u63cf\u5668\u4f1a\u5728\u63a5\u6536\u5230\u5e7f\u64ad\u5305\u540e\u53d1\u9001 Scan Request \u7d22\u8981\u66f4\u591a\u6570\u636e\uff1b\n    \/\/ \u867d\u7136\u6bd4\u88ab\u52a8\u6a21\u5f0f (Passive Scan) \u66f4\u8017\u7535\uff0c\u4f46\u80fd\u66f4\u5feb\u901f\u4e14\u5b8c\u6574\u5730\u83b7\u53d6\u5916\u8bbe\u4fe1\u606f\u3002\n    pBLEScan-&gt;setActiveScan(true);\n\n    \/\/ \u8bbe\u7f6e\u626b\u63cf\u95f4\u9694 (Interval) \u4e0e \u626b\u63cf\u7a97\u53e3\u5927\u5c0f (Window)\uff0c\u5355\u4f4d\u5747\u4e3a \u6beb\u79d2 (ms)\n    pBLEScan-&gt;setInterval(100);\n    pBLEScan-&gt;setWindow(99); \/\/ \u7a97\u53e3\u503c\u5fc5\u987b\u5c0f\u4e8e\u6216\u7b49\u4e8e\u95f4\u9694\u503c\n}\n\nvoid loop()\n{\n    \/\/ \u542f\u52a8\u4e00\u6b21 BLE \u626b\u63cf:\n    \/\/ \u53c2\u6570 1: \u626b\u63cf\u6301\u7eed\u65f6\u95f4 (\u79d2)\n    \/\/ \u53c2\u6570 2: is_continue\uff0c\u8bbe\u7f6e\u4e3a false \u8868\u793a\u91cd\u65b0\u8fdb\u884c\u4e00\u6b21\u626b\u63cf\uff0c\u800c\u4e0d\u662f\u63a5\u7740\u4e0a\u6b21\u7684\u7ee7\u7eed\n    BLEScanResults *foundDevices = pBLEScan-&gt;start(scanTime, false);\n\n    \/\/ \u6253\u5370\u672c\u6b21\u626b\u63cf\u5230\u7684\u8bbe\u5907\u603b\u6570\n    Serial.print(&quot;Devices found: &quot;);\n    Serial.println(foundDevices-&gt;getCount());\n    Serial.println(&quot;Scan done!&quot;);\n\n    \/\/ \u53ca\u65f6\u6e05\u7a7a\u626b\u63cf\u7ed3\u679c\u7f13\u51b2\u533a\uff0c\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u6216\u8fc7\u5ea6\u5360\u7528\u5185\u5b58\n    pBLEScan-&gt;clearResults();\n\n    \/\/ \u5ef6\u8fdf 2 \u79d2\u540e\u8fdb\u5165\u4e0b\u4e00\u8f6e\u626b\u63cf\u5faa\u73af\n    delay(2000);\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1786278548228.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 596px; --smush-placeholder-aspect-ratio: 596\/509;\" \/><\/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,2206,2205,1467,2203,1468],"class_list":["post-25139","post","type-post","status-publish","format-standard","hentry","category-all","category-arduino","category-c-c","category-esp32","category-platformio","category-599","tag-ble","tag-eddystonetlm_beacon","tag-eddystoneurl_beacon","tag-esp32","tag-ibeacon","tag-1468"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25139","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=25139"}],"version-history":[{"count":2,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25139\/revisions"}],"predecessor-version":[{"id":25150,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25139\/revisions\/25150"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}