Android application to check Bluetooth status ON/OFF & turn ON/OFF by using android app

14/04/2017   07:17:53 PM

Hello in this android article i explain how to create an android application to check Bluetooth status of your android mobile ON/OFF & also with the help of android app turned ON/OFF Bluetooth of your android mobile. All android Bluetooth APIs are present in the android.bluetooth package. It have many classes and interfaces that need to create Bluetooth connections.Here we will use BluetoothAdapter.

What is BluetoothAdapter ?

It is the class
It is used to discover other Bluetooth devices.
MAC address is used to instantiate a BluetoothDevice.
It create a BluetoothServerSocket used to listen for communications from other devices.

 
1. Create a new project in android studio.
 
2. Add following code on 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"
    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="com.xperiaitinnovations.bluetoothstatus.MainActivity">
   <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/toggleButton"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp"
        android:onClick="onClick"
        android:textOff="ON"
        android:textOn="OFF" />
</RelativeLayout>

 

3. Add following code on MainActivity.java

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
 
public class MainActivity extends AppCompatActivity   {
ToggleButton tblbutton;
    BluetoothAdapter bluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   tblbutton= (ToggleButton) findViewById(R.id.toggleButton);
//tblbutton.setOnClickListener(this);
    }
 
    public void onClick(View v)
    {
        if (tblbutton.isChecked()) {
            //getting bluetooth object
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            //confirming that device support for bluetooth or not
            if (bluetoothAdapter == null) {
                Toast.makeText(this, "Device does not support to Bluetooth", Toast.LENGTH_LONG).show();
            }
//enable bt if is disabled
            if (!bluetoothAdapter.isEnabled()) {
                //enabled bluetooth
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intent, 1);
            }
//creating intent other discover other bt devices
            //enabled bluetooth
            Intent discoverintent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverintent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3000);
            startActivity(discoverintent);
        }
            else{
                Toast.makeText(getApplicationContext()," Bluetooth turned off",Toast.LENGTH_LONG).show();}
                //get bt object if bt is enable making disable
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if(bluetoothAdapter.isEnabled())
            {
                //disable the bt
                if(bluetoothAdapter!=null)
                {
                    bluetoothAdapter.disable();
                }
            }
            }
}

 

3. Add following code in AndroidManifest.xml file above <application> element

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 

OUTPUT 

Current bluetooth status of android mobile

android application to check bluetooth status ON/OFF

 

Through Android app,Enabling bluetooth

android application to check bluetooth status ON/OFF

 

Current Blutooth status is turned ON

android application to check bluetooth status ON/OFF

 

Current Blutooth status is turned OFF

android application to check bluetooth status ON/OFF


Output

                   

Support us by Sharing