How to create an app to turn on, turn off Bluetooth of android device dynamically on button click example.
Bluetooth is one of the most oldest way to transfer files between two mobile phone, laptops, computers. So in this tutorial we are enabling and disabling android mobile phone device Bluetooth on switch button on and off event. So when application developer on the switch button then it will start the device Bluetooth and after off the switch button it will automatically turn off the phones Bluetooth. So here is the complete step by step tutorial for Enable Disable Bluetooth in android programmatically.
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 Enable Disable Bluetooth in android programmatically.
Code for MainActivity.java file.
package com.enabledisablebluetooth_android_examples.com; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; public class MainActivity extends Activity { Switch switchButton; TextView textview; BluetoothAdapter bluetoothadapter; int i = 1; Intent bluetoothIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); switchButton = (Switch)findViewById(R.id.switch1); textview = (TextView)findViewById(R.id.textView1); bluetoothadapter = BluetoothAdapter.getDefaultAdapter(); switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { BluetoothEnable(); } else { BluetoothDisable(); } } }); } public void BluetoothEnable(){ bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(bluetoothIntent, i); textview.setText("Bluetooth ON"); } public void BluetoothDisable(){ bluetoothadapter.disable(); textview.setText("Bluetooth OFF"); } }
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.enabledisablebluetooth_android_examples.com.MainActivity" > <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch1" android:layout_centerHorizontal="true" android:layout_marginBottom="62dp" android:gravity="center" android:text="Bluetooth status show here" android:textAppearance="?android:attr/textAppearanceLarge" /> </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.enabledisablebluetooth_android_examples.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" 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>
Screenshots: