Managing Bluetooth Properties and State

The Bluetooth Adapter offers methods for reading and setting properties of the local Bluetooth hardware.

The Bluetooth Adapter properties can be read and changed only if the Bluetooth adapter is currently turned on (that is, if its device state is enabled). If the device is off, these methods will return null.

If the Bluetooth Adapter is turned on, and you have included the Bluetooth permission in your manifest, you can access the Bluetooth Adapter's friendly name (an arbitrary string that users can set and then use to identify a particular device) and hardware address, as shown in Listing 13-2.

Use the isEnabled method, as shown in Listing 13-2, to confirm the device is enabled before accessing these properties.

LISTING 13-2: Reading Bluetooth Adapter properties downloadon BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); Wrox.com

String toastText; if (bluetooth.isEnabled()) {

String address = bluetooth.getAddress(); String name = bluetooth.getName(); toastText = name + " : " + address;

else toastText = "Bluetooth is not enabled";

Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();

If you also have the bluetooth_admin permission you can change the friendly name of the Bluetooth Adapter using the setName method:

bluetooth.setName("Blackfang");

To find a more detailed description of the current Bluetooth Adapter state, use the getState method, which will return one of the following BluetoothAdapter constants:

> STATE_TURNING_ON

> STATE_TURNING_OFF

> STATE_OFF

By default the Bluetooth adapter will be turned off. In order to conserve battery life and optimize security, most users will keep Bluetooth disabled unless it's in use.

To enable the Bluetooth Adapter you can start a system sub-Activity using the action_request_enable Bluetooth Adapter static constant as a startActivityForResult action string:

String enableBT = BluetoothAdapter.ACTION_REQUEST_ENABLE; startActivityForResult(new Intent(enableBT), 0);

The sub-Activity is shown in Figure 13-1. It prompts the user to turn on Bluetooth and asks for confirmation. If the user agrees, the sub-Activity will close and return to the calling Activity once the Bluetooth Adapter has turned on (or has encountered an error). If the user selects no, the sub-Activity will close and return immediately. Use the result code parameter returned in the onActivityResult handler to determine the success of this operation.

E3 A CD&ffljj'^4:24AM

FIGURE 13-1

It is also possible to turn the Bluetooth Adapter on and off directly, using the enable and disable methods, if you include the BLUETOOTH_ADMIN permission in your manifest.

Note that this should be done only when absolutely necessary and that the user should always be notified if you are manually changing the Bluetooth Adapter status on the user's behalf. In most cases you should use the Intent mechanism described earlier.

Enabling and disabling the Bluetooth Adapter are somewhat time-consuming, asynchronous operations. Rather than polling the Bluetooth Adapter, your application should register a Broadcast Receiver that listens for action_state_changed. The broadcast Intent will include two extras, extra_state and extra_previous_state, which indicate the current and previous Bluetooth Adapter states, respectively.

Listing 13-3 shows how to use an Intent to prompt the user to enable Bluetooth and a Broadcast Receiver to track changes in the Bluetooth Adapter status.

1 LISTING 13-3: Enabling Bluetooth and tracking the adapter state

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

Available for download on

Wrox.com BroadcastReceiver bluetoothState = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {

String prevStateExtra = BluetoothAdapter.EXTRA_PREVIOUS_STATE; String stateExtra = BluetoothAdapter.EXTRA_STATE; int state = intent.getIntExtra(stateExtra, -1); int previousState = intent.getIntExtra(prevStateExtra, -1);

case (BluetoothAdapter.STATE_TURNING_ON) : { tt = "Bluetooth turning on"; break;

case (BluetoothAdapter.STATE_ON) : { tt = "Bluetooth on"; unregisterReceiver(this); break;

case (BluetoothAdapter.STATE_TURNING_OFF) : { tt = "Bluetooth turning off"; break;

case (BluetoothAdapter.STATE_OFF) : { tt = "Bluetooth off"; break;

default: break;

Toast.makeText(this, tt, Toast.LENGTH_LONG).show();

String actionStateChanged = BluetoothAdapter.ACTION_STATE_CHANGED; String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE; registerReceiver(bluetoothState, new IntentFilter(actionStateChanged)); startActivityForResult(new Intent(actionRequestEnable), 0);

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment