Reputation: 450
I am unable to connect to wifi in Android 10. The code I have works fine till Android 9, I have been trying to implement the same for Android 10 without any luck :(
private void RequestNetwork(string _ssid, string _passphrase)
{
var specifier = new WifiNetworkSpecifier.Builder()
.SetSsid(_ssid)
.SetWpa2Passphrase(_passphrase)
.Build();
var request = new NetworkRequest.Builder()
.AddTransportType(TransportType.Wifi) // we want WiFi
.RemoveCapability(NetCapability.Internet) // Internet not required
.SetNetworkSpecifier(specifier) // we want _our_ network
.Build();
NetworkCallback _callback = new NetworkCallback();
connection_manager.RequestNetwork(request, _callback);
}
private class NetworkCallback : ConnectivityManager.NetworkCallback
{
public Action NetworkAvailable { get; set; }
//bool IsBusy = false;
public override void OnAvailable(Network network)
{
base.OnAvailable(network);
connection_manager.BindProcessToNetwork(network);
}
public override void OnUnavailable()
{
base.OnUnavailable();
}
}
Upvotes: 0
Views: 2799
Reputation: 128
Here is the my code for connecting wifi but problem is after connecting wifi internet not worked.
If your internet problem is not occured then please share your code with us:
try
{
var connectivityManager = Android.App.Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
if (!wifiManager.IsWifiEnabled)
{
Forms.Context.StartActivity(new Intent(Android.Provider.Settings.ActionWifiSettings));
return false;
}
var callback = new NetworkCallback(connectivityManager)
{
NetworkAvailable = network =>
{
Console.WriteLine("Connected");
MessagingCenter.Send<string, Boolean>("ConnectModel", "connecttowifi", true);
},
NetworkUnavailable = () =>
{
Console.WriteLine("Not Connected");
MessagingCenter.Send<string, Boolean>("ConnectModel", "connecttowifi", false);
}
};
var specifier = new WifiNetworkSpecifier.Builder()
.SetSsid(ssid)
.SetWpa2Passphrase(password)
.Build();
var request = new NetworkRequest.Builder()
.AddTransportType(TransportType.Wifi)
//.AddCapability(NetCapability.Internet)
//.RemoveCapability(NetCapability.Internet)
.SetNetworkSpecifier(specifier)
.Build();
connectivityManager.RequestNetwork(request, callback);
return true;
}
catch (Exception e)
{
Console.Write(e.Message);
return false;
}
Upvotes: 0
Reputation: 24460
I see that you copied code from my blog post based on the comments. I guess you have tried running the sample App I provided on GitHub?
Anyways, first of all make sure you have added the following permissions in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Except for the missing permissions the code you provided should work and show you a system dialog looking like this:
Upvotes: 2