0 votes
by (140 points)
recategorized by

Dear community, I hope somebody can help me here.

I'm developing an UnityApp for this situation:

There are two WiFi's ("1" and "2").
When my app is starting, I want to check if "1" or "2" is available and connect to the one, which is there.
If both are available, I want to check which one has the stronger signal or is closer to the headset and connect to this one.

I can use the AndroidHelper (connectWifi) to make the connection, but how do I do the scan?
Will I get a return code if I try to connect to a non existing Wifi, so I can just try each connection?
How to determine, which connection has the stronger signal?

Thank you very much!


2 Answers

0 votes
by (55.5k points)

It is recommended to use Android API to scan WiFi and obtain WiFi strength. To call the methods in Unity, you will need to make a Jar package and call Java method in C#.

by (140 points)
edited by

Hi DevSupport!

Thank you very much. I dived in Android API and getRssi() seems to be the value I want to check for signal strength.

I created an easy C# Unity-Script and Unity XR project to read out SSID but all I get is "unknown SSID" which means I'm missing permission.

The script is:
var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
var wifiManager = activity.Call("getSystemService", "wifi");
SSID = wifiManager.Call("getConnectionInfo").Call("getSSID");

I tried with custom AndroidManifest.xml including:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESSNETWORKSTATE"
uses-permission android:name="android.permission.ACCESSFINELOCATION"
uses-permission android:name="android.permission.ACCESSWIFISTATE"
uses-permission android:name="android.permission.ACCESSFINELOCATION"
uses-permission android:name="android.permission.ACCESSCOARSELOCATION"

But no success.

What am I missing?

Thanks again!

by (55.5k points)

Hello, you can try the following code:

  AndroidJavaObject activityContext = new AndroidJavaObject("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");

  AndroidJavaObject cm = activityContext.Call<AndroidJavaObject>("getSystemService", "connectivity");

  string ssid = cm.Call<AndroidJavaObject>("getActiveNetworkInfo").Call<string>("getExtraInfo");

  Debug.Log("Wifi Get SSID is" + ssid);

We tested that this method can obtain SSID information.

enter image description here

by (100 points)
edited by

Hi I am not much familiar with coding. I want to get a list of available wifi and their rssi and show it in my unity application. I managed to learn about adding an android plugin. But I'm stuck with how to return my list of available networks to unity.

I have done my coding in android studio so that the function returns a string array of the available networks' SSID. Could someone please provide me with some help so I can know how to get this string array to unity.

The below code snippets are from android studio and unity respectively.

enter image description here

    AndroidJavaClass ssidManager = new AndroidJavaClass ("com.example.wifirssitest.ssidmanager");

    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
    AndroidJavaObject context  = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    context.Call ("runOnUiThread", new AndroidJavaRunnable(() => {
        //call the static method 
        ssidManager.CallStatic ("getSSID",  context  );
0 votes
by (1.7k points)

For scaning wifi, you can refer to here:https://developer.android.com/guide/topics/connectivity/wifi-scan

And the method to get the wifi list can refer to this Android code:https://gist.github.com/ahmed-BH/566bd22169c097bd6e2851e10431c6f4