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 c70dc18
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 62 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
59 changes: 40 additions & 19 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 @@ -486,27 +489,29 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
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 +578,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 +616,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 +725,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 Down Expand Up @@ -751,6 +759,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 +797,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 +829,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 +857,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 +891,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 +916,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 +941,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 +953,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 +1047,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 +1147,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 +1200,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 +1300,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 +1325,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 Expand Up @@ -1485,7 +1506,7 @@ public void onReceive(Context context, Intent intent) {
}
}
};
webView.getContext().registerReceiver(bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED));
ContextCompat.registerReceiver(webView.getContext(), bondStateReceiver, new IntentFilter(ACTION_BOND_STATE_CHANGED), ContextCompat.RECEIVER_EXPORTED);
}
}

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 c70dc18

Please sign in to comment.