.NET MAUI / Android how to get user to select Wi-Fi Network

230 Views Asked by At

Yes, this question has been asked before (here, here, and thousands of other places), but all of these questions assume a lot of prior knowledge that I don't have, such as what/where file/folder/location/platform the code given is supposed to live, what the very first step is, etc. They all jump in with code. It's like watching a movie where you miss the first half -- you don't know what's going on for lack of context.

What I'm attempting to do
This is a common pattern when dealing with Android apps that need to access devices like dash-cams:

  • in the app, the user is asked to put the device into hot spot mode
  • the user puts the device itself into hot spot mode, at which point the device creates an SSID in a Wi-Fi network (WLAN).
  • in the app, the user presses a "connect" button (or it might say "choose network" or whatever); the button simply opens the Settings >> Connections >> WiFi page in the Android OS, where the user can select the SSID of a network.
  • the user chooses the correct network (in my case, the user will know the name of the network and PW)
  • if the connection is good, then the app continues on to do whatever it needs to do; in this case, the connection will be the "connected no internet" type, which I believe is the ConstrainedInternet connection

As far as I can tell, .NET MAUI doesn't have a direct API that can open the "choose network" page in Android OS, so I have to use Android code to do it. The Maui docs sort of explains how to do this, but not well enough that I understand, in a granular, step-by-step way, how to do it.
I think I'm supposed to create an interface for the method that I want to use, then call that interface in code that I put into the "Android" platform folder. I may need to use this method that's in the Maui.ApplicationModel IAppInfo.ShowSettingsUI(), but I can't find directions on exactly, step-by-step how to do this. I also could be totally wrong.

If anyone could tell me how to get to the point, in a .NET MAUI app for Android, where the user can choose a specific Wi-Fi network, and maybe explain it like you'd explain it to a Golden Retriever, I would truly appreciate it.

OR: maybe I'm missing some documentation? Maybe there's a great explanation out there, and I just haven't seen it.

EDIT: found documentation
Here and here are the docs that explain how to put Android code (or IOS or Windows or whatever) into your .NET MAUI project.

There are two workflows to use non-.NET code. In both methods, you create an interface for the "thing" that you want to do (have the user choose a network, utilize the GPS, determine the orientation of the phone, etc.), then you either use "conditional" statements for the implementation of the interface #if ANDROID // code #endif or you put the implementation into the folder that corresponds with the platform.

1

There are 1 best solutions below

7
Guangyu Bai - MSFT On

Maui is a cross-platform UI framework. You need to use the different platform Api to control the Wi-Fi by using Conditional compilation.

        private void Button_Clicked(object sender, EventArgs e)
        {
#if ANDROID
            var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);

            var formattedSsid = "Ssid";
            var formattedPassword = "password";

            var wifiConfig = new WifiConfiguration
            {
                Ssid = formattedSsid,
                PreSharedKey = formattedPassword
            };

            var addNetwork = wifiManager.AddNetwork(wifiConfig);
            var network = wifiManager.ConfiguredNetworks
                 .FirstOrDefault(n => n.Ssid == formattedSsid);

            if (network == null)
            {
                Console.WriteLine($"Cannot connect to network: {formattedSsid}");
                return;
            }

#endif
        }

Here is the information you can use.

  1. WifiManager for Android platform
  2. NEHotspotConfiguration for IOS platform
  3. Invoke platform code. How to invoke the native apis on MAUI.