Use Wifi network with no internet

DASTDAST Member ✭✭
edited January 2019 in Xamarin.Android

Hello,

i need to create app which needs to use wi-fi network which has no internet. I found possibility to do it using connectivitymanager and network callback, but I found code only in Java and I have really problem to implement in C# NetworkCallback with OnAvailable method inside.

connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
     @Override
     public void onAvailable(Network network) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Build.VERSION.RELEASE.equalsIgnoreCase("6.0")) {
               if (!Settings.System.canWrite(mActivity)) {
                      Intent goToSettings = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                      goToSettings.setData(Uri.parse("package:" + mActivity.getPackageName()));
                      mActivity.startActivity(goToSettings);
                }
             }

             connectivityManager.bindProcessToNetwork(null);

       if (mSsid.contains("my_iot_device-xxxxxxxxx")) {
            connectivityManager.bindProcessToNetwork(network);
       }
}

I know, it should be managed somehow using delegates and events in C#, but I am not really sure how. Do you have any ideas how to implements this NetworkCallback with method inside? Is there any other possibility how to accomplish this task?

Best Answer

Answers

  • DimChrisDimChris USMember ✭✭✭✭

    You can use network without internet connection without any problem. Example you can communicate with a database as well without internet connection. Which is the purpose you want to use network without internet?

  • DASTDAST Member ✭✭

    @DimChris said:
    You can use network without internet connection without any problem. Example you can communicate with a database as well without internet connection. Which is the purpose you want to use network without internet?

    Thank you for your answer. I am using wi-fi connection to exchange data with an instrument, instrument does not have an internet connection. On some phones, I can connect to wi-fi without internet connection without problem, on some phones, I have problem e.g. when the wi-fi password is changed and user needs to reconnect (wi-fi is not connected then or phone is then connected to another wi-fi which has internet connection), that's why I need to force using wi-fi without network connection. The java code should do exactly this - by using OnAvailable in NetworkCallback and then using bindProcessToNetwork should solve the problem. I just don't have idea how to implement this in C#.

    Do you have any tips for me how to implement this in xamarin.android?

  • DimChrisDimChris USMember ✭✭✭✭
    edited January 2019

    Look password has nothing to do with internet connection. If password will change then it's normal that you can't connect in this wifi. All wifi's have credentials. Like a password. Also just delete all other networks from your device and just keep this one network which you want always to connect. I think your problem doesn't have to do with java code. Just search into your phone settings.

  • DASTDAST Member ✭✭
    edited January 2019

    @DimChris said:
    Look password has nothing to do with internet connection. If password will change then it's normal that you can't connect in this wifi. All wifi's have credentials. Like a password. Also just delete all other networks from your device and just keep this one network which you want always to connect. I think your problem doesn't have to do with java code. Just search into your phone settings.

    Thanks again for your answer. I don't want to delete all user wi-fi's, this would not be very good user experience. Of course I understand that I need to delete certain wi-fi and add it again with new password. That's what I am doing. What I am saying, it's not working on every phone - that's why I need also force it from the code. And it is only happening when wi-fi has no internet connection.

    My problem - I am connected to wi-fi. During connection wi-fi password is changed, so app detects that mobile is not connected to wi-fi anymore. User sets new password and app should automatically connect to wi-fi which was set by user. On some phones this is not possible, because if wi-fi does not have internet connection, it connects to wrong one (or to any, if another wi-fi is not present in the system).

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭

    @DAST Here is sample guide to access following connectivity:
    ACCESS_FINE_LOCATION
    ACCESS_NETWORK_STATE
    ACCESS_WIFI_STATE

    https://www.c-sharpcorner.com/article/how-to-check-networkinfo-in-xamarin-android-app-using-visual-studio-2015/

  • DASTDAST Member ✭✭
    edited January 2019

    @SHIVANKUCHAUHAN said:
    @DAST Here is sample guide to access following connectivity:
    ACCESS_FINE_LOCATION
    ACCESS_NETWORK_STATE
    ACCESS_WIFI_STATE

    https://www.c-sharpcorner.com/article/how-to-check-networkinfo-in-xamarin-android-app-using-visual-studio-2015/

    Thank you for your answer. This is already working in my case. I need to force app to use wi-fi network with no internet connection.

    I already found solution in Java (link below or code snippet in my question). I just need a hint, how to reimplement used Java callback to C# (or if there is another possibility how to do it in Xamarin.Android).

    https://stackoverflow.com/questions/42329775/using-a-wifi-without-internet-connection?noredirect=1&lq=1

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭

    @DAST OnAvailable method is available !

    Check the documentation, https://developer.xamarin.com/api/type/Android.Net.ConnectivityManager+NetworkCallback/

  • DASTDAST Member ✭✭

    Thank you for your answer. I was trying to implement OnAvailable in a same way as it is in Java, but I don't have any idea, how to implement code below. Problem is that the method is declared in the body of networkcallback itself.

    connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) {} }

    Do you have any idea or hint, how to implement this in C# (Xamarin.Android)?

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭
    @DAST Create the object of connectivityManager like below, and you should be able to achieve, what you want to do.
    

    Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager) GetSystemService(ConnectivityService);

  • DASTDAST Member ✭✭

    @DimChris said:
    If i understand what you want to do, i found a solution

    void ConnectToNetwork()
        {
            string networkSSID = "yournetworkname";
            string networkPass = "yourcode";
    
            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.Ssid = string.Format("\"{0}\"", networkSSID);
            wifiConfig.PreSharedKey = string.Format("\"{0}\"", networkPass);
    
            WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Android.Content.Context.WifiService);
    
            // Use ID
            int netId = wifiManager.AddNetwork(wifiConfig);
            wifiManager.Disconnect();
            wifiManager.EnableNetwork(netId, true);
            wifiManager.Reconnect();
        }
    

    Thank you for your answer. I was using similar code before, but I wouldn't use disconnect, because in my case usually the wifi was already disconnected. But it seems it was not enough and I need to use wifiManager.Disconnect() every time. Your answer is working, thank you again.

  • maira_mairamaira_maira Member ✭✭

    @Dast i have exactly the same problem did u find any solution?
    It would be very helpful. thank you

  • DASTDAST Member ✭✭
    edited January 17

    @maira_maira said:
    @Dast i have exactly the same problem did u find any solution?
    It would be very helpful. thank you

    Hello, I used the code @DimChris posted here and everything worked as expected. In my case the disconnecting of the wifi manager was the problem I believe.

  • maira_mairamaira_maira Member ✭✭

    @Dast , thanks for ur quick response, but I already use that code and I can't find a way to solve it! The phone that I use disable the connection cause the wifi has no internet! the code that u suggested at first is to force the device to connect to wifi without internet :/

  • DimChrisDimChris USMember ✭✭✭✭

    What brand name is the device? Xaomi?

  • DimChrisDimChris USMember ✭✭✭✭
    edited January 17

    Try to create a Hot spot from another device, with same Network Name and password, also give internet on this network.
    Then disable this network and open the other one without internet. It is possible to stay connected with this method.

  • maira_mairamaira_maira Member ✭✭

    @DimChris yes it is xaomi! I will try it! Thanks a lot :)

Sign In or Register to comment.