【问题标题】:How to connect my wearable to a Node.js server on Tizen?如何将我的可穿戴设备连接到 Tizen 上的 Node.js 服务器?
【发布时间】:2019-11-08 17:33:45
【问题描述】:

我在使用 Tizen 从可穿戴模拟器访问 Node.js 服务器上的某个路由时遇到问题。

我尝试使用警报来检查是否到达了通信的部分(.open 和 .send),并且它似乎可以完美地到达它们并且没有检测到错误。我还确保添加正确的权限和访问权限。

        function postDataToServer() {
        var xmlHttp = new XMLHttpRequest();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
                if (xmlHttp.status === 200) {
                    alert("data posted successfully..");
                } else {

                }
            }
        };

        xmlHttp.open("PUT", "http://localhost:3004/read_watch",true);
        alert('hi');
        xmlHttp.send(null);
        }

当建立与 read_watch 的连接时,我应该期待编译器上的 console.log 消息。

【问题讨论】:

    标签: xml http tizen samsung-galaxy-gear


    【解决方案1】:

    我刚刚在我的 Tizen 可穿戴模拟器上用 nodejs 本地服务器测试了你的代码 sn-p。

    我的开发环境如下:

    • Windows7 上的 Tizen Studio v3.3
    • Windows7 上的 Tizen v5.0 可穿戴仿真器
    • Ubuntu 16.04.3 LTS 上的 nodejs 服务器

    以下是具有互联网权限的config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" id="http://yourdomain/BasicXHR" version="1.0.0" viewmodes="maximized">
        <tizen:application id="lvdoC7pmtq.BasicXHR" package="lvdoC7pmtq" required_version="4.0"/>
        <content src="index.html"/>
        <feature name="http://tizen.org/feature/screen.size.all"/>
        <icon src="icon.png"/>
        <name>BasicXHR</name>
        <tizen:profile name="wearable"/>
        <tizen:privilege name="http://tizen.org/privilege/internet"/>
    </widget>
    

    以下是 index.html,其中包含您的代码并在 Tizen Wearable Emulator 上运行:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
        <meta name="description" content="Tizen Wearable basic template generated by Samsung Wearable Web IDE"/>
    
        <title>Tizen Wearable Web IDE - Tizen Wearable - Tizen Wearable basic Application</title>
    
        <link rel="stylesheet" type="text/css" href="css/style.css"/>
        <script src="js/main.js"></script>
        <script>    
        function postDataToServer() {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState === 4) {
                    if (xmlHttp.status === 200) {
                        console.log("data posted successfully.." + xmlHttp.responseText);
                    } else {
                        console.log("Error");
                    }
                }
            };
            xmlHttp.open("PUT", "http://{My Ubuntu IP Address}:3004/read_watch",true);
            console.log('hi');
            xmlHttp.send(null);
        }
        postDataToServer();
        </script>
    </head>
    
    <body>
      <div class=contents>
        <div style='margin:auto;'>
            <span class=content_text id=textbox>Basic</span>
        </div>
      </div>
    </body>
    </html>
    

    以下是我在 Ubuntu 服务器上运行的 nodejs 代码 sn-p:

    var http = require('http');
    http.createServer(function (req, res) {
      if (req.url == '/read_watch') {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><p>got message from Tizen</p></body></html>');
        res.end();
      }
    }).listen(3004);
    

    以下是 Tizen Wearable Emulator 控制台上的日志:

    file:///index.html (25) :hi
    file:///index.html (18) :data posted successfully..<html><body><p>got message from Tizen</p></body></html>
    

    随着更改,它可以正常工作,正如我们预期的那样。 :)

    我想知道您在 config.xml 中指定了哪个权限,以及是否有任何网络问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多