Android activity onRequestPermissionsResult callback not being invoked when activity launched from Unity

34 Views Asked by At

In Unity, we are calling

`

private const string JAVA_CLASS_DEF = "com.{class removed for Stack Overflow}";
private const string UNITY_ACTIVITY_DEF = "com.unity3d.player.UnityPlayer"; AndroidJavaObject activity = new AndroidJavaClass(UNITY_ACTIVITY_DEF)
           .GetStatic<AndroidJavaObject>("currentActivity"); // unity activity

    androidClass = new AndroidJavaClass(JAVA_CLASS_DEF);
    androidClass .CallStatic("initialise", activity, callbackInstance, 3000l, true);`

Which successfully calls the following function on our Android Library AAR plugin.

public class {our class name} extends Activity {

public void initialise(Activity newActivity, final EirBTCallbacks newCallback, long newTimeout, boolean logOutput) {
        activity = newActivity;
        callback = newCallback;
        connectionTimeout = newTimeout;

    java.util.logging.Logger.getLogger("{our plugin name}").log(java.util.logging.Level.INFO, "Invoking Launch");
    requestBluetoothPermissions();
}

private static void requestBluetoothPermissions() {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
        java.util.logging.Logger.getLogger("EIR_BT").log(Level.WARNING, "Permissions not granted. Requesting...");
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT},
                REQUEST_ENABLE_BT);
    } else {
        // permissions already granted, proceed with initialization
        java.util.logging.Logger.getLogger("EIR_BT").log(java.util.logging.Level.INFO, "Permissions granted");
        initializeBluetooth();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    java.util.logging.Logger.getLogger("{our plugin name}").log(java.util.logging.Level.INFO, "On Permissions Result");
    switch (requestCode) {
        case PERMISSION_REQUEST_BLUETOOTH_CONNECT:
        case PERMISSION_REQUEST_BLUETOOTH_SCAN:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                java.util.logging.Logger.getLogger("{our plugin name}").log(java.util.logging.Level.INFO, "Permissions good" + grantResults[0]);
                initializeBluetooth();
            } else {
                java.util.logging.Logger.getLogger("{our plugin name}").log(java.util.logging.Level.INFO, "Permissions bad " + grantResults[0]);
            }
            break;
    }
}// the rest of our code...}`

This all functions as expected, however upon accepting (or denying) the permissions prompt, the onRequestPermissionsResult callback is not invoked.

We are unsure why this is happening, AI has been as useful as a chocolate teapot and we cannot figure this out. We suspected it was perhaps something to do with the activity being the Unity activity, but even when creating and running the bluetooth process on a new activity, we still did not get the callback. This is using AndroidX, and gradle 7.1.2 as per the Unity documentation.

In all tests, the permissions prompt has shown as expected, but upon acceptance or denial, no callback was invoked, essentially blocking the thread and resulting in the need for app relaunch (in which case the app could proceed, as permissions had at that point been granted).

0

There are 0 best solutions below