Skip to content

Commit

Permalink
Suppress permission-related lint warnings on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Jul 13, 2024
1 parent 2ebde63 commit 7925381
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ See the [examples](https://github.com/don/cordova-plugin-ble-central/tree/master
## Supported Platforms

- iOS
- Android (likely supports 6+, but 8.1 or greater recommended)
- Android 6+ (8.1 or greater recommended)
- Browser (where navigator.bluetooth is supported)

# Installing
Expand Down
156 changes: 91 additions & 65 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.megster.cordova.ble.central;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
Expand Down Expand Up @@ -170,6 +171,7 @@ protected void pluginInitialize() {
}
}

@SuppressLint("MissingPermission")
@Override
public void onDestroy() {
removeStateListener();
Expand All @@ -180,6 +182,7 @@ public void onDestroy() {
}
}

@SuppressLint("MissingPermission")
@Override
public void onReset() {
removeStateListener();
Expand All @@ -196,7 +199,7 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback

if (bluetoothAdapter == null) {
Activity activity = cordova.getActivity();
boolean hardwareSupportsBLE = activity.getApplicationContext()
@SuppressLint("ObsoleteSdkInt") boolean hardwareSupportsBLE = activity.getApplicationContext()
.getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) &&
Build.VERSION.SDK_INT >= 18;
Expand Down Expand Up @@ -434,79 +437,86 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
validAction = false;
break;
}

switch (options.optString("callbackType", "")) {
case "":
break;
case "all":
scanSettings.setCallbackType( ScanSettings.CALLBACK_TYPE_ALL_MATCHES );
break;
case "first":
scanSettings.setCallbackType( ScanSettings.CALLBACK_TYPE_FIRST_MATCH );
break;
case "lost":
scanSettings.setCallbackType( ScanSettings.CALLBACK_TYPE_MATCH_LOST );
break;
default:
callbackContext.error("callbackType must be one of: all | first | lost");
validAction = false;
break;
if (Build.VERSION.SDK_INT >= 23) {
switch (options.optString("callbackType", "")) {
case "":
break;
case "all":
scanSettings.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
break;
case "first":
scanSettings.setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH);
break;
case "lost":
scanSettings.setCallbackType(ScanSettings.CALLBACK_TYPE_MATCH_LOST);
break;
default:
callbackContext.error("callbackType must be one of: all | first | lost");
validAction = false;
break;
}
}

switch (options.optString("matchMode", "")) {
case "":
break;
case "aggressive":
scanSettings.setMatchMode( ScanSettings.MATCH_MODE_AGGRESSIVE );
break;
case "sticky":
scanSettings.setMatchMode( ScanSettings.MATCH_MODE_STICKY );
break;
default:
callbackContext.error("matchMode must be one of: aggressive | sticky");
validAction = false;
break;
if (Build.VERSION.SDK_INT >= 23) {
switch (options.optString("matchMode", "")) {
case "":
break;
case "aggressive":
scanSettings.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE);
break;
case "sticky":
scanSettings.setMatchMode(ScanSettings.MATCH_MODE_STICKY);
break;
default:
callbackContext.error("matchMode must be one of: aggressive | sticky");
validAction = false;
break;
}
}

switch (options.optString("numOfMatches", "")) {
case "":
break;
case "one":
scanSettings.setNumOfMatches( ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT );
break;
case "few":
scanSettings.setNumOfMatches( ScanSettings.MATCH_NUM_FEW_ADVERTISEMENT );
break;
case "max":
scanSettings.setNumOfMatches( ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT );
break;
default:
callbackContext.error("numOfMatches must be one of: one | few | max");
validAction = false;
break;
if (Build.VERSION.SDK_INT >= 23) {
switch (options.optString("numOfMatches", "")) {
case "":
break;
case "one":
scanSettings.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT);
break;
case "few":
scanSettings.setNumOfMatches(ScanSettings.MATCH_NUM_FEW_ADVERTISEMENT);
break;
case "max":
scanSettings.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT);
break;
default:
callbackContext.error("numOfMatches must be one of: one | few | max");
validAction = false;
break;
}
}

switch (options.optString("phy", "")) {
case "":
break;
case "1m":
scanSettings.setPhy( BluetoothDevice.PHY_LE_1M );
break;
case "coded":
scanSettings.setPhy( BluetoothDevice.PHY_LE_CODED );
break;
case "all":
scanSettings.setPhy( ScanSettings.PHY_LE_ALL_SUPPORTED );
break;
default:
callbackContext.error("phy must be one of: 1m | coded | all");
validAction = false;
break;
if (Build.VERSION.SDK_INT >= 26 /*O*/) {
switch (options.optString("phy", "")) {
case "":
break;
case "1m":
scanSettings.setPhy(BluetoothDevice.PHY_LE_1M);
break;
case "coded":
scanSettings.setPhy(BluetoothDevice.PHY_LE_CODED);
break;
case "all":
scanSettings.setPhy(ScanSettings.PHY_LE_ALL_SUPPORTED);
break;
default:
callbackContext.error("phy must be one of: 1m | coded | all");
validAction = false;
break;
}
}

if (validAction) {
String LEGACY = "legacy";
if (!options.isNull(LEGACY))
if (Build.VERSION.SDK_INT >= 26 /*O*/ && !options.isNull(LEGACY))
scanSettings.setLegacy( options.getBoolean(LEGACY) );

long reportDelay = options.optLong("reportDelay", -1 );
Expand Down Expand Up @@ -573,6 +583,7 @@ private void enableBluetooth(CallbackContext callbackContext) {
cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);
}

@SuppressLint("MissingPermission")
private void getBondedDevices(CallbackContext callbackContext) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
if (!PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
Expand Down Expand Up @@ -610,6 +621,7 @@ private UUID[] parseServiceUUIDList(JSONArray jsonArray) throws JSONException {
return serviceUUIDs.toArray(new UUID[jsonArray.length()]);
}

@SuppressLint("MissingPermission")
private void onBluetoothStateChange(Intent intent) {
final String action = intent.getAction();

Expand Down Expand Up @@ -718,6 +730,7 @@ private void removeLocationStateListener() {
this.locationStateReceiver = null;
}

@SuppressLint("MissingPermission")
private void connect(CallbackContext callbackContext, String macAddress) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
if (!PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
Expand All @@ -734,7 +747,7 @@ private void connect(CallbackContext callbackContext, String macAddress) {
return;
}

if (!peripherals.containsKey(macAddress) && BLECentralPlugin.this.bluetoothAdapter.checkBluetoothAddress(macAddress)) {
if (!peripherals.containsKey(macAddress) && BluetoothAdapter.checkBluetoothAddress(macAddress)) {
BluetoothDevice device = BLECentralPlugin.this.bluetoothAdapter.getRemoteDevice(macAddress);
Peripheral peripheral = new Peripheral(device);
peripherals.put(macAddress, peripheral);
Expand All @@ -751,6 +764,7 @@ private void connect(CallbackContext callbackContext, String macAddress) {

}

@SuppressLint("MissingPermission")
private void autoConnect(CallbackContext callbackContext, String macAddress) {

if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
Expand Down Expand Up @@ -788,6 +802,7 @@ private void autoConnect(CallbackContext callbackContext, String macAddress) {

}

@SuppressLint("MissingPermission")
private void disconnect(CallbackContext callbackContext, String macAddress) {

Peripheral peripheral = peripherals.get(macAddress);
Expand Down Expand Up @@ -819,6 +834,7 @@ private void setPin(CallbackContext callbackContext, final String pin) {
}

broadCastReceiver = new BroadcastReceiver() {
@SuppressLint("MissingPermission")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Expand Down Expand Up @@ -846,6 +862,7 @@ public void onReceive(Context context, Intent intent) {
}
}

@SuppressLint("MissingPermission")
private void bond(CallbackContext callbackContext, String macAddress, boolean usePairingDialog) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
List<String> missingPermissions = new ArrayList<String>();
Expand Down Expand Up @@ -879,6 +896,7 @@ private void bond(CallbackContext callbackContext, String macAddress, boolean us
}
}

@SuppressLint("MissingPermission")
private void unbond(CallbackContext callbackContext, String macAddress) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
if (!PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
Expand All @@ -903,6 +921,7 @@ private void unbond(CallbackContext callbackContext, String macAddress) {
}
}

@SuppressLint("MissingPermission")
private void readBondState(CallbackContext callbackContext, String macAddress) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
if (!PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
Expand All @@ -927,6 +946,7 @@ private void readBondState(CallbackContext callbackContext, String macAddress) {
}
}

@SuppressLint("MissingPermission")
private void requestMtu(CallbackContext callbackContext, String macAddress, int mtuValue) {
Peripheral peripheral = peripherals.get(macAddress);
if (peripheral != null) {
Expand All @@ -938,6 +958,7 @@ private void requestMtu(CallbackContext callbackContext, String macAddress, int
}
}

@SuppressLint("MissingPermission")
private void requestConnectionPriority(CallbackContext callbackContext, String macAddress, String priority) {
Peripheral peripheral = peripherals.get(macAddress);

Expand Down Expand Up @@ -1031,6 +1052,7 @@ private void write(CallbackContext callbackContext, String macAddress, UUID serv

}

@SuppressLint("MissingPermission")
private void connectL2cap(CallbackContext callbackContext, String macAddress, int psm, boolean secureChannel) {
Peripheral peripheral = peripherals.get(macAddress);
if (peripheral == null) {
Expand Down Expand Up @@ -1130,6 +1152,7 @@ private void removeNotifyCallback(CallbackContext callbackContext, String macAdd
}

private ScanCallback leScanCallback = new ScanCallback() {
@SuppressLint("MissingPermission")
@Override
public void onScanResult(int callbackType, ScanResult result) {
LOG.w(TAG, "Scan Result");
Expand Down Expand Up @@ -1182,6 +1205,7 @@ public void onScanFailed(int errorCode) {
};


@SuppressLint("MissingPermission")
private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds, ScanSettings scanSettings) {

if (!locationServicesEnabled() && Build.VERSION.SDK_INT < 31) {
Expand Down Expand Up @@ -1281,6 +1305,7 @@ private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] servic
callbackContext.sendPluginResult(result);
}

@SuppressLint("MissingPermission")
private void stopScan() {
stopScanHandler.removeCallbacks(stopScanRunnable);
if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
Expand All @@ -1305,6 +1330,7 @@ private boolean locationServicesEnabled() {
return (locationMode > 0);
}

@SuppressLint("MissingPermission")
private void listKnownDevices(CallbackContext callbackContext) {
if (COMPILE_SDK_VERSION >= 31 && Build.VERSION.SDK_INT >= 31) { // (API 31) Build.VERSION_CODE.S
if (!PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
Expand Down
25 changes: 4 additions & 21 deletions src/android/BLECommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class BLECommand {
// BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
// BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT

private CallbackContext callbackContext;
private UUID serviceUUID;
private UUID characteristicUUID;
private final CallbackContext callbackContext;
private final UUID serviceUUID;
private final UUID characteristicUUID;
private byte[] data;
private int type;
private int psm;

private final int type;

public BLECommand(CallbackContext callbackContext, UUID serviceUUID, UUID characteristicUUID, int type) {
this.callbackContext = callbackContext;
Expand All @@ -40,19 +38,6 @@ public BLECommand(CallbackContext callbackContext, UUID serviceUUID, UUID charac
this.type = type;
}

public BLECommand(CallbackContext callbackContext, int psm, int type) {
this.callbackContext = callbackContext;
this.psm = psm;
this.type = type;
}

public BLECommand(CallbackContext callbackContext, int psm, byte[] data, int type) {
this.callbackContext = callbackContext;
this.psm = psm;
this.data = data;
this.type = type;
}

public int getType() {
return type;
}
Expand All @@ -72,6 +57,4 @@ public UUID getCharacteristicUUID() {
public byte[] getData() {
return data;
}

public int getPSM() { return psm; }
}
4 changes: 2 additions & 2 deletions src/android/L2CAPContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.annotation.RequiresPermission;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.LOG;
Expand Down Expand Up @@ -34,6 +34,7 @@ public L2CAPContext(BluetoothDevice device, int psm) {
this.executor = Executors.newSingleThreadExecutor();
}

@RequiresPermission("android.permission.BLUETOOTH_CONNECT")
public void connectL2cap(CallbackContext callbackContext, boolean secureChannel) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Expand Down Expand Up @@ -107,7 +108,6 @@ public void writeL2CapChannel(CallbackContext callbackContext, byte[] data) {
}
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void readL2CapData() {
try {
final BluetoothSocket lSocket = this.socket;
Expand Down
Loading

0 comments on commit 7925381

Please sign in to comment.