Community » Forums » Android - Examples > Android Bluetooth API
November 27, 2009 3:25:00 PM PST (40 weeks ago). Seen 3,932 times.
Photo Marko Gargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Posts: 95
Android was release in version 1.0 without the Bluetooth support. The API was yanked out of the final release at the last minute. The reason was that the development team felt they didn't have the time to complete it properly and didn't want to commit to half-fast API.

Android 2.0 (SDK level 5) finally brings the official support for the Bluetooth. It is offered via android.bluetooth package.

Note that the emulator still doesn't support Bluetooth emulation. And since my Dev2/G2 is still running Android 1.5, I do not have a way to test this code and evolve it.

The main access to Bluetooth is done via the BluetoothAdapter. The adapter, returned via getDefaultAdapter() static method, offers access to various devices. The startDiscovery() method on the adapter will start the discovery of other discoverable devices. It is a synchronous call that can take some time to complete.



To get the list of all the devices that have been paired with the local device, use adapter.getBondedDevices().

/src/com.marakana/BluetoothDemo.java
Code:

package com.marakana;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.widget.TextView;

public class BluetoothDemo extends Activity {
TextView out;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

out = (TextView) findViewById(R.id.out);

// Getting the Bluetooth adapter
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + adapter);

// Check for Bluetooth support in the first place
// Emulator doesn't support Bluetooth and will return null
if(adapter==null) {
out.append("\nBluetooth NOT supported. Aborting.");
return;
}

// Starting the device discovery
out.append("\nStarting discovery...");
adapter.startDiscovery();
out.append("\nDone with discovery...");

// Listing paired devices
out.append("\nDevices Pared:");
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
out.append("\nFound device: " + device);
}
}
}


Mainfest File

Don't forget to add appropriate permissions in your manifest file:
Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.marakana" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@android:style/Theme.Light">
<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>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</application>

<uses-sdk android:minSdkVersion="5" />
</manifest>


Output

As I mentioned before, my emulator and my physical phone do not support Bluetooth at this point, so the output just says so. I'll update this example when the support is there.

Edited one time. Last edit by Marko Gargenta on Jun 15, 2010 at 1:15:25 AM (about 11 weeks ago).
May 26, 2010 12:55:32 PM PDT (15 weeks ago)
Photo Syed Agha
None
Member since May 20, 2010
Posts: 6
when i run this code on emulator it gives me the error "application has stopped unexpectedly". so can u please tell me . what is the problem?
May 26, 2010 1:30:26 PM PDT (15 weeks ago)
Photo Marko Gargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Posts: 95
Look at your logs. It's hard to tell what went wrong without any info. Use your LogCat - it's your friend.
May 28, 2010 1:13:12 PM PDT (14 weeks ago)
Photo Syed Agha
None
Member since May 20, 2010
Posts: 6
i am facing problem getDefaultAdapter . when i try to take the name or address of the adapter. it say "Application stopped unexpectedly" i check the logcat it say "unable to start activity component info" . what would u suggest?
May 28, 2010 1:14:53 PM PDT (14 weeks ago)
Photo Syed Agha
None
Member since May 20, 2010
Posts: 6
public class BlueToothAdapter extends Activity {
/** Called when the activity is first created. */
//BluetoothAdapter = BluetoothAdapter.getdefault();
private final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
//BluetoothDevice bluetooth = BluetoothAdapter.getDefaultAdapter().getAddress();

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

get_Name();
}
public void get_Name(){
if(bluetooth==null){

toast ="Bluetooth is not Enabled";
}
else{



String name=bluetooth.getName();
// String address = bluetooth.getAddress();
toast= name;
Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
}


}
}


this is my code
Edited one time. Last edit by Syed Agha on May 28, 2010 at 1:16:13 PM (about 11 weeks ago).