除非在调试模式Cordova iOS项目中刷新,否则无法正确加载源代码

时间:2017-12-08 14:36:53

标签: javascript ios cordova promise

我正在尝试初始化Cordova iOS项目中的蓝牙中心。来自this github的插件。我的代码如下所示:

-www

index.js

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
        //initialize the Bluetooth adapter
        document.addEventListener('deviceready', function () {
          new Promise(function (resolve) {
                bluetoothle.initialize(resolve, { request: true, statusReceiver: false });
              }).then(initializeSuccess, handleError);
          });
    },
/* rest part is just the Cordova helloword template*/
};

app.initialize();

/ServiceS/BLEService.js

function initializeSuccess(result) {

    if (result.status === "enabled") {

        log("Bluetooth is enabled.");
        log(result);
    }

    else {

        document.getElementById("start-scan").disabled = true;

        log("Bluetooth is not enabled:", "status");
        log(result, "status");
    }
}

function handleError(error) {

    var msg;

    if (error.error && error.message) {

        var errorItems = [];

        if (error.service) {

            errorItems.push("service: " + (uuids[error.service] || error.service));
        }

        if (error.characteristic) {

            errorItems.push("characteristic: " + (uuids[error.characteristic] || error.characteristic));
        }

        msg = "Error on " + error.error + ": " + error.message + (errorItems.length && (" (" + errorItems.join(", ") + ")"));
    }

    else {

        msg = error;
    }

    log(msg, "error");

    if (error.error === "read" && error.service && error.characteristic) {

        reportValue(error.service, error.characteristic, "Error: " + error.message);
    }
}

function log(msg, level) {

    level = level || "log";

    if (typeof msg === "object") {

        msg = JSON.stringify(msg, null, "  ");
    }

    console.log(msg);

    if (level === "status" || level === "error") {

        var msgDiv = document.createElement("div");
        msgDiv.textContent = msg;

        if (level === "error") {

            msgDiv.style.color = "red";
        }

        msgDiv.style.padding = "5px 0";
        msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px";
        document.getElementById("output").appendChild(msgDiv);
    }
}

和index.html

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>

        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>                
            </div>
            <p id="output"></p>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/Services/BLEService.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

基本上这里的一切都只是来自Cordova helloword和蓝牙插件的github的启动代码。我试图在XCode中构建项目,到目前为止一切正常。构建并在设备上运行没有错误。

在我直接在手机上运行项目之后,无论初始化成功还是失败都没有响应。我确实在输出xcode中显示消息&#34;已接收事件:deviceready&#34;虽然。所以我认为这只是&#34;承诺&#34;的一部分。 javascript没有用。比我从safari链接调试器并在那里使用开发人员工具。如果我在那里手动点击刷新,下次在手机上重新加载应用程序时,我会看到&#34;蓝牙已启用&#34;现在输出中有{&#34; status&#34;:&#34;已启用&#34;}。

但我不知道为什么承诺中的所有函数都不会在第一次加载。有人对此有任何线索吗?

1 个答案:

答案 0 :(得分:0)

好的。我最终了解了这个插件的.m部分。在2017年12月至11月的cordova-plugin-bluetoothle github上,如果你查看BluetoothLePlugin.m第664行,当你初始化centralManager时,第一次初始化它时,无论是启用还是禁用它都不会返回。只有您第二次召回它,您才能看到“已启用”的反馈。因此,我将index.js中的代码更改为以下内容:

document.addEventListener('deviceready', function () {
          //the reason following function be called twice is, the plugin is designed like this
          //(check BluetoothLePlugin.m line 664 and after)
          //first time this function will intialize the centralManager, but will not check whether it is enabled or not
          new Promise(function (resolve) {
                      bluetoothle.initialize(resolve, { request: true, statusReceiver: false });
                      }).then(initializeSuccess, handleError);
          //for the second time it will check if it's enabled or not
          new Promise(function (resolve) {
                      bluetoothle.initialize(resolve, { request: true, statusReceiver: false });
                      }).then(initializeSuccess, handleError);
          });

它的工作正如我预期的那样。我正在考虑向作者报告这个有趣的“特征”,看看这个奇怪的解决方案是由于我的误解还是什么。