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="http://schemas.android.com/apk/res/android"
 package="com.as400samplecode" android:versionCode="1"
 android:versionName="1.0">

 <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">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>

</manifest>

Application Layout - 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: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:textAppearance="?android:attr/textAppearanceMedium"
  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" />

</RelativeLayout>

Application Activity - MainActivity.java

package com.as400samplecode;

import android.os.Bundle;
import android.app.Activity;
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;

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

  //reference to the button
  changeStatus = (Button) findViewById(R.id.changeStatus);
  changeStatus.setOnClickListener(this);

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

  //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");
   changeStatus.setEnabled(false);
  }
  //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");
   }else{ 
    status.setText("BlueTooth is currently switched OFF");
    changeStatus.setText("Switch ON Bluetooth");
   } 
  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 @Override
 public void onClick(View v) {

  switch (v.getId()) {

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

 }
}