Posts Tagged Bluetooth

Bluetooth in Android-Part II

In my last post, I’ve discussed about how to check whether a device supports Bluetooth and enabling and disabling Bluetooth in a device which supports Bluetooth. In this post let’s see how to make our device discoverable to other Bluetooth devices.

Refer me previous post, Bluetooth in Android-Part I, so that things would be clear to you.

First of all to get the local Bluetooth adapter we use the getDefaultAdapter().

BluetoothAdapter myBTadapter = BluetoothAdapter.getDefaultAdapter();

Now in order to make our device discoverable to others we need to start activity with action intent ACTION_REQUEST_DISCOVERABLE. Though by default the duration to remain discoverable is 120seconds, this can be changed and can give a maximum duration of about 3600 seconds. Now to define a new duration that we desire, we need to add an intent extra named EXTRA_DISCOVERABLE_DURATION.

Just look at the code below:

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
startActivity(discoverableIntent);

Here the duration of the device to be discoverable is set as 400 seconds.

In the XML, I’ve added only one button. Just have a look at 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" />
    <Button
        android:id="@+id/btnMakeDiscoverable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Make your Deivice Discoverable" />
 </LinearLayout>

Here is a screen shot of how the UI will look like:

Add the following permissions in your manifest:

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

The Whole code:

package com.deepthi.mybluetoothdiscoverable;
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.Button;
import android.widget.Toast;
public class MyBluetoothDiscoverableActivity extends Activity {
 /** Called when the activity is first created. */
 Button MakeDiscoverable_btn;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 MakeDiscoverable_btn=(Button)findViewById(R.id.btnMakeDiscoverable);
 MakeDiscoverable_btn.setOnClickListener(new OnClickListener() {

 public void onClick(View v) {
 // TODO Auto-generated method stub
 BluetoothAdapter myBTadapter = BluetoothAdapter.getDefaultAdapter();
 Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
 discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
 startActivity(discoverableIntent);
 Toast.makeText(getApplicationContext(), "Now your device is discoverable by others", Toast.LENGTH_LONG).show();
 }
 });
 }
}

Hope this post was useful.Try this with your real Bluetooth supported android device. 🙂  Check this link for more details about emulator.

, , , , , , ,

8 Comments