Monday 12 January 2015

To Enable/Disable WiFi and Bluetooth Programmatically and Checking its Status

The following Sample Code describes To Enable/Disable WiFi and Bluetooth Programmatically and Checking its Status  in android:

1. activity_main.xml:
<LinearLayout 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:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="MediumText"
            android:textColor="#ff0000"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MediumText"
            android:textColor="#29166f"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <CheckBox
            android:id="@+id/checkBox1"
            android:paddingLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textColor="#ff0000"
            android:text="MediumText"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MediumText"
            android:textColor="#29166f"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <CheckBox
            android:id="@+id/checkBox2"
            android:paddingLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>


2. MainActivity:

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
WifiManager wifi;
private BluetoothAdapter mBluetoothAdapter;
private TextView Bluetooth_text;
private TextView Bluetooth_status;
private CheckBox Bluetooth_check;
private TextView Wifi_text;
private CheckBox Wifi_check;
private TextView Wifi_status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wifi_text = (TextView) findViewById(R.id.textView1);
Wifi_status = (TextView) findViewById(R.id.textView2);
Wifi_check = (CheckBox) findViewById(R.id.checkBox1);
Bluetooth_text = (TextView) findViewById(R.id.textView3);
Bluetooth_status = (TextView) findViewById(R.id.textView4);
Bluetooth_check = (CheckBox) findViewById(R.id.checkBox2);
// Initiate wifi service manager
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Check for wifi is disabled
if (wifi.isWifiEnabled() == false) {
Wifi_text.setText("To Enable Wifi, Tick Your Checkbox");
Wifi_status.setText("Wifi Disabled");
Wifi_check.setChecked(false);
else {
Wifi_text.setText("To Disable Wifi, Untick Your Checkbox");
Wifi_status.setText("Wifi Enabled");
Wifi_check.setChecked(true);
}
// reference to the bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Bluetooth_text.setText("BlueTooth adapter not found");
Bluetooth_status.setText("BlueTooth Disabled");
Bluetooth_check.setChecked(false);
} else if (mBluetoothAdapter.isEnabled()) 
{
Bluetooth_text.setText("To Disable BlueTooth, Untick Your Checkbox");
Bluetooth_status.setText("BlueTooth Enabled");
Bluetooth_check.setChecked(true);
else {
Bluetooth_text.setText("To Enable BlueTooth, Tick Your Checkbox");
Bluetooth_status.setText("BlueTooth Disabled");
Bluetooth_check.setChecked(false);
}
Bluetooth_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
Bluetooth_text.setText("To Enable BlueTooth, Tick Your Checkbox");
Bluetooth_status.setText("BlueTooth Disabled");
Bluetooth_check.setChecked(false);
} else
 {
mBluetoothAdapter.enable();
Bluetooth_text.setText("To Disable BlueTooth, Untick Your Checkbox");
Bluetooth_status.setText("BlueTooth Enabled");
Bluetooth_check.setChecked(true);
}
}
});
Wifi_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
Wifi_text.setText("To Enable Wifi, Tick Your Checkbox");
Wifi_status.setText("Wifi Disabled");
Wifi_check.setChecked(false);
else {
wifi.setWifiEnabled(true);
Wifi_text.setText("To Disable Wifi, Untick Your Checkbox");
Wifi_status.setText("Wifi Enabled");
Wifi_check.setChecked(true);
}
}
});
}
}

3. Update ManifestFile:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
<uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
ScreenShot:


1 comment:

  1. Thank you for sharing this valuable android coding and information.

    ReplyDelete