Get Ssid Of Connected Network As Well As My Ip Address

Posted on

Android instance code to become ssid in addition to IP address, using WifiInfo.

Tested on Samsung Milky Way A9, running Android 6.0.1.

Wifi enabled in addition to connected

Wifi disabled

MainActivity.java

package com.blogspot.android_er.androidwireless;  import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.format.Formatter; import android.widget.TextView;  world shape MainActivity extends AppCompatActivity {      TextView tvWifiEnabled, tvWifiState, tvWifiInfo, tvSSID,                 tvRssi, tvIP, tvFormattedIP1, tvFormattedIP2;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          tvWifiEnabled = (TextView)findViewById(R.id.WifiEnabled);         tvWifiState = (TextView)findViewById(R.id.WifiState);         tvWifiInfo = (TextView)findViewById(R.id.WifiInfo);         tvSSID = (TextView)findViewById(R.id.SSID);         tvRssi = (TextView)findViewById(R.id.Rssi);         tvIP = (TextView)findViewById(R.id.IP);         tvFormattedIP1 = (TextView)findViewById(R.id.FormattedIP1);         tvFormattedIP2 = (TextView)findViewById(R.id.FormattedIP2);          //To foreclose retentiveness leaks on devices prior to Android N,         //retrieve WifiManager amongst         //getApplicationContext().getSystemService(Context.WIFI_SERVICE),         //instead of getSystemService(Context.WIFI_SERVICE)         WifiManager wifiManager =                 (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);         tvWifiEnabled.setText("isWifiEnabled(): " + wifiManager.isWifiEnabled());         tvWifiState.setText(readtvWifiState(wifiManager));          WifiInfo wifiInfo = wifiManager.getConnectionInfo();         if(wifiInfo == null){             tvWifiInfo.setText("wifiInfo == cipher !!!");         }else{             tvWifiInfo.setText(wifiInfo.toString());             tvSSID.setText("SSID: " + wifiInfo.getSSID());             tvRssi.setText("Rssi: " + wifiInfo.getRssi() + " dBm");              int ipAddress = wifiInfo.getIpAddress();             /*             Formatter.formatIpAddress() was deprecated inwards API grade 12.             This method does non back upward IPv6 addresses.              */             String FormatedIpAddress = Formatter.formatIpAddress(ipAddress);             String FormatedIpAddress2 = String.format("%d.%d.%d.%d",                     (ipAddress & 0xff),                     (ipAddress >> 8 & 0xff),                     (ipAddress >> sixteen & 0xff),                     (ipAddress >> 24 & 0xff));             tvIP.setText("IP: " + wifiInfo.getIpAddress());             tvFormattedIP1.setText("" + FormatedIpAddress);             tvFormattedIP2.setText("" + FormatedIpAddress2);         }      }      // "android.permission.ACCESS_WIFI_STATE" is needed     someone String readtvWifiState(WifiManager wm){         String trial = "";         switch (wm.getWifiState()){             instance WifiManager.WIFI_STATE_DISABLED:                 trial = "WIFI_STATE_DISABLED";                 break;             instance WifiManager.WIFI_STATE_DISABLING:                 trial = "WIFI_STATE_DISABLING";                 break;             instance WifiManager.WIFI_STATE_ENABLED:                 trial = "WIFI_STATE_ENABLED";                 break;             instance WifiManager.WIFI_STATE_ENABLING:                 trial = "WIFI_STATE_ENABLING";                 break;             instance WifiManager.WIFI_STATE_UNKNOWN:                 trial = "WIFI_STATE_UNKNOWN";                 break;             default:         }         render result;     } }  

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidwireless.MainActivity" >      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <TextView         android:id="@+id/WifiEnabled"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/WifiState"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <TextView         android:id="@+id/WifiInfo"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="italic"/>     <TextView         android:id="@+id/SSID"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"         android:textColor="#FF0000"/>     <TextView         android:id="@+id/Rssi"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textColor="#00FF00"/>     <TextView         android:id="@+id/comment1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"         android:text="get IP Address:"/>     <TextView         android:id="@+id/IP"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="italic"         android:textColor="#0000FF"/>     <TextView         android:id="@+id/FormattedIP1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="italic"         android:textColor="#0000FF"/>     <TextView         android:id="@+id/FormattedIP2"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="italic"         android:textColor="#0000FF"/>  </LinearLayout>  

uses-permission of “android.permission.ACCESS_WIFI_STATE” is needed inwards AndroidManifest.xml.

Remark on MAC:
Please lead maintain a await on the captured screen; the MAC address is 02:00:00:00:00:00.

Refer to Android 6.0 Changes – Access to Hardware Identifier:
To supply users amongst greater information protection, starting inwards this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi in addition to Bluetooth APIs. The WifiInfo.getMacAddress() in addition to the BluetoothAdapter.getAddress() methods at in ane trial render a constant value of 02:00:00:00:00:00.