Search code examples
androidunity-game-engineandroid-manifestandroid-permissionsandroid-bluetooth

Unity Android wants BROADCAST_STICKY, BLUETOOTH, and MODIFY_AUDIO_SETTINGS for Microphone access


I have a problem where Unity is requiring too many permissions. Before, I was building with Unity 2017, and my app needed three android permissions. These are the only ones I think I need:

android.permission.RECORD_AUDIO
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE

Now, I've upgraded to 2019.2.21f1, and I discovered a few extra permissions were added without changing the code:

android.permission.MODIFY_AUDIO_SETTINGS
android.permission.BROADCAST_STICKY
android.permission.BLUETOOTH

I've gone through all my code, deleting bits of it, until I found the line that caused these permissions to appear in my merged manifest (I was checking the manifest-merger-release-report) -- it's a reference to the list of microphones: Microphone.devices. When I make a reference to that, the permissions appear.

I need to reference this string array because I need to call Microphone.Start somewhere in order to get audio input, but I don't care about bluetooth or broadcasting stickies. I don't want to use any of these new permissions. Does anyone know why using Microphone.devices would cause these permissions to appear?

In the build settings, my minimum API level is 16, and the target API level is highest installed.

I don't want to downgrade my Unity version again. I want to find a way to just require the RECORD_AUDIO permission, without the three new ones -- the only other option is to not use the Microphone, which will make my game less fun. Boo hoo.


Solution

  • Now i don't think this answer will be of much help but since you seem pretty desperate, as in out of options since this is a problem in unity's core system, could you not try to instead use a different microphone library than the one builtin to unity?

    For example i found this:

    NatDevice is a cross-platform media device API for iOS, Android, macOS, and Windows. NatDevice provides powerful abstractions for using hardware cameras and microphones through a concise .NET API.

    https://assetstore.unity.com/packages/tools/integration/natdevice-media-device-api-162053

    You could also roll your own with native android code, like this guy describes:

    https://support.frozenmountain.com/hc/en-us/community/posts/115000768894-Unity-Android-Audio-Capture-Provider

    You can do it using a Unity "plugin": https://docs.unity3d.com/Manual/PluginsForAndroid.html Their docs go into more detail, but the general idea is that you want to write the app component that uses the native library using Java, and then activate it from C#. This avoids a lot of expensive cross-calls between the two runtimes. Using this approach, you would extend RtcLocalMedia in Java along with the rest of your component code, and then activate that component from C#.

    Another option is to completely overwrite the manifest file to exclude the permissions you don't want (although you need to make sure it all works still, because unity could still depend on them)

    A good description is here:

    https://stackoverflow.com/a/40931309/4122889

    Hope this atleast gave you some ideas.