gpt4 book ai didi

java - 我如何编写一个 android cordova 插件,如果蓝牙打开则返回 1,否则返回 0?

转载 作者:行者123 更新时间:2023-11-29 21:00:56 28 4
gpt4 key购买 nike

我目前正在阅读cordova文档,发现基本大纲如下:

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 代码并将信息分派(dispatch)回调用 javascript。

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

感谢您的帮助

最佳答案

是的,您需要导入 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;
}
}

你可以阅读更多:

关于java - 我如何编写一个 android cordova 插件,如果蓝牙打开则返回 1,否则返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123835/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com