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) ...
  }

 }
}

20 comments :

  1. I have a 7" ICS 4.0.4 android tablet pc with rooted can i block or remove permanently the wifi, bluetooth, hdmi, otg, sd card and all communication's.

    Please guide me...

    ReplyDelete
    Replies

    1. In your blog I was happy to see your article, better than last time, and have made great progress, I am very pleased. I am looking forward to your article will become better and better.
      hotmail login | red ball | 192.168.1.1

      Delete
  2. It is an example of very good and for me, a good base for another important issue. Thank you and congratulations

    ReplyDelete
  3. Thank you very much for the guidence

    ReplyDelete
  4. See whether the subsidizing organization makes awards to people. Painstakingly consider this step in light of the fact that some stipend producers provide for associations that serve people instead of specifically financing people.

    ReplyDelete
  5. wireless technology accepted for exchanging abstracts over abbreviate distancesIf you are activity to use Bluetooth Scanner in your adaptable appliance to browse barcodes again it will stop you from application the bendable keyboard unless a keyboard block is provided by the scanner company.

    ReplyDelete
  6. I read your post. A man connect to Bluetooth manually or programmatically. This post help how to connect programmatically. Thanks for your guidance post.

    ReplyDelete
  7. Thanks for your sharing! Your post is really helpful. You show me how to switch on and off Android Bluetooth programmatically.

    ReplyDelete
  8. Mechanical advances, especially those spinning around the utilization of cellular telephones for promoting purposes, have opened up numerous new stages for achieving purchasers. There is a considerable measure of buzz about Bluetooth advertising, yet does it really pay off by contacting the target group and producing a positive customer reaction?

    ReplyDelete
  9. Contact us if you are looking Buzz For Android We Develop in affordable price.....

    ReplyDelete
  10. very useful information! Thank you for letting us know!

    ReplyDelete
  11. Yes, very useful informations, thank for sharing great post!

    ReplyDelete
  12. This information is really helpful. Thanks a lot for sharing! Moreover, this is an effective post.

    ReplyDelete
  13. Thanks a lot for sharing! Moreover, this is an effective post.

    ReplyDelete
  14. Programming is really good, but only few people are ready to learn it

    ReplyDelete
  15. Thanks a lot for your review . can You publish any review http://digishoe.com/best-sneakers-for-plantar-fasciitis/

    ReplyDelete