Dyskord
Dyskord

Reputation: 387

Unity3D how to add permissions to AndroidManifest.xml for android plugin

So I'm working on a Unity plugin for android and I've found a problem that seems poorly documented. I need to add the INTERNET, CHANGE_WIFI_STATE, ACCESS_WIFI_STATE, CHANGE_NETWORK_STATE, and ACCESS_NETWORK_STATE permissions to my project.

I added them to the AndroidManifest.xml file in my android plugin package, but I get an error saying that I need permissions. I believe that I need to add the permissions to a different AndroidManifest.xml in the Unity build. I'm not really sure how to get Unity to add them. (I was able to add the internet permission via player settings in build)

Unity Manual says that the only way to do this is to edit the Manifest file manually, but that seems like a bad way to go about it; I wouldn't want everyone who uses the plugin to have to manually edit their file. Also, I have a custom third party asset imported into this project and looking at Unity's AndroidManifest file, I see that the asset was able to add its permissions to the manifest file without me having to do anything, so I know that what I want to do is possible.

Also, I believe all of my permissions are not considered dangerous so I don't have to manually ask the user for permission, but if I did, I'm not sure how I would go about asking.

Here is some of the code for my project that might be of use:

The AndroidManifest.xml in my plugin package:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.unitywifidirectlibrary" >

    <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
    <uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="ANDROID.PERMISSION.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="ANDROID.PERMISSION.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="ANDROID.PERMISSION.CHANGE_WIFI_STATE"/>

    
</manifest>

The error that I get when I attempt to check if WiFi is enabled as a test:

java.lang.SecurityException WifiP2pService: Neither user 10194 nor current process has android.permission.ACCESS_WIFI_STATE.

The function that causes the error:

public static string DisplayWifiStatus()
    {
        if (!isSetup)
            setup();
        string result = "function was not called";
        if (Application.platform == RuntimePlatform.Android)
        {
            try
            {
                result = WDAndroid.Call<string>("displayWifiStatus");
            }
            catch (Exception e)
            {
                result = e.Message;
            }
        }
        else
        {
            print("(DisplayWifiStatus) This function is not supported on this platform");
        }
        return result;
    }

Does anyone have any experience with this problem? Any ideas for things I could try? Thanks!

Upvotes: 2

Views: 3473

Answers (1)

Dyskord
Dyskord

Reputation: 387

Figured it out. You can add an AndroidManifest.xml to assets -> plugins -> android and it will be merged with unity's. This is a bit inconvenient for an plugin, there's no good way to include it in the jar file or with a C# wrapper, especially if you have multiple android plugins that need to contribute to the file, but it got the job done for me.

Upvotes: 4

Related Questions