Wednesday, May 2, 2012

How to handle bluetooth settings from your application



How to handle bluetooth settings from your application


This is a sample activity which shows How to enable and disable bluetooth settings from your application. Last post published in this forum is What is DDMS and how it works.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project bluetoothdemo.
2.) Add following permissions in AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.bluetoothdemo"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN">
    </uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".bluetoothdemo" 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>
3.) Put the following code snippet in res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent">
   <TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" />
   <RelativeLayout android:id="@+id/relativeLayout1"android:layout_width="fill_parent" android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical">
        <ToggleButton android:layout_width="wrap_content"android:text="ToggleButton" android:layout_height="wrap_content"android:id="@+id/toggleButton1"android:gravity="center|center_vertical|center_horizontal">
        </ToggleButton>
   </RelativeLayout>
</LinearLayout>
4.) Get the BluetoothAdapter, The BluetoothAdapter is required for any and all Bluetooth activity. For example:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null)
{
        // Device does not support Bluetooth
}
5.) Next, you need to ensure that Bluetooth is enabled. Call isEnabled() to check whether Bluetooth is currently enable. For example :
if (!mBluetoothAdapter.isEnabled())
{
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, 5);
}
6.) If you would like to make the local device discoverable to other devices, call startActivityForResult(Intent, int) with the ACTION_REQUEST_DISCOVERABLE action Intent. For example :
Intent discoverableIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
7.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. bluetoothdemo. Enter following information:
Project name: bluetoothdemo
Build Target: Android APIs 2.1
Application name: bluetoothdemo
Package name: com.app. bluetoothdemo
Create Activity: bluetoothdemo
On Clicking Finish bluetoothdemo code structure is generated with the necessary Android Packages being imported along with bluetoothdemo.java. bluetoothdemo class will look like following:
package com.app.bluetoothdemo;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class bluetoothdemo extends Activity implements OnClickListener {
   ToggleButton tb;
   BluetoothAdapter mBluetoothAdapter;
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tb = (ToggleButton) findViewById(R.id.toggleButton1);
        tb.setOnClickListener(this);
   }
   public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.toggleButton1 :
                if((tb).isChecked())
                {
                    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    if (mBluetoothAdapter == null) {
                        // Device does not support Bluetooth
                    }
                    if (!mBluetoothAdapter.isEnabled()) {
                       Intent enableBtIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                       startActivityForResult(enableBtIntent, 5);
                    }
                    Intent discoverableIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                    startActivity(discoverableIntent);
                }
                else
                {
                    Toast myToast = Toast.makeText(getApplicationContext(),"Bluetooth turned off", Toast.LENGTH_SHORT);
                    myToast.show();    
                    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    if (mBluetoothAdapter == null) {
                        // Device does not support Bluetooth
                    }
                    if (mBluetoothAdapter.isEnabled()) {
                        if (mBluetoothAdapter != null) {
                                mBluetoothAdapter.disable();
                        }
                    }
                }
          }
     }
}
Output – The final output:
Share and Enjoy:

No comments: