Advertisement

header ads

Android Studio Tutorials - How to get WiFi info of nearby wireless networks without connecting!


Think of a situation that you wanted to get nearby WiFi network details as well as the signal strength without connecting to any WiFi network. Can we do that? yes of course, we can easily make an Android application that scan through all the wireless networks nearby and give us the information of the wireless networks that has the strongest signal strength. And also by this method we can find the most close WiFi network.



So first open up the Android studio and then create a new project. Give all the details necessary and choose Empty Activity. After creating the project you can start coding the application.






At first we need to modify the code of ativity_main.xml file. So modify the code as below.

<TextView 
    android:id="@+id/info"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" />
  
After that you need to add the following code to MainActivity.java, inside the onCreate method

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.widget.TextView; 

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView info = (TextView)findViewById(R.id.info);

    WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    wifi.startScan();

    ScanResult result = wifi.getScanResults().get(0);
    String ssid = result.SSID;
    String bssid = result.BSSID;
    int rssi = result.level;
    String rssiString = String.valueOf(rssi);
    info.setText("Network Name:\t" + ssid + "\nMac Address:\t"  
                 + bssid + "\nSignal Strength:\t" + rssiString);


}

Then you need to make some changes in the AndroidManifest.xml . Add the following codes before the <application> </application> tabs.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

So what we do here is, we scan through all the nearby wifi networks and take their names that is SSID and their mac address (BSSID) and the signal strength (Level) and then output then to the TextView.

So after coding you can connect an Android device in debugging mode and deploy your code to that device and test your application.

Important : Use an Android Device less that version 6.0. because due to the security reasons if you need to test your application in an android device runs on android version 6.0 you should extends the code with bunch of other codes. I'll talk about them in another tutorial. 

If you have any questions feel free to post them in the comment section. I'll try to answer them all.

Post a Comment

0 Comments