Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force XAMARIN app to use WIFI only

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?

like image 888
sesmajster Avatar asked Jan 21 '26 09:01

sesmajster


1 Answers

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.

like image 143
sesmajster Avatar answered Jan 23 '26 08:01

sesmajster