2014-09-30 78 views
1

我目前通过科尔多瓦文档阅读和发现的基本轮廓如下:如何编写一个android cordova插件,如果蓝牙打开,将返回1,否则返回0?

package org.apache.cordova.plugin; 

import org.apache.cordova.api.CordovaPlugin; 
import org.apache.cordova.api.PluginResult; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

/** 
* This class echoes a string called from JavaScript. 
*/ 
public class Echo extends CordovaPlugin { 
    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals("echo")) { 
      String message = args.getString(0); 
      this.echo(message, callbackContext); 
      return true; 
     } 
     return false; 
    } 

    private void echo(String message, CallbackContext callbackContext) { 
     if (message != null && message.length() > 0) { 
      callbackContext.success(message); 
     } else { 
      callbackContext.error("Expected one non-empty string argument."); 
     } 
    } 
} 

这使得有很大的意义,我的理解Java代码如何被执行,调度信息返回给调用JavaScript的。

但是,我没有看到我如何访问android中的api,告诉我蓝牙是打开还是关闭。我需要导入android包吗?有关于此主题的文档?

感谢所有帮助

回答

2

是的,你需要导入BluetoothManagerBluetoothAdapter

事情是这样的:

import android.bluetooth.BluetoothManager; 
import android.bluetooth.BluetoothAdapter; 

... 

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
private BluetoothAdapter mBluetoothAdapter; 

... 

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) { 
    // BLUETOOTH is NOT SUPPORTED 
    //use PackageManager.FEATURE_BLUETOOTH_LE if you need bluetooth low energy 
    return false; 
} else { 
    mBluetoothAdapter = bluetoothManager.getAdapter(); 
    if (mBluetoothAdapter == null) { 
     // BLUETOOTH is NOT AVAILABLE 
     return false; 
    } else { 
     if (mBluetoothAdapter.isEnabled()) 
      // BLUETOOTH is TURNED ON 
      return true; 
     else 
      // BLUETOOTH is TURNED OFF 
      return false; 
    } 
} 

你可以更了解它:

+1

我刚刚编辑和改变'FEATURE_BLUETOOTH_LE'到'FEATURE_BLUETOOTH'来检查标准蓝牙,如果你需要低能量,尽管使用'FEATURE_BLUETOOTH_LE'常量 – benka 2014-09-30 15:05:44

相关问题