Check Bluetooth Status Using Bluetooth Adapter

This example will explain how to check if Bluetooth is supported in running device and whether its ON or OFF.

In the example below we will call startActivityForResult() with Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE, when user click button to enable it. Then we will re-check the status again in onActivityResult() when result returned.

Follow the steps below to create the code example:

Step1

Create a new android project in your android IDE and follow the steps of new project template. You will get some default code in main activity and main layout file. Replace the code of your main layout with the code below:

 

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidbluetooth.MainActivity" >
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/enablebt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable BlueTooth" />

</LinearLayout>

 

 

Step2

We will manage the the call to BluetoothAdapter intent in our main activity file. Write below code in your activity:

 

package com.example.bluetoothstatuschecker;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView textInfo;
Button buttonEnableBT;
BluetoothAdapter bluetoothAdapter;

final static int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textInfo = (TextView)findViewById(R.id.info);
buttonEnableBT = (Button)findViewById(R.id.enablebt);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
textInfo.setText("BlueTooth not supported in this device");
buttonEnableBT.setEnabled(false);
}else{
if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

}

buttonEnableBT.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_ENABLE_BT){
if(resultCode==RESULT_OK){
Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
}
}

if (bluetoothAdapter.isEnabled()) {
buttonEnableBT.setEnabled(false);
textInfo.setText("BlueTooth enabled");
}else{
buttonEnableBT.setEnabled(true);
textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
}

}

}

 

 

Step3

You must need the bluetooth access permission to access the status from you app. So add BlueTooth permission in manifest file as below:

 

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

 

 

OutPut

Run to check the output as below:

bluetooth1

bluetooth2

That’s it. Happy Coding 🙂

Leave a Comment: