From 8a04ac34b4150a04c2136bd0182751c89bc3e16d Mon Sep 17 00:00:00 2001 From: Oleksandr Korneiko Date: Tue, 18 Oct 2022 10:42:10 +0300 Subject: [PATCH] fix: android 12 enable permissions call --- src/android/BLECentralPlugin.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/android/BLECentralPlugin.java b/src/android/BLECentralPlugin.java index c3df495f..64f2d821 100644 --- a/src/android/BLECentralPlugin.java +++ b/src/android/BLECentralPlugin.java @@ -344,8 +344,19 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback } else if (action.equals(ENABLE)) { enableBluetoothCallback = callbackContext; - Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH); + + //check android12+ - perms should be asked in a bulk + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + String[] ANDROID_12_BLE_PERMISSIONS = new String[]{ + Manifest.permission.BLUETOOTH_SCAN, + Manifest.permission.BLUETOOTH_CONNECT + }; + cordova.requestPermissions(this, REQUEST_ENABLE_BLUETOOTH, ANDROID_12_BLE_PERMISSIONS); + } + else{ + Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH); + } } else if (action.equals(START_STATE_NOTIFICATIONS)) { @@ -1227,6 +1238,12 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int } switch(requestCode) { + case REQUEST_ENABLE_BLUETOOTH: + LOG.d(TAG, "User enabled Bluetooth"); + if (enableBluetoothCallback != null) { + enableBluetoothCallback.success(); + } + break; case REQUEST_BLUETOOTH_SCAN: LOG.d(TAG, "User granted Bluetooth Scan Access"); findLowEnergyDevices(permissionCallback, serviceUUIDs, scanSeconds, scanSettings);