The Android SDK ships with powerful Bluetooth APIs capable of managing the local Bluetooth adapter, scanning for nearby Bluetooth devices, transferring data between bluetooth devices, and more. This Bluetooth quick look will show you the most fundamental steps necessary to begin programming Bluetooth applications on the Android SDK.
Step 1: Import the Bluetooth Package
The first thing to do when you want to work with Bluetooth in an Android application is to import the Bluetooth API package. To do so, add the following line of code to the class you want to use the Bluetooth APIs:
import android.bluetooth.*;
Step 2: Set Bluetooth Permissions
In order to work with Bluetooth in your app, you'll need to set the following permission:
<uses-permission android:name="android.permission.BLUETOOTH" />
For more advanced Bluetooth tasks, such as setting the name of the local device as we will do in this tutorial, you'll need to configure the Bluetooth Admin permission. Add the following line to do so:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Step 3: Access the Bluetooth Adapter
The Android Bluetooth APIs include a BluetoothAdapter
class. This class is used as a singleton to access the Bluetooth radio on the Android device. This class must be instantiated in order to perform any Bluetooth related tasks in the program.
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
If the device the above is run on does not support Bluetooth, the returned value will be equal to "null". In a practical application, you should check for this condition and update the app if needed. You could check for this with a simple if
statement:
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); if(bluetooth != null) { // Continue with bluetooth setup. }
Step 4: Check if Bluetooth is Enabled
Like I pointed to above, when you first call getDefaultAdapter()
null will be returned if Bluetooth is not supported on the device. However, even if Bluetooth is supported on the device, it may not be enabled by the user. This is the next thing to check.
if (bluetooth.isEnabled()) { // Enabled. Work with Bluetooth. } else { // Disabled. Do something else. }
Step 5: Display Device Name and Address
Using the code above, let's next display a message to the user to report on the Bluetooth status. If Bluetooth is enabled on the device, we will display the device name and address. If Bluetooth is disabled, we will display a message to tell the user.
String status; if (bluetooth.isEnabled()) { String mydeviceaddress = bluetooth.getAddress(); String mydevicename = bluetooth.getName(); status = mydevicename + ” : ” + mydeviceaddress; } else { status = “Bluetooth is not Enabled.”; } Toast.makeText(this, status, Toast.LENGTH_LONG).show();
This code snippet uses the getName()
method to report the name of the Bluetooth device. If you set the BLUETOOTH_ADMIN
permission earlier, you could also change the device name to something else, like this:
bluetooth.setName("AndroidCoder");
Step 6: Display Bluetooth State
The BluetoothAdapter.getState()
method is used in order to get a detailed description of the bluetooth state. This method will return one of the four things:
- STATE_TURNING_ON
- STATE_ON
- STATE_TURNING_OFF
- STATE_OFF
The Bluetooth adapter is off by default to conserve battery life. The code above could have been modified to include the state like this:
String state = bluetooth.getState(); status = mydevicename + ” : ” + mydeviceaddress + " : " + state;
Conclusion
This has been a very quick look at the BluetoothAdapter
class and some of the fundamental methods used when just getting started with Bluetooth on Android. The code included above will check if Bluetooth is enabled and display diagnostic information if it is and an error message if it isn't.
NOTE: In order to test Bluetooth applications, you must run this code on an actual Android device. It will not work in the emulator.
Comments