Android Apps

Get Network Connection Name – Android App development

March 21, 2013 by Sarath DR in Android Apps, Featured with 0 Comments

Somebody asked the same question in my website. You can use the below code to retrieve the name of the network connection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
public class NativeAccess
{
   	private WebView mAppView;
        private DroidGap mGap;
 
        public NativeAccess(DroidGap gap, WebView view)
        {
            mAppView = view;
            mGap = gap;
        } 
 
public function getConnectionName()
     {
 
        String networkProvider = ""; 
 
        try{
 
ConnectivityManager cm = (ConnectivityManager)this.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
 
            NetworkInfo ni = cm.getActiveNetworkInfo();
 
 
            if( ni.isConnected() )
            {	
 
 
 
            	if( ni.getTypeName() == "mobile" )
            	{
            		networkProvider = ni.getExtraInfo(); 
            	}
            	else if( ni.getTypeName() == "WIFI"  )
               {
 
            		WifiManager wifiManager = (WifiManager) this.getContext().getSystemService(Context.WIFI_SERVICE);
                    WifiInfo connectionInfo = wifiManager.getConnectionInfo();
 
                    if (connectionInfo != null ) 
                    {
                    	networkProvider = connectionInfo.getSSID();
                    	Log.d("TAG_NETWORK", connectionInfo.getSSID() );
                    }
 
            	}
 
 
 
            	Log.d("TAG_NETWORK", ni.getExtraInfo() );
                Log.d("TAG_NETWORK", ni.getReason());
 
 
            }
 
        }
        catch( Exception e )
        {
        	Log.d("TAG_NETWORK", e.getMessage() );
        }
 
return networkProvider; 
 
}
 
}

If you want to access the below code function from an html page add native access to the class as below. Then call that function from javascript. This will be useful in Phonegap.

1
2
3
4
5
 
// Add this in onCrete function of the class which extends DroidGap 
 
NativeAccess cna = new NativeAccess(this, appView );    
appView.addJavascriptInterface(cna, "NativeAccess");

You can call the function from javascript as follows.

 
var connectionName = window.CustomNativeAccess.getConnectionName() ;

Tagged , ,

Share this to

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close