Android WiFi: Android - LeaVe my baThRoom at-least !

android wifi

WiFi is a technology for wireless local area networking with devices based on the IEEE 802.11 standards. Devices that can use Wi-Fi technology include personal computers, video-game consoles, smartphones, digital cameras, tablet computers, digital audio players and modern printers. Wi-Fi compatible devices can connect to the Internet via a WLAN and a wireless access point. Such an access point (or hotspot) has a range of about 20 meters (66 feet) indoors and a greater range outdoors. Hotspot coverage can be as small as a single room with walls that block radio waves, or as large as many square kilometers achieved by using multiple overlapping access points.

Android allows applications to access to view the access the state of the wireless connections at very low level. Android provides WiFi API through which applications can communicate with the lower-level wireless stack that provides WiFi network access. Almost all information from the device supplicant is available, including the connected network's link speed, IP address, negotiation state, and more, plus information about other networks that are available. Some other API features include the ability to scan, add, save, terminate and initiate WiFi connections.

WifiManager is the primary API for managing all aspects of WiFi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It's Syntax is given below:-

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


WifiManager class provides different methods to control WiFi activities:-

  • int addNetwork(WifiConfiguration config): Add a new network description to the set of configured networks.
  • WifiManager.MulticastLock createMulticastLock(String tag): Create a new MulticastLock
  • WifiManager.WifiLock createWifiLock(String tag): This method creates a new WifiLock.
  • boolean disconnect(): This method disassociate from the currently active access point.
  • boolean enableNetwork(int netId, boolean disableOthers): This method allow a previously configured network to be associated with.
  • int getWifiState(): This method gets the Wi-Fi enabled state
  • boolean isWifiEnabled(): This method return whether Wi-Fi is enabled or disabled.
  • boolean setWifiEnabled(boolean enabled): This method enable or disable Wi-Fi.
  • int updateNetwork(WifiConfiguration config): This method update the network description of an existing configured network.
  • boolean disableNetwork (int netId): Disable a configured network.
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 −

class WifiScanReceiver extends BroadcastReceiver {

   public void onReceive(Context c, Intent intent) {
   }
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

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 :-


List wifiScanList = mainWifiObj.getScanResults();

String data = wifiScanList.get(0).toString();

Example

Let's see the simple example of wifi to enable and disable the wifi service.
To run this example you need actual Android device.
  • You will use Android studio to create an Android application under a package net.suven.android.androidwifi.
  • Modify src/MainActivity.java file to add necessary code.
  • Modify the res/layout/activity_main to add respective XML components.
  • Modify the AndroidManifest.xml to add the necessary permissions
  • Run the application and choose a running android device and install the application on it and verify the results.
Following is the content of src/MainActivity.java

package net.suven.android.androidwifi;

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;
import android.widget.Toast;


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.button);
        disableButton=(Button)findViewById(R.id.button1);

        enableButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(true);
                Toast.makeText(getBaseContext(), "WiFI Enabled",
                        Toast.LENGTH_LONG).show();

            }
        });

        disableButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(false);
                Toast.makeText(getBaseContext(), "WiFI Disabled",
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}
Following is the content of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"

    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="30dp"
        android:text="ANDROID WIFI"
        android:layout_above="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="11dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SCTPL"
        android:id="@+id/textView2"
        android:textSize="35dp"
        android:textColor="#ff16ff01"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/suvenlogo"
        android:layout_centerVertical="true"
        android:layout_alignEnd="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enable WiFi"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/textView2"
        android:layout_marginEnd="14dp"
        android:layout_marginBottom="56dp" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="76dp"
        android:text="Disable WiFI"
        android:layout_alignBaseline="@+id/button"
        android:layout_alignBottom="@+id/button"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="20dp" />

</RelativeLayout>
Following is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.suven.android.androidwifi">
    <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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Comments

Popular posts from this blog

PPC - SomeOne SpenDing - SomeOne EarNing

Web scrapping tools,Sooo Muuch Data - Analysis Needed !

Must kNow Web Dev toolS - 2OI7