2015-09-04 65 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