WiFi Setup

I would like to configure multiple devices to connect to the same Wi-Fi network without having to type the password for each one (and ideally, without needing that wifi network present at the time of device setup). I know that I can do this with an MDM but paying a monthly fee is not worth it for this service alone.

Is there a way to setup a ML2 to connect to a known wifi network without using an MDM or typing a password manually?

Edit to add: I've made a QR code, and if the device is in range of my network, i can use the QR code instead of typing manually, but it would be great if I could also configure with a QR code for a network that's not present.

I sadly don't have a solution for you, but here is what i tried already:
You can add the Wifi using WifiNetworkSuggestion, but there is no notification for the user to confirm the connection so it has no real effect.

You can connect to the wifi by creating a NetworkRequest, which works in that the user gets a popup and the ML2 connects while the app is open, however i had no sucess in sending/recieving anything on the network/internet.

Code For NetworkRequest
private static ConnectivityManager connectivityManager = null;
...
{
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
        builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        WifiNetworkSpecifier wifiNetworkSpecifier = getWifiNetworkSpecifier(ssid, password);
        builder.setNetworkSpecifier(wifiNetworkSpecifier);
        NetworkRequest networkRequest = builder.build();

        RegisterCallback();

        connectivityManager.requestNetwork(networkRequest, callback);
}
  private static void RegisterCallback() {
        callback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                connectivityManager.bindProcessToNetwork(network);
                Log.d("NetworkManager", "WiFi connected");
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                Log.d("NetworkManager", "Wifi not Available: ");
                connectivityManager.bindProcessToNetwork(null);
            }
        };
    }
1 Like

I think the API might be specific to using Wifi Direct and not the network directly. I created a small script a while back that I vaguely remember working.

I found it helpful to test using an Android 10, x86_64 virtual device without google play services in Android Studio to test.


Make sure to specify the following permissions in your apps manifest and set the API level to 29

  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.INTERNET" />

Put the WifiConnector.kt script into your project's plugins folder and put the WifiManager.cs script on a game object in your scene called WifiManager

Wifi Connection Scripts.zip (3.8 KB)