Connect to wifi programatically android sdk >=23(marshmallow)

Hi! Trying for days to connect programmatically to wifi device that has no internet but i want to exchange data!
firstly I used this code
if (netId != -1)

        {
            IList<WifiConfiguration> myWifi = wifiManager.ConfiguredNetworks;

            foreach (var i in myWifi)
            {
                if (i.Ssid != null && i.Ssid.Equals(formattedSsid))
                {                     
                        wifiManager.Disconnect();
                        wifiManager.EnableNetwork(i.NetworkId, true);
                        wifiManager.SetWifiEnabled(true);
                        wifiManager.Reconnect();                                              

                        break;
                    }                
                }

It works perfect for android under 21

then I tried to force connectivity with wifi using ConnectivityManager.NetworkCallback , here is my code

if ((Build.VERSION.SdkInt >= BuildVersionCodes.M) ||
((Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) &&
!(Build.VERSION.SdkInt >= BuildVersionCodes.M)))
{
ConnectivityManager connection_manager =
(ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);

                            NetworkRequest.Builder request = new NetworkRequest.Builder();
                            request.AddTransportType(TransportType.Wifi);~~~~                              
                            connection_manager.RequestNetwork(request.Build(), new MyCreateCallBack());
                        }

public class MyCreateCallBack : ConnectivityManager.NetworkCallback
{
public static Context _context = Android.App.Application.Context;

    ConnectivityManager connection_manager =
        (ConnectivityManager) _context.GetSystemService(Context.ConnectivityService);


    public override void OnAvailable(Network network)
    {
        var wifiManager = _context?.ApplicationContext?.GetSystemService(Context.WifiService) as WifiManager;
        WifiInfo wifiInfo = wifiManager.ConnectionInfo;
        var mSsid = GetNetworkSsid(_context);        

        if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
        {             

            if (mSsid.Equals(Helpers.Settings.Barista))
            {
                connection_manager.BindProcessToNetwork(network);
            }
        }
        else
        {
            ConnectivityManager.SetProcessDefaultNetwork(null);
            if (mSsid.Equals(Helpers.Settings.Barista))
            {
                ConnectivityManager.SetProcessDefaultNetwork(network);
            }

        }

        connection_manager.UnregisterNetworkCallback(this);

    }

    private string GetNetworkSsid(Context context)
    {
        var wifiManager = _context?.ApplicationContext?.GetSystemService(Context.WifiService) as WifiManager;
        WifiInfo wifiInfo = wifiManager.ConnectionInfo;
        if (wifiInfo?.SupplicantState == SupplicantState.Completed)
        {
            return wifiInfo.SSID;
        }

        return "";
    }
}

where mSsid the ssid i want to connect

At first it connects but after a second disconnect and the reason that says is --> network_selection_disabled_no_internet

But when I choose from phone to connect to this device i get after some seconds the msg Box:
This wi-fi has no access to the internet[continue /use data]

Sorry for all this.....but my question is how to force connection to this device ??? without my app crash and without have the user to choose it from wifi selection?
Thank you

Sign In or Register to comment.