0

我正在使用 react-native-permissions 包在我的应用程序中请求一些权限。当我启动它时,应用程序显示模式以接受请求的权限,然后我调用一个需要接受 ACCESS_FINE_LOCATION 权限的方法。因此,当代码到达该方法时,我收到以下错误:

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)

应用程序.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;
                }
            }
        });
    }
}

我有点困惑,因为我已经在请求他们了。所以我想知道是否有任何方法可以让我的 android 本机模块知道权限已经被授予。提前致谢 :)

4

0 回答 0