2015-09-04 67 views
0

我已经安装了Rand Dusing的BLE Cordova插件,并按照他的示例初始化蓝牙,但我无法克服这一点。这是我index.js插件不工作

var blePlugin = window.bluetoothle; 
 

 
function bleInitialize() { 
 
    var paramsObj = { 
 
    request: true, 
 
    statusReceiver: true 
 
    }; 
 
    document.getElementById("BTHWStatus").innerHTML = 'Initialze BLE: Checking BT status...'; 
 
    blePlugin.initialize(bleInitializeSuccess, bleInitializeError, paramsObj); 
 
    document.getElementById("BTHWStatus").innerHTML = 'Initialze BLE: Complete'; 
 
    return false; 
 
} 
 

 
function bleInitializeSuccess(obj) { 
 
    if (obj.status == "enabled") 
 
    document.getElementById("BTHWStatus").innerHTML = 'BT is turned ON'; 
 
    else 
 
    document.getElementById("BTHWStatus").innerHTML = 'BLE:Initialize: Unexpected error'; 
 
} 
 

 
function bleInitializeError(obj) { 
 
    document.getElementById("BTHWStatus").innerHTML = "Initialize Error : " + JSON.stringify(obj); 
 
}

这是index.html

<h3>Bluetooth Test App</h3> 
 
<br> 
 
<div> 
 
    <a id="BTHWStatus">BT is turned OFF</a> 
 
    <br/> 
 
    <br/> 
 
    <span>Enable/Disable Bluetooth :</span> 
 
    <input type="checkbox" id="BTSelect"> 
 
    <button id="BTApply">Apply</button> 
 
    <br/> 
 
</div>

谁能告诉我,如果我在index.js文件的任何不正确,可能会阻止该插头在不工作?我已经使用命令cordova plugin验证了插件已正确安装在命令提示符中。

回调函数是否正确编码或者我错过了什么?我可以通过blePlugin.initialize函数在应用页面上打印Initialze BLE: Checking BT status...,但之后没有任何结果。

感谢并感谢您的帮助。

回答

0

您的应用程序代码是否被执行after the deviceready event?从你的代码我看不到它,所以问题可能是这个。

如果您不等待deviceready事件,那么js代码将会正常工作,但Cordova相关功能将不可用。

+0

是的,'deviceready'后面的应用程序代码正在执行,我可以从'deviceready'中调用设备信息(来自cordova-plugin-device的Cordova,Version,Model等)。我也从deviceready调用'bleInitialize'。 – SB77