WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Wi-Fi Tutorial With Example In Android Studio


wi fi application need many information such as scan, add, save, terminate.


In Android WifiManager API to manage all aspects of WIFI connectivity.You can instantiate this class by calling getSystemService method
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

We need to BroadcastReceiver registration for scan a list of wireless networks class WifiScanReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { } } WifiScanReceiver wifiReciever = new WifiScanReceiver(); registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); When your wifi start scan it method is called startScan.then we need to access any object by calling the get method of list.
List wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();List wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();

Example Wi-Fi Android

step.1 create activity file src/MainActivity.java
package com.example.welookups.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;

public class MainActivity extends Activity {
   Button enableButton,disableButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      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);
         }
      });
   }
}
then create content of the xml res/layout/activity_main.xml.

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
	
   <ImageView
      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="58dp"
      android:layout_alignParentBottom="true"
      android:layout_alignStart="@+id/imageView" />

</RelativeLayout>
then create AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
   
<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>
      
 </application>
</manifest>