Let Android Module know that some permissions are already granted on React-native class

622 views Asked by At

I'm using react-native-permissions package to request some permissions in my app. When I start it , the app shows the modal to accept the requested permissions after that I call a method that requires ACCESS_FINE_LOCATION permission accepted. So when the code reaches that method I get the following error:

2020-12-18 11:51:47.687 22777-22882/com.panicrn E/unknown:ReactNative: CatalystInstanceImpl caught native exception
    java.lang.SecurityException: ACCESS_FINE_LOCATION not granted. Please call `Activity.requestPermissions(String[], int)` first.
        at io.flic.flic2libandroid.Flic2Manager.checkScanPermission(Flic2Manager.java:486)
        at io.flic.flic2libandroid.Flic2Manager.startScan(Flic2Manager.java:506)
        at com.panicrn.FlicScanner.scanButtons(FlicScanner.java:35)
        at java.lang.reflect.Method.invoke(Native Method)

App.js:

  componentDidMount() {
    result = Permission.checkPermission(PERMISSION_TYPE.location)
    console.log("Result: "+result)
    if (result){
      FlicScanner.initFlicInstance();
      console.log("Scanning buttons")
      FlicScanner.scanButtons();
    }

AppPermissions.js:

const PLATFORM_FINE_LOCATION_PERMISSIONS = {
    android:  PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
}

const REQUEST_PERMISSION_TYPE = {
    fineLocation: PLATFORM_FINE_LOCATION_PERMISSIONS
}

const PERMISSION_TYPE = {
    location: 'fineLocation'
}

class AppPermissions {

    checkPermission = async (type): Promise<boolean> => {
        const permissions = REQUEST_PERMISSION_TYPE[type][Platform.OS]
        console.log(permissions)
        if (!permissions) {
            return true
        }
        try {
            const result = await check(permissions)
            if (result === RESULTS.GRANTED) return true
            return this.requestPermission(permissions)
        } catch (error) {
            return false
        }

    }

    requestPermission = async (permissions): Promise<boolean> => {
        try{
            console.log("requestPermission result: "+(result === RESULTS.GRANTED))
            const result = await request(permissions)
            return result === RESULTS.GRANTED
        } catch (error) {
            return false
        }
    }

}

const Permission = new AppPermissions()
export { Permission, PERMISSION_TYPE }

FlicScanner.java:

package com.panicrn;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.facebook.react.PackageList;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import io.flic.flic2libandroid.Flic2Button;
import io.flic.flic2libandroid.Flic2ButtonListener;
import io.flic.flic2libandroid.Flic2Manager;
import io.flic.flic2libandroid.Flic2ScanCallback;

public class FlicScanner extends ReactContextBaseJavaModule  {

    Flic2Manager manager;

    FlicScanner(ReactApplicationContext context){
        super(context);
    }

    @NonNull
    @Override
    public String getName() {
        return "FlicScanner";
    }

    @ReactMethod
    public void scanButtons() {
        Flic2Manager.getInstance().startScan(new Flic2ScanCallback() {
            @Override
            public void onDiscoveredAlreadyPairedButton(Flic2Button button) {

            }

            @Override
            public void onDiscovered(String bdAddr) {

            }

            @Override
            public void onConnected() {

            }

            @Override
            public void onComplete(int result, int subCode, Flic2Button button) {
                if (result == Flic2ScanCallback.RESULT_SUCCESS){
                    Toast.makeText(getReactApplicationContext(),"Connected button with MAC : "+button.getUuid(),Toast.LENGTH_SHORT).show();
                    addButtonListener(button);
                }
            }
        });
    }

    @ReactMethod
    public void initFlicInstance(){
        ContextCompat.startForegroundService(getReactApplicationContext(),new Intent(getReactApplicationContext(), FlicService.class));
        manager = Flic2Manager.initAndGetInstance(getReactApplicationContext(),new Handler());
    }

    @ReactMethod
    public void flicToast(){
        Toast.makeText(getReactApplicationContext(),"Pressed from Java module",Toast.LENGTH_SHORT).show();
    }

    public void addButtonListener(Flic2Button button) {
        button.addListener(new Flic2ButtonListener(){
            @Override
            public void onButtonSingleOrDoubleClickOrHold(Flic2Button button, boolean wasQueued, boolean lastQueued, long timestamp, boolean isSingleClick, boolean isDoubleClick, boolean isHold) {

                if (isSingleClick){
                    Toast.makeText(getReactApplicationContext(),"Has hecho un click al botón",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isDoubleClick){
                    Toast.makeText(getReactApplicationContext(),"Has hecho doble click al botón",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isHold){
                    Toast.makeText(getReactApplicationContext(),"Has mantenido el botón",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }
}

I'm a bit confused because I'm already requesting them. So I would like to know if there's any way to let my android native module know that the permissions are already granted. Thanks in advance :)

0

There are 0 answers