{"id":8025,"date":"2021-03-22T21:02:19","date_gmt":"2021-03-22T13:02:19","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=8025"},"modified":"2021-03-22T21:03:53","modified_gmt":"2021-03-22T13:03:53","slug":"wt32-eth01-web-bluetooth","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=8025","title":{"rendered":"WT32-ETH01 - Web Bluetooth"},"content":{"rendered":"<p>--<\/p>\n<ul>\n<li><a href=\"https:\/\/googlechrome.github.io\/samples\/web-bluetooth\/index.html\" target=\"_blank\" rel=\"noopener\">Web Bluetooth Samples<\/a><\/li>\n<li><a href=\"https:\/\/dotblogs.com.tw\/RichardNote\/2019\/08\/14\/173603\" target=\"_blank\" rel=\"noopener\">[TypeScript] \u900f\u904e\u85cd\u82bd\u9023\u7dda\u63a5\u6536\u8cc7\u6599<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/WebBluetoothCG\/demos\" target=\"_blank\" rel=\"noopener\">WebBluetoothCG \/ demos<\/a><\/li>\n<\/ul>\n<p>\u4f7f\u7528 demo \u53c3\u7167 sample \u4e92\u76f8\u9a57\u8b49\u8f03\u5bb9\u6613\u641e\u61c2\uff0cUUID \u5fc5\u9808\u4f7f\u7528\u5b8c\u6574\u683c\u5f0f<\/p>\n<p><strong>\u904b\u4f5c\u689d\u4ef6<\/strong><\/p>\n<ol>\n<li>\u7db2\u9801\u5fc5\u9808 HTTPS<\/li>\n<li>\u624b\u6a5f\u700f\u89bd\u5668\u7248\u672c\u5fc5\u9808\u7b26\u5408 <a title=\"Permalink to Browser compatibility\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Web_Bluetooth_API#browser_compatibility\" target=\"_blank\" rel=\"noopener\">Browser compatibility<\/a><\/li>\n<\/ol>\n<p>--<\/p>\n<h2>\u7bc4\u4f8b\uff1a\u4f7f\u7528 Web Bluetooth \u63a7\u5236 WT32-ETH01 \u4e0a\u7684 LED<\/h2>\n<p>HTML<\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\r\n    &lt;title&gt;Web Bluetooth&lt;\/title&gt;\r\n    &lt;script src=\"\/3rdParty\/jquery-1.11.3.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/3rdParty\/php.js\"&gt;&lt;\/script&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"\/3rdParty\/bootstrap-4.3.1\/css\/bootstrap.min.css\"&gt;\r\n\r\n    &lt;!--  --&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"\/3rdParty\/bootstrap4-toggle\/css\/bootstrap4-toggle.min.css\"&gt;\r\n    &lt;script src=\"\/3rdParty\/bootstrap4-toggle\/js\/bootstrap4-toggle.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"container\"&gt;\r\n    &lt;div&gt;\r\n        &lt;button type=\"button\" class=\"btn btn-info\" onclick=\"a()\"&gt;\u914d\u5c0d&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n\r\n    &lt;div class=\"toggle_box\"&gt;\r\n        &lt;label&gt;&lt;input type=\"checkbox\" class=\"input_toggle\" data-style=\"fast\" data-toggle=\"toggle\"&gt;&lt;\/label&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;script&gt;\r\n    var myCharacteristic;\r\n    var bluetoothDevice;\r\n\r\n    \/\/ \u4e0d\u80fd\u7db2\u9801\u8f09\u5165\u5c31\u57f7\u884c  \u5fc5\u9808\u624b\u52d5\u89f8\u767c\r\n    function a(){\r\n        var options = {\r\n            \/\/ acceptAllDevices: true\r\n            filters: [\r\n                { namePrefix: 'ESP32' }\r\n            ],\r\n            optionalServices: [ 'f5c8924c-86e9-11eb-8dcd-0242ac130003' ]\r\n        };\r\n\r\n        \/\/ \u6383\u63cf\r\n        bluetoothDevice = null;\r\n        navigator.bluetooth.requestDevice(options).then(function ( device ){\r\n            bluetoothDevice = device;\r\n            return device.gatt.connect();\r\n        })\r\n        .then(function(server){\r\n            return server.getPrimaryService('f5c8924c-86e9-11eb-8dcd-0242ac130003');\r\n        })\r\n        .then(function(service){\r\n            return service.getCharacteristic('fea784d6-86e9-11eb-8dcd-0242ac130003');\r\n        })\r\n        .then(function(characteristic){\r\n            myCharacteristic = characteristic;\r\n        })\r\n        .catch(function ( error ){\r\n        });\r\n    }\r\n\r\n    \/\/ switch button\r\n    $('.input_toggle').change(function() {\r\n        point_click($(this).prop('checked'));\r\n    });\r\n\r\n    function point_click($power){\r\n        if ($power) control = 'on';\r\n        else        control = 'off';\r\n        if ( myCharacteristic ){\r\n            var encoder = new TextEncoder('utf-8');\r\n            var sendMsg = '{\"led\":\"'+ control +'\"}';\r\n            myCharacteristic.writeValue(encoder.encode(sendMsg));\r\n        }\r\n    }\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Arduino<\/p>\n<pre class=\"lang:arduino decode:true\">#include &lt;Arduino.h&gt;\r\n#include &lt;BLEDevice.h&gt;\r\n#include &lt;BLEServer.h&gt;\r\n#include &lt;BLEUtils.h&gt;\r\n#include &lt;BLE2902.h&gt;\r\n#include &lt;ArduinoJson.h&gt;\r\n\r\n#define BAUDRATE 115200\r\n#define SERVICE_UUID        \"f5c8924c-86e9-11eb-8dcd-0242ac130003\"\r\n#define CHARACTERISTIC_UUID \"fea784d6-86e9-11eb-8dcd-0242ac130003\"\r\n\r\n\/\/ \r\nBLEServer *pServer;\r\nBLEService *pService;\r\nBLECharacteristic *pCharacteristic;\r\nbool deviceConnected = false;\r\nstd::string rxValue = \"\";\r\n\r\n\/\/ \r\nclass MyServerCallbacks : public BLEServerCallbacks\r\n{\r\n    void onConnect(BLEServer *pServer)\r\n    {\r\n        Serial.println(\"= Connected =\");\r\n        deviceConnected = true;\r\n    }\r\n    void onDisconnect(BLEServer *pServer)\r\n    {\r\n        Serial.println(\"= Disonnected =\");\r\n        deviceConnected = false;\r\n    }\r\n};\r\n\r\nclass MyCallbacks : public BLECharacteristicCallbacks\r\n{\r\n    void onWrite(BLECharacteristic *pCharacteristic)\r\n    {\r\n        \/\/ Serial.println(\"= Receive =\");\r\n        rxValue = pCharacteristic-&gt;getValue();\r\n        String rx_str = rxValue.c_str();\r\n        Serial.println(rx_str);\r\n\r\n        \/\/ JSON \u8655\u7406\r\n        StaticJsonDocument&lt;256&gt; doc;\r\n        char json_val[rx_str.length()+1];\r\n        rx_str.toCharArray(json_val, rx_str.length()+1);\r\n        deserializeJson(doc, json_val, rx_str.length());\r\n        String val = doc[\"led\"];\r\n        Serial.println(val);\r\n\r\n        if (val == \"on\")\r\n        {\r\n            digitalWrite(5, LOW);\r\n        }\r\n        if (val == \"off\")\r\n        {\r\n            digitalWrite(5, HIGH);\r\n        }\r\n    }\r\n};\r\n\r\nvoid setup()\r\n{\r\n    Serial.begin(BAUDRATE);\r\n    pinMode(5, OUTPUT);\r\n\r\n    BLEDevice::init(\"ESP32\");\r\n    pServer = BLEDevice::createServer();\r\n    pServer-&gt;setCallbacks(new MyServerCallbacks());\r\n    pService = pServer-&gt;createService(SERVICE_UUID);\r\n    pCharacteristic = pService-&gt;createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE);\r\n    pCharacteristic-&gt;addDescriptor(new BLE2902());\r\n    pCharacteristic-&gt;setCallbacks(new MyCallbacks());\r\n    pService-&gt;start();\r\n    pServer-&gt;getAdvertising()-&gt;start();\r\n}\r\n\r\nvoid loop()\r\n{\r\n}<\/pre>\n<p>--<\/p>\n<h2>\u6f14\u793a<\/h2>\n<p>\u914d\u5c0d\u6642\uff0c\u4ec0\u9ebc\u53ef\u4ee5\u9078\u64c7\u7684\u6642\u6a5f\u5f88\u8ff7\uff0c\u63a7\u5236\u7684 LED \u662f\u53f3\u5074\u7684\u7da0\u8272 LED<\/p>\n<p><iframe loading=\"lazy\" width=\"852\" height=\"480\" src=\"https:\/\/www.youtube.com\/embed\/FSYaCzByDg4\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>--<\/p>\n<h2>\u554f\u984c<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.esp32.com\/viewtopic.php?f=19&amp;t=4546\" target=\"_blank\" rel=\"noopener\">BLE arduino send values over 20 bytes<\/a><\/li>\n<\/ul>\n<p>BLE\u00a0notify \u6700\u9577\u53ea\u80fd 20 bytes<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"8025\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,838&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- Web Bluetoot...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"8025\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,838&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/8025"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8025"}],"version-history":[{"count":13,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/8025\/revisions"}],"predecessor-version":[{"id":8042,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/8025\/revisions\/8042"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}