Application overview:
This application is to get the number of available wifi by scanning and we know the broadcast receiver class actually used for getting alert. As example, when your battery is low or when you get an SMS alert and here we will use broadcast receiver to get the available number of WiFi and the number of connection change when we are going to access the WiFi.
Required software and tools for this application are:
- Eclipse IDE, you can use Eclipse Indigo or Juno or also Android Studio
- Android SDK
- At least one WiFi to connect
Please follow the steps below in order to make this application to connect a WiFi in Android device.
File – New- Android application project.
Give application name is for example 'WiFi connect'.
Project name and Package name
The next, select API level- android 4.0 ice cream sandwich. Target SDK API level 14 android 4.0 and compile with android 4.0 ice cream sandwich and theme to for ex. to holo light with dark action bar.
Then next change the text here for ex 'WiFi' and also change the foreground color.
Next activity is blank activity.
Then the next activity name is MainActivity and the layout name is ActivityMain. And finish.
Now here is our activity main.xml.
So in the relative layout change all the padding.
Text view take android:id @+id/mainText.
Layout width and height are both wrap content.
Android text is @string/hello world. So change it to android:layout_centerHorizontal is true. Android:layout_centerVertical is true. Android:text @string/hello_world.
And at last, Save it.
Now I am gonna browse to main activity.java.
Now first, you have to declare text view 'tv'; WiFi manager 'wifi'; WiFi receiver;
And then declare WiFi receiver class. Now you have to create a class WiFi receiver, enclosing type 'wificonnect.MainActivity'.
Then declare the wifi list.
Now string builder sb = new String builder. Now inside on create method, write tv = (text view) findViewById (R.id.mainText). wifi = (WiFiManager)getSystemService (Context.WIFI_SERVICE)
Now if wifi is enabled – if (wifi.isWiFiEnabled() == false). Toast.make text.get application context. Text is wifi is disabled .. enabled it, Toast .wifi . set wifi enabled is true. Receiver = new wifi receiver. Then register receiver reciver new intent filter is wifi manager. Scan_Results_Available_Action.
Now import this package android.content. intentfilter.
Now wifi.start scan. Main text.set text starting scan. Tv.set text (starting scan).
Now we have to override the methods. Source override implement methods onResume and onPause.
Add inside on pause inside on resume method. Register receiver (receiver, new intent filter(wifi manager.SCAN_RESULTS_AVAILABLE_ACTIONS)). Super.onResume and inside on pause method write unregister receiver is receiver.
Now public class wifi receiver extends broadcast receiver.
Now add unimplemented methods. Public void on receiver context context, intent intent. Change context and intent intent. Then inside the on receive method write string builder sb = new String builder. Wifi list that is paste here wifilist = new Wifi list. Wifilist = wifi.getscan result. Sb.append ( wifi connection : +wifi list .size () + n). for (int I = 0; i<wifi list.size(); i++).
Now string builder sb.append (new Integer(I+1). To String()+). Sb.append (wifilist.get(i).to string). And then sb.append (). now write text tv.set text is sb.
Now wifi manager is wifi manager. Now save it.
Now test the application in your device in manifest write uses permission android name = "android.permission.ACCESS_WIFI_STATE" and
uses permission android name = "android permission.CHANGE_WIFI_STATE".
Save it.
Complete code to create WiFi application in Android
app>src>main>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wificonnect"
android:versionCode="1"
android:VersionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
</manifest>
app>src>main>java>com.example.wificonnect>MainActivity.java
package com.example.wificonnect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends AppCompatActivity {
public class WifiRecevier extends BroadcastReceiver{
@override
public void onReceiver(Context context, Intent intent){
//TODO Auto-generate method stub
sb = new stringBuilder();
wifiList = wifi.getScanResult();
sb.append("n wifi connections : "+wifiList.size()+"nn");
for (int i=0; i<wifiList.size(); i++){
sb.append(new Integer(i+1).toString()+"");
sb.append(wifiList.get(i).toString());
sb.append("nn");
}
tv.setText(sb);
}
}
TextView tv;
WifiManager wifi;
WifiReceiver receiverwifi;
List<ScanResult> wifiList;
StringBuilder sb = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.mainText);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false){
Toast.makeText(getApplicationContext(), "Wifi is disabled...Enable It", Toast.LENGTH_SHORT).show();
wifi.setWifiEnabled(true);
}
receiver = new WifiReceiver();
registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULT_AVAILABLE_ACTION));
wifi.startScan();
tv.setText("Starting Sacn....");
}
@overide
protected void onResume() {
//TODO Auto-generate method stub
registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULT_AVAILABLE_ACTION));
super.onResume();
}
@overide
protected void onPause() {
//TODO Auto-generate method stub
unregisterReceiver(receiver);
super.onPause();
}
}
app>src>main>res>layout>activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wificonnect.MainActivity">
<TextView
android:id="@+id/mainText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>