If this helps anyone, it was my final re-work... (thanks Brian and John)

// using Android.Net

  try
  {
        ConnectivityManager cm = (ConnectivityManager)GetSystemService(
Android.Content.Context.ConnectivityService );
        NetworkInfo[] info = cm.GetAllNetworkInfo();
        StringBuilder sb = new StringBuilder();
        foreach( var n in info )
        {
                if( sb.Length > 0 )
                        sb.Append( "\n" );
                sb.Append( n.TypeName + "\n" );
                sb.Append( "IsAvailable: " + n.IsAvailable.ToString() + "\n"
);
                sb.Append( "IsConnectedOrConnecting: " +
n.IsConnectedOrConnecting.ToString() + "\n" );
                sb.Append( "IsConnected: " + n.IsConnected.ToString() + "\n"
);
                sb.Append( "ExtraInfo: " + n.ExtraInfo + "\n" );
        }
        Utl.ShowMessage( sb.ToString() );
  }
  catch( Exception ex )
  {
        Utl.ShowException( "Net", ex );                         
  }


Steve Sharrock
Architecture/Design/Programming
www.sharkcode.com

-----Original Message-----
From: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of John Murray
Sent: Friday, May 13, 2011 12:58 AM
To: 'Discussions related to Mono for Android'
Subject: Re: [mono-android] checking wifi or mobile connection

Many thanks Brian - worked a treat
Just had to change Android.Content.Context.WifiService for
Android.Content.Context.ConnectivityService
In my old code and it would have worked The wonders of programming API's :-)
Thanks again John Murray

-----Original Message-----
From: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Brian Long
Sent: 13 May 2011 01:06
To: monodroid@lists.ximian.com
Subject: Re: [mono-android] checking wifi or mobile connection

John

With the  first bit, you are requesting a WifiManager, and are casting it to
a ConnectivityManager - if you want one of those, specify
Android.Content.Context.ConnectivityService

The second one, I'm not sure why you'd get a typecast problem

Code as below I'd expect to work, however I can't test it thanks to being
unable to successfully require the ACCESS_WIFI_STATE and
ACCESS_NETWORK_STATE permissions (as per other message in this list).
Covers wifi and mobile in the first case, just wifi in the second.

private string GetNetStateDescription(bool mobile) {
    ConnectivityManager connectivityManager =
(ConnectivityManager)GetSystemService(Android.Content.Context.ConnectivitySe
rvice);
    //Requires ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE to avoid security
exception
    NetworkInfo networkInfo =
connectivityManager.GetNetworkInfo(mobile ?
Android.Net.ConnectivityType.Mobile :
Android.Net.ConnectivityType.Wifi);
    if (networkInfo.IsConnected)
        return "Connected";
    else if (networkInfo.IsConnectedOrConnecting)
        return "Connecting";
    else
        return "Disconnected";
}

private string GetWiFiStateDescription() {
    //Requires ACCESS_WIFI_STATE permission to avoid security exception
    WifiManager wifiManager =
(WifiManager)GetSystemService(Android.Content.Context.WifiService);
    switch (wifiManager.WifiState)
    {
        case WifiState.Disabled:
            return "Disabled";
        case WifiState.Disabling:
            return "Disabling";
        case WifiState.Enabled:
            return "Enabled";
        case WifiState.Enabling:
            return "Enabling";
        default:
            return "Unknown";
    }
}

- Brian

> I am sure it must be me but I cant get past this one
>
> I  am trying to check the wifi state
>
> I have lifted two bits of code from the net (each slightly different ) 
> but
both give an unhadled excpetion  to do with invalid cast exception
>
>
>
> Version 1
>
>
>
> ConnectivityManager connManager =
(ConnectivityManager)GetSystemService(Android.Content.Context.WifiService);
>
>                                 NetworkInfo mWifi =
connManager.GetNetworkInfo(Android.Net.ConnectivityType.Wifi);
>
>                                                                 if
(mWifi.IsConnectedOrConnecting)
>
> {
>
>    
                                                                           
// Do whatever
>
>
                                                                            
    Toast.MakeText(this,"wifif is connecting",ToastLength.Long).Show();
>
>                                                                 }
>
> Version 2
>
>
>
> SupplicantState supState;
>
>             var wifiManager =
(WifiManager)GetSystemService(Android.Content.Context.WifiService);
>
>             WifiInfo wifiInfo = wifiManager.ConnectionInfo;
>
>             supState = wifiInfo.SupplicantState;
>
>
>
>
>
> Both have the same system exception or more or less – referring to a 
> type
cast
>
>
>
> Yes I have enabled Access_Wifi in the manifest
>
>
>
> Any ideas anyone ?
>
> Also I want to the same for mobile connection – is the same approach
appropriate?
>
>
>
> Best regards
>
> John Murray
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Monodroid mailing list
> Monodroid@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/monodroid
>
>
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid


_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to