Skip to content

Commit

Permalink
Merge pull request #10 from BigRoomStudios/android-12-perms
Browse files Browse the repository at this point in the history
Android 12 perms
  • Loading branch information
Brian Leighton authored Aug 29, 2022
2 parents 2acab1c + 4c49376 commit 8191e90
Showing 1 changed file with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;

import com.google.gson.Gson;

Expand Down Expand Up @@ -256,6 +257,8 @@ public void discoverDevice() {
}
CCConsumerApi api = CCConsumer.getInstance().getApi();

debug("starting discovery");

mBluetoothSearchResponseListener = new BluetoothSearchResponseListener() {
@Override
public void onDeviceFound(BluetoothDevice device) {
Expand All @@ -265,6 +268,9 @@ public void onDeviceFound(BluetoothDevice device) {
params.putString("id", device.getAddress());
params.putString("name", device.getName());

debug("on device found");
debug(device.getName());

sendEvent("BoltDeviceFound", params);
}
};
Expand Down Expand Up @@ -342,23 +348,56 @@ private void validateExpiryDate(String expiryDate) throws ValidateException {
}

@ReactMethod
private void setupConsumerApiEndpoint(String url) {
public void setupConsumerApiEndpoint(String url) {

CCConsumer.getInstance().getApi().setEndPoint("https://" + url);
}

private Boolean checkPermission() {
@ReactMethod
public Boolean checkPermission() {

int currentVer = android.os.Build.VERSION.SDK_INT;

boolean hasPermission = (
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
);

if (currentVer >= 31 && hasPermission) {
hasPermission = (hasPermission &&
ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH") == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_ADMIN") == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT") == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_SCAN") == PackageManager.PERMISSION_GRANTED
);
}

return (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED);
return hasPermission;
}

private void requestPermission() {
@ReactMethod
public void requestPermission() {

final Activity activity = getCurrentActivity();

String[] permissions = { Manifest.permission.RECORD_AUDIO, Manifest.permission.ACCESS_FINE_LOCATION };
ActivityCompat.requestPermissions(activity, permissions, REQUEST_PERMISSIONS);
int currentVer = android.os.Build.VERSION.SDK_INT;

ArrayList<String> permissions = new ArrayList<String>();

permissions.add(Manifest.permission.RECORD_AUDIO);
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);

if (currentVer >= 31) {
// there doesn't seem to be a constant for this (i.e. Manifest.permission.BLUETOOTH_CONNECT) -- kept getting compiler errors
// but using the permission string directly seems to work
permissions.add("android.permission.BLUETOOTH");
permissions.add("android.permission.BLUETOOTH_ADMIN");
permissions.add("android.permission.BLUETOOTH_CONNECT");
permissions.add("android.permission.BLUETOOTH_SCAN");
}

String[] array = permissions.toArray(new String[permissions.size()]);

ActivityCompat.requestPermissions(activity, array, REQUEST_PERMISSIONS);
}
}

0 comments on commit 8191e90

Please sign in to comment.