Reputation: 762
I am building an app in XAMARIN
, that has to connect to a custom device via wifi
and that device does not have internet connection. So my app always wants to work on mobile data and there for never communicating with the device. Is there anyway to force the app to use only WIFI
and never mobile data? I guess I'm not the only one with that problem as I've seen a few posts about this, but none in xamarin
specific.
EDIT: I found that if I let the application run for about 50 seconds, then it will use the wifi insted of mobile data. Is this due to some timeout? If so, then can I shorten the timeout?
Upvotes: 3
Views: 1931
Reputation: 762
After a lot of sweating I have found the solution. Make a class with this code:
using Android.Content;
using Android.Net;
namespace Project.Communication
{
class ForceNetworkType
{
public static Context _context = Android.App.Application.Context;
/// <summary>
/// Forces the wifi over cellular
/// </summary>
public static void ForceWifiOverCellular()
{
ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Wifi);
var callback = new ConnectivityManager.NetworkCallback();
connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
}
/// <summary>
/// Forces the cellular over wifi.
/// </summary>
public static void ForceCellularOverWifi()
{
ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Cellular);
connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
}
}
/// <summary>
/// Custom network available call back.
/// </summary>
public class CustomNetworkAvailableCallBack : ConnectivityManager.NetworkCallback
{
public static Context _context = Android.App.Application.Context;
ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
public override void OnAvailable(Network network)
{
//ConnectivityManager.SetProcessDefaultNetwork(network); //deprecated (but works even in Android P)
connection_manager.BindProcessToNetwork(network); //this works in Android P
}
}
}
And Then you just use where you need to force certain Network:
ForceNetworkType.ForceWifiOverCellular();
to force the wifi over mobile data.
Upvotes: 2
Reputation: 739
Using Xamarin.Essentials. Like this:
>
var profiles = Connectivity.ConnectionProfiles;
if (profiles.Contains(ConnectionProfile.WiFi))
{
// Active Wi-Fi connection.
}
You can change this check to block users that without a wifi connection.
reference:
Upvotes: 0
Reputation: 967
I believe the best you can actually do is set the preference of what you want the device to use. With both OS, it will prefer a Wifi connection over a cellular connection unless it times out on the request.
However you can use the Reachability class in iOS to say something like:
//PseudoCode
if(ReachabilityStatus == ReachableViaWifi)
{
//Only do things when WIFI is on
}
else
{
//Only do things when WIFI is not on
}
Within Android, you can do the following with ConnectivityManager:
//PseudoCode again
ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(Context.ConnectivityService);
NetworkInfo wifi = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
if (wifi.IsConnected)
{
//Pretty fly for a wifi
}
else
{
//No wifi here!
}
Upvotes: -1