Android Bluetooth Switch on/off programmatically

Bluetooth is a wireless technology standard for exchanging data over short distances, low-bandwidth peer-to-peer communications, between mobile devices creating personal area networks (PANs) with high levels of security. The Bluetooth device in Android is controlled via the BluetoothAdapter class. For the adatper to work you application manifest needs to include the BLUETOOTH and the BLUETOOTH_ADMIN permission.

If you are going to use Bluetooth Scanner in your mobile application to scan barcodes then it will stop you from using the soft keyboard unless a keyboard wedge is provided by the scanner company. So the only way to use the keyboard is to switch off the bluetooth as the mobile device thinks that the scanner is as a bluetooth keyboard. The ablity to quickly switch off bluetooth and switch it back on is needed if you want to both scan barcodes as well as use the keyboard in your app.

Android Bluetooth Switch on/off programmatically

Android Manifest

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android=""
 package="com.as400samplecode" android:versionCode="1"

 <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 <application android:icon="@drawable/ic_launcher"
  android:label="@string/app_name" android:theme="@style/AppTheme">
  <activity android:name=".MainActivity" android:label="@string/app_name">
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />


Application Layout - activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android=""
 xmlns:tools="" android:layout_width="match_parent"
 android:layout_height="match_parent" android:padding="5dp">

 <TextView android:id="@+id/status" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true" android:layout_marginTop="10dp"
  android:text="BlueTooth is currently switched OFF"
  android:textStyle="bold" />

 <Button android:id="@+id/changeStatus" style="?android:attr/buttonStyleSmall"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:layout_alignParentLeft="true" android:layout_below="@+id/status"
  android:layout_marginTop="10dp" android:text="Switch ON Bluetooth"
  android:textStyle="bold" />


Application Activity -

package com.as400samplecode;

import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

 private BluetoothAdapter mBluetoothAdapter;
 private Button changeStatus;
 private TextView status;

 public void onCreate(Bundle savedInstanceState) {

  //reference to the button
  changeStatus = (Button) findViewById(;

  //reference to the text view
  status = (TextView) findViewById(;

  //reference to the bluetooth adapter
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  //check if adatpter is available, please note if you running 
  //this application in emulator currently there is no support for bluetooth
  if(mBluetoothAdapter == null){
   status.setText("BlueTooth adapter not found");
   changeStatus.setText("BlueTooth Disabled");
  //check the status and set the button text accordingly
  else {
   if (mBluetoothAdapter.isEnabled()) {
    status.setText("BlueTooth is currently switched ON");
    changeStatus.setText("Switch OFF Bluetooth");
    status.setText("BlueTooth is currently switched OFF");
    changeStatus.setText("Switch ON Bluetooth");


 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(, menu);
  return true;

 public void onClick(View v) {

  switch (v.getId()) {

   //disable the bluetooth adapter
   if (mBluetoothAdapter.isEnabled()) {
    status.setText("BlueTooth is currently switched OFF");
    changeStatus.setText("Switch ON Bluetooth");
   //enable the bluetooth adapter
    status.setText("BlueTooth is currently switched ON");
    changeStatus.setText("Switch OFF Bluetooth");
   // More buttons go here (if any) ...