PhoneGap 3.0.0 Tanelih 蓝牙插件在 Android 上已启用错误回调不起作用

PhoneGap 3.0.0 Tanelih Bluetooth Plugin on Android isEnabled onError callback not working

本文关键字:启用 回调 不起作用 Android 错误 Tanelih 插件 PhoneGap      更新时间:2023-09-26

我正在测试Tanelih的蓝牙插件在Android上的PhoneGap 3.0.0的功能。该插件似乎运行良好;我可以使用链接到JavaScript函数的HTML按钮打开和关闭蓝牙,并获得onSuccess/onError回调以显示函数是否有效的消息。

但是,当我尝试查看是否使用window.bluetooth.isEnabled(isEnabledSuccess, isEnabledError);启用蓝牙时,无论蓝牙是启用还是禁用,回调始终是已启用成功。

以下是我的一些索引.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>

这是一些蓝牙.js(我没有碰过这个文件):

    Bluetooth.prototype.isEnabled = function(onSuccess, onError)
    {
    exec(onSuccess, onError, "Bluetooth", "isEnabled", []);
    }

以下是一些蓝牙插件.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 异常时,才会调用错误函数(这不太可能)。成功函数返回一个布尔值,告诉您蓝牙是否已启用。因此,请尝试以下操作:

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;
}