我正在测试 Tanelih's Bluetooth Plugin 的功能适用于 Android 上的 PhoneGap 3.0.0。 该插件似乎运行良好;我可以使用链接到 JavaScript 函数的 HTML 按钮打开和关闭蓝牙,并获取 onSuccess/onError 回调以显示消息(无论函数是否有效)。
但是,当我尝试使用 window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
查看是否启用了蓝牙时无论蓝牙是启用还是禁用,回调始终是 isEnabledSuccess。
这是我的一些 index.html:
<head>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
}
function isEnabledSuccess(isEnabled)
{
var element = document.getElementById('status');
element.innerHTML = "Enabled";
}
function isEnabledError(isEnabled)
{
var element = document.getElementById('status');
element.innerHTML = "Disabled";
}
</script>
</head>
<body>
<p id="status"></p>
</body>
这是一些 bluetooth.js(我没有接触过这个文件):
Bluetooth.prototype.isEnabled = function(onSuccess, onError)
{
exec(onSuccess, onError, "Bluetooth", "isEnabled", []);
}
这里是一些BluetoothPlugin.java(我没有接触过这个文件):
/**
* Is Bluetooth on.
*
* @param args Arguments given.
* @param callbackCtx Where to send results.
*/
private void isEnabled(JSONArray args, CallbackContext callbackCtx)
{
try
{
callbackCtx.sendPluginResult(new PluginResult(PluginResult.Status.OK, _bluetooth.isEnabled()));
}
catch(Exception e)
{
this.error(callbackCtx, e.getMessage(), BluetoothError.ERR_UNKNOWN);
}
}
有没有人有什么想法?
最佳答案
只有在调用插件期间发生 Java 异常时才会调用错误函数(这不太可能)。成功函数返回一个 bool 值,告诉您蓝牙是否启用。因此,请尝试这样的事情:
function onDeviceReady()
{
window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);
}
function isEnabledSuccess(isEnabled)
{
var element = document.getElementById('status');
if(isEnabled){
element.innerHTML = "Enabled";
}else{
element.innerHTML = "Disabled";
}
}
function isEnabledError(error)
{
var element = document.getElementById('status');
element.innerHTML = "Cannot determine Bluetooth status: " + error.message;
}
关于javascript - Android 上的 PhoneGap 3.0.0 Tanelih 蓝牙插件已启用 onError 回调无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19595048/