I need to connect a smartphone to specific WiFi network that is created by other device. I'm using Xamarin.Forms to develop my app. The goal is to connect the smartphone automatically, that is, programmatically. I just need to do Android development.
This is the connection code:
public class WifiConnector : IWifiConnector
{
public void ConnectToWifi(string ssid, string password)
{
try
{
WifiNetworkSpecifier.Builder wifiNetworkSpecifierBuilder = new WifiNetworkSpecifier.Builder();
wifiNetworkSpecifierBuilder.SetSsid(ssid);
wifiNetworkSpecifierBuilder.SetWpa2Passphrase(password);
WifiNetworkSpecifier wifiNetworkSpecifier = wifiNetworkSpecifierBuilder.Build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.AddTransportType(TransportType.Wifi);
networkRequestBuilder.AddCapability(NetCapability.Internet);
networkRequestBuilder.SetNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.Build();
ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
connectivityManager.RequestNetwork(networkRequest, new NetworkCallback());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
class NetworkCallback : ConnectivityManager.NetworkCallback
{
public override void OnAvailable(Network network)
{
base.OnAvailable(network);
Log.Debug("WifiConnector", "OnAvailable(): " + network.ToString());
}
}
}
I don't have any compilation errors, but at runtime I get this error:
Didn't find class "android.net.wifi.WifiNetworkSpecifier$Builder" on path: DexPathList[[zip file "/data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/lib/arm64, /data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
at Java.Interop.JniEnvironment+Types.FindClass (System.String classname) [0x0015a] in <00c315a988634383b446eff646084784>:0
at Java.Interop.JniType..ctor (System.String classname) [0x00006] in <00c315a988634383b446eff646084784>:0
at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname) [0x00018] in <00c315a988634383b446eff646084784>:0
at Java.Interop.JniPeerMembers.get_JniPeerType () [0x0000c] in <00c315a988634383b446eff646084784>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.get_JniPeerType () [0x00014] in <00c315a988634383b446eff646084784>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.StartCreateInstance (System.String constructorSignature, System.Type declaringType, Java.Interop.JniArgumentValue* parameters) [0x0003f] in <00c315a988634383b446eff646084784>:0
at Android.Net.Wifi.WifiNetworkSpecifier+Builder..ctor () [0x0001f] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.Net.Wifi.WifiNetworkSpecifier.cs:82
at ConnectToWifi.Droid.Services.WifiConnector.ConnectToWifi (System.String ssid, System.String password) [0x00002] in C:\DATA\Code tests\Xamarin\ConnectToWifi\ConnectToWifi\ConnectToWifi.Android\Services\WifiConnector.cs:27
--- End of managed Java.Lang.ClassNotFoundException stack trace ---
java.lang.ClassNotFoundException: Didn't find class "android.net.wifi.WifiNetworkSpecifier$Builder" on path: DexPathList[[zip file "/data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/lib/arm64, /data/app/com.companyname.connecttowifi-i0OpZvWjDZqiRX01MQJcMA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at crc64ee486da937c010f4.ButtonRenderer.n_onClick(Native Method)
at crc64ee486da937c010f4.ButtonRenderer.onClick(ButtonRenderer.java:104)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14275)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
I added this permissions in AndroidManifest.xml file:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Details about project, libraries and development environment:
- Microsoft Visual Studio Professional 2019 Version 16.11.12
- Xamarin.Forms 5.0.0.2012
- Android target version: Android 11 API 30
- Smartphone Android version: Android 9 API 28
Why is this error happening?