Detect Check Bluetooth is enabled or not in android programmatically

How to find and show Bluetooth is start or Off in android using app on button click.

Using BluetoothAdapter class android app developer can easily detect the current state of Bluetooth that it is start or off. So in this tutorial we are detecting the current state of Bluetooth on button click and show on screen using Textview. So here is the complete step by step tutorial for Detect Check Bluetooth is enabled or not in android programmatically.

android-project-download-code-button

Note : Please add BLUETOOTH and BLUETOOTH_ADMIN permission to your AndroidManifest.xml file.

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

 

How to Detect Check Bluetooth is enabled or not in android programmatically.

Code for MainActivity.java file.

 package com.checkbluetoothenabledornot_android_examples.com;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Button button;
 BluetoothAdapter bluetoothadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 bluetoothadapter = BluetoothAdapter.getDefaultAdapter();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if(bluetoothadapter.isEnabled()){
 
 textview.setText("Bluetooth is Enable");
 
 }
 else {
 
 textview.setText("Bluetooth is disable");
 }
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <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.checkbluetoothenabledornot_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="148dp"
 android:text="Bluetooth Status"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="44dp"
 android:text="Click Here to Check bluetooth is enabled or not in android programmatically" />

</RelativeLayout>

Code for AndroidManifest.xml file.

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

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.BLUETOOTH"/>
 <uses-permission android: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=".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>

Screenshot:

Detect Check Bluetooth is enabled or not in android programmatically

Click here to download Detect Check Bluetooth is enabled or not in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *