How do I access Android Libraries in Unity C# so the app shows Wifi SSIDs on ML2 device?

Give us as much detail as possible regarding the issue you're experiencing.

Unity Editor version: 2022.2.0b8:
ML2 OS version: September 2022 B3E.220818.12-R.085:
MLSDK version: 0.53.3:
Host OS: Windows 11: (Windows/MacOS)

Error messages from logs (syntax-highlighting is supported via Markdown): No Errors
We would like to have access to restricted Android permissions.

Our ultimate goal is to list the Wifi SSID's that are available and their strength on the ML2.

In the Unity Editory, we made a panel with text that is visible when we build to the device. We had to add a "ManagedWifi" dll (built on top of NativeWifi) in the same folder as our ManagedWifi C# script, and on "playing" we can see the various SSID's available as well as their strengths (NIWC-Guest - 93, BEMR - 90, etc.).

However, no change can be seen in the ML2 device (it still states the stub "Testing Wifi").

We tried to change the manifest manually, but no changes are seen in the Player Settings. Here are our requests:

  1. How do we add a restricted package to the manifest (Specifically: android.permissions.Access_wifi_state)
  2. We put the package in the manifest, but it doesn't show up in the Project Settings => Magic leap => Manifest Settings => Permissions. How do we get our changes to show there?
  3. What is the process for accessing the wifi state library in Unity C#?
  4. Is there a Lumin-specific route for accessing the library in Unity C#?

Can you address those questions, OR (even better) provide a better solution as to how to continuously access wifi SSIDs that on apps that are built on the Magic Leap?

Thanks, V/r

1 Like

Hi @Clint,

Thank you for your question! Let me know if the following answers unblock your project:

  1. Unity - Manual: Override the Android App Manifest - you'll need to override the app manifest in unity settings and add yours to this. You can see the example of us doing so in our Unity SDK Examples project - Sign in ・ Cloudflare Access
  2. Project Settings > Magic Leap > Manifest Settings only shows magic leap specific permissions only. Its simply an optional utility to help devs know what ML-specific permissions exist (i.e. the permissions required by ML C-APIs) and to add them to the custom manifest easily. Our utility uses the same route as the link i posted above i.e. modify the custom manifest of the project, the only difference being that it does so programatically.. Users are still free to update the custom manifest with their own stuff, and the "Manifest Settings" utility will simply add onto that.
  3. Unity offers the AndroidJNI api, which is the c# equivalent of the c/c++ jni api - Unity - Scripting API: AndroidJNI which can be used to invoke java code from within c#.. so you have 2 options of achieving what you want - either write the C# jni equivalent of java code that would be used for accessing the wifi manager OR create a .aar plugin of that java code, import it into unity and write the C# jni equivalent to invoke the simpler functions of your aar plugin than all the complicated stuff that you might otherwise have to do in option 1. Option 1 vs 2 really depend on the context of your specific app
  4. No, its all pure Android now :slight_smile:
1 Like

Thanks @kvlasova . I was using the AAR route for a while, but I finally got my code to work seemingly without it. First, I set tons of permissions:
uses-permission android:name="android.permission.CHANGE_WIFI_STATE"
uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.ACCESS_WIFI_STATE"

And use this code:
private void setWifiInfo()
{
try
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"))
{
using (var wifiManager = activity.Call<AndroidJavaObject("getSystemService", "wifi"))
{
AndroidJavaObject wifiInfoJava = wifiManager.Call("getConnectionInfo");
string result = wifiInfoJava.Call("getBSSID");
...

... to finally get the access to WifiInfo public methods, such as getBSSID, getSSID, get Rssi, getIpAddress, getMacAddress. Several do not work in its current state, such as getNetworkID, getWifiStandard, and go to the catch.

Additionally, when calling SSID, it only shows [unknown ssid]. I would prefer it to say the real SSID, but at least knowing the Call is going through is a good first step. If anyone familiar, and see any solution to the SSID, as well as the other methods going to the catch?

1 Like