【问题标题】:PhoneGap 3.0.0 Tanelih Bluetooth Plugin on Android isEnabled onError callback not workingAndroid上的PhoneGap 3.0.0 Tanelih蓝牙插件isEnabled onError回调不起作用
【发布时间】:2013-10-25 16:30:24
【问题描述】:

我正在为 Android 上的 PhoneGap 3.0.0 测试 Tanelih's Bluetooth Plugin 的功能。 该插件似乎运行良好;我可以使用链接到 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);
        }
    }

有人有什么想法吗?

【问题讨论】:

    标签: javascript android cordova bluetooth phonegap-plugins


    【解决方案1】:

    只有在调用插件期间发生 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多