Skip to content

Instantly share code, notes, and snippets.

@johnpc
Created September 30, 2014 20:24
Show Gist options
  • Save johnpc/26098db96d7d4f8efdb8 to your computer and use it in GitHub Desktop.
Save johnpc/26098db96d7d4f8efdb8 to your computer and use it in GitHub Desktop.
A cordova plugin that checks whether an android device has bluetooth activated
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;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothAdapter;
/**
* This class determines whether android device has an active bluetooth connection, called from JavaScript using Cordova.
**/
public class hasBluetooth extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment