Archive for May, 2012

Bluetooth in Android-Part I

This time I thought of posting some basic information about Bluetooth in Android.

Android supports Bluetooth APIs to access Bluetooth functionalities.

Four major tasks necessary to communicate using Bluetooth are:

1) Setting up Bluetooth

2) Finding devices

3) Connecting devices

4) Transferring data between devices

In this post I’m going to discuss about how to check whether the device supports Bluetooth and if it does, how to enable the same.

Here we go!! 🙂

First of all we need to check whether the device supports Bluetooth. For every Bluetooth activity we need a class named BluetoothAdapter which controls the local Bluetooth device. So the first step is getting the Bluetooth adapter of the device which is done using the method getDefaultAdapter().

Just check the code below:

BluetoothAdapter myBTadapter=BluetoothAdapter.getDefaultAdapter();

Now, as you know now that the getDefaultAdapter() method returns the local Bluetooth adapter, if it returns null then we can easily understand that the device doesn’t support Bluetooth. So let’s display a toast saying it doesn’t support Bluetooth.

Here is the code.

BluetoothAdapter myBTadapter=BluetoothAdapter.getDefaultAdapter();
if(myBTadapter==null)
{
 Toast.makeText(getApplicationContext(), "Device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
 }

Now, if  getDefaultAdapter method doesn’t return null and if Bluetooth is disabled we need to enable the same.

Let’s see how it is done. First of all look at the code below.

if(!myBTadapter.isEnabled())
 {
 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
 startActivityForResult(enableBtIntent, REQ_BT_ENABLE);
 Toast.makeText(getApplicationContext(), "Enabling Bluetooth!!", Toast.LENGTH_LONG).show();
 }

First of all what we need to do is check whether Bluetooth is enabled on the device using method isEnabled(). It returns a Boolean value. If the value returned is false, the Bluetooth is not enabled. So we need to enable it. For that we need to create an intent with action  ACTION_REQUEST_ENABLE (which is a BluetoothAdapter static constant).

The intent is passed to startActivityForResult(). The second argument of the method is REQ_BT_ENABLE which is an integer constant which is passed back to onActivityResult() as the requestCode.

The code below shows the onActivityResult():

@Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQ_BT_ENABLE){
if (resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "BlueTooth is now Enabled", Toast.LENGTH_LONG).show();
}
 if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Error occured while enabling.Leaving the application..", Toast.LENGTH_LONG).show();
 ///finish();
 }
 }
 }//onActivityResult

We can disable the Bluetooth using disable() method as shown below:

myBTadapter.disable();

The Whole Code :

package com.deepthi.mybluetoothenabledemo;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MyBluetoothEnabledemoActivity extends Activity {
 /** Called when the activity is first created. */
 BluetoothAdapter myBTadapter;
 Integer REQ_BT_ENABLE=1;
 CheckBox enable;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 myBTadapter=BluetoothAdapter.getDefaultAdapter();
 enable=(CheckBox)findViewById(R.id.cboxEnable);
 enable.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 if(buttonView.isChecked())
 {
 if(myBTadapter==null)
 {
 Toast.makeText(getApplicationContext(), "Device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
 }
 else
 {
 if(!myBTadapter.isEnabled())
 {
 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
 startActivityForResult(enableBtIntent, REQ_BT_ENABLE);
 Toast.makeText(getApplicationContext(), "Enabling Bluetooth!!", Toast.LENGTH_LONG).show();
 }
 }
 }
 else
 {
 Toast.makeText(getApplicationContext(), "Disabling Bluetooth!!", Toast.LENGTH_LONG).show();
 myBTadapter.disable();
 }
 }
 });

 }
 @Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQ_BT_ENABLE){
if (resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "BlueTooth is now Enabled", Toast.LENGTH_LONG).show();
}
 if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Error occured while enabling.Leaving the application..", Toast.LENGTH_LONG).show();
 ///finish();
 }
 }
 }//onActivityREsult
}

In the Manifest:

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

The XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello" />
<CheckBox
 android:id="@+id/cboxEnable"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Enable" />
</LinearLayout>

Try this on a real Android device. As the emulator does not support Bluetooth. Check this link.

Hope this post has been useful to you. 🙂

Advertisements

, , , , , , , , ,

27 Comments