Friday, 28 August 2015

Android Programming Tutorials 28 : WiFi In Android


OutPut Of the Tutorial:




Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: WiFiApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import java.util.List;

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class MainActivity<T> extends Activity {
               
                WifiManager mainWifiObj;
                WifiScanReceiver wifiReciever;
                ListView list;
                String wifis[];
               
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                list = (ListView) findViewById(R.id.listView1);
    mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                wifiReciever = new WifiScanReceiver();
                mainWifiObj.startScan();
                }
                @Override
                protected void onPause() {
               // TODO Auto-generated method stub
               unregisterReceiver(wifiReciever);
               super.onPause();
               }
               @Override
               protected void onResume() {
               // TODO Auto-generated method stub
    registerReceiver(wifiReciever, new                                                                                       IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
               super.onResume();
               }
               class WifiScanReceiver extends BroadcastReceiver{
               @Override
               public void onReceive(Context context, Intent intent) {
               //List<E>O Auto-generated method stub
               List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
               wifis = new String[wifiScanList.size()];
               for(int i=0; i<wifiScanList.size(); i++){
               wifis[i] =((wifiScanList.get(i).toString()));
               }
     list.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, wifis));
               }
              }
       }

     2.Simple Copy and Past the below code:  actvitiy_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:background="@drawable/backwifi"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
       
        <ListView
            android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:drawSelectorOnTop="false"
          android:background="@android:color/background_dark"
          android:listSelector="@android:color/darker_gray" >
          </ListView>
        
    </RelativeLayout>

    3.Copy and past the code for res=>value=>string.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">WifiApp</string>
        <string name="action_settings">Settings</string>
        <string name="title_activity_list_wifi">List</string>

    </resources>
     4.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.ambilpursunil.newapp.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>
    5.Right click on the project and Run As-->Android Application




    OutPut Of the Tutorial:


    OutPut:

    When our application launch if any is WiFi ON then it will appear in the form of list. 
    NOTE: In The Above Output video there is no wifi avaliable that is why screen is empty,
    if any wifi service is avaliable the it will display in the list

    Stay Tune For Next Tutorial...  ToggleButton In Android:



    Please Send Your Comments To ambilpura.sunil@gmail.com

    No comments:

    Post a Comment