Академический Документы
Профессиональный Документы
Культура Документы
Android - Wi-Fi
Advertisements
Android allows applications to access to view the access the state of the wireless
connections at very low level. Application can access almost all the information of a wifi
connection.
The information that an application can access includes connected network's link speed,IP
address, negotiation state, other networks information. Applications can also scan, add,
save, terminate and initiate Wi-Fi connections.
Android provides WifiManager API to manage all aspects of WIFI connectivity. We can
instantiate this class by calling getSystemService method. Its syntax is given below −
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
In order to scan a list of wireless networks, you also need to register your
BroadcastReceiver. It can be registered using registerReceiver method with argument of
your receiver class object. Its syntax is given below −
The wifi scan can be start by calling the startScan method of the WifiManager class. This
method returns a list of ScanResult objects. You can access any object by calling the get
method of list. Its syntax is given below −
Apart from just Scanning, you can have more control over your WIFI by using the methods
Accept
defined in WifiManager class. They are listed as follows −
Learn more
Sr.No Method & Description
https://www.tutorialspoint.com/android/android_wi_fi.htm 1/7
5/14/2019 Android Wi-Fi
1 addNetwork(WifiConfiguration config)
This method add a new network description to the set of configured networks.
2 createWifiLock(String tag)
3 disconnect()
5 getWifiState()
6 isWifiEnabled()
7 setWifiEnabled(boolean enabled)
8 updateNetwork(WifiConfiguration config)
Example
Here is an example demonstrating the use of WIFI. It creates a basic application that open
your wifi and close your wifi
To experiment with this example, you need to run this on an actual device on which wifi is
turned on.
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Steps Description
https://www.tutorialspoint.com/android/android_wi_fi.htm 2/7
5/14/2019 Android Wi-Fi
5 Run the application and choose a running android device and install the application on
it and verify the results.
package com.example.sairamkrishna.myapplication;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
enableButton=(Button)findViewById(R.id.button1);
disableButton=(Button)findViewById(R.id.button2);
enableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
});
disableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
});
}
}
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="76dp"
android:text="Enable Wifi"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/imageView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable Wifi"
android:layout_marginBottom="93dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/imageView" />
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
We</application>
use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
</manifest>
https://www.tutorialspoint.com/android/android_wi_fi.htm 4/7
5/14/2019 Android Wi-Fi
Android studio will display following window to select an option where you want to run your
Android application.
Select your mobile device as an option and It will shows the following image−
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Accept
Learn more
https://www.tutorialspoint.com/android/android_wi_fi.htm 5/7
5/14/2019 Android Wi-Fi
Now click on disable wifi button.then the sample output should be like this −
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Accept
Learn more
https://www.tutorialspoint.com/android/android_wi_fi.htm 6/7
5/14/2019 Android Wi-Fi
Learn more
https://www.tutorialspoint.com/android/android_wi_fi.htm 7/7