Android Bluetooth API



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


Manifest 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.

Published November 27, 2009