Working with Bluetooth Android

Bluetooth APIs were made available as part of the Android 2.0 SDK. Clearly, that means that not all Android devices have Bluetooth hardware. However, this is a popular consumer feature that Android developers can use to their advantage. When Bluetooth hardware is present, Android applications can

  • Scan for and discover Bluetooth devices and interact with the Bluetooth adapter.
  • Establish RFCOMM connections and transfer data to and from devices via data streams.
  • Maintain point-to-point and multipoint connections with Bluetooth devices and manage multiple connections.

The Bluetooth APIs are part of the android.bluetooth package. As you might expect, the application must have permission to use the Bluetooth services. The android .permission. BLUETOOTH permission is required to connect to Bluetooth devices. Similarly, Android applications must have the android.permission.BLUETOOTH_ADMIN permission in order to administer Bluetooth hardware and related services, including tasks enabling or disabling the hardware and performing discovery scans.

The Bluetooth APIs are divided into several useful classes, including

  • The BluetoothAdapter class represents the Bluetooth radio hardware on the local device.
  • The BluetoothDevice class represents a remote Bluetooth device.
  • The BluetoothServerSocket class is used to open a socket to listen for incoming connections and provides a BluetoothSocket object when a connection is made.
  • The BluetoothSocket class is used by the client to establish a connection to a remote device. After the device is connected, BluetoothSocket object is used by both sides to handle the connection and retrieve the input and output streams.

Checking for the Existence of Bluetooth Hardware

The first thing to do when trying to enable Bluetooth functionality within your application is to establish whether or not the device has a Bluetooth radio. You can do this by calling and checking the return value of the BluetoothAdapter class’s static method called getDefault Adapter().

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Log.d(DEBUG_TAG, "No bluetooth available.");
// ...
} else {
// bt available
}

Enabling Bluetooth

After you have determined that the device has a Bluetooth radio, you need to check to see if it is enabled using the BluetoothAdapter class method called isEnabled(). If the Bluetooth adapter is enabled, you can proceed. Otherwise, you need to request that it is turned on. This can be done in several ways:

  • Fire off the BluetoothAdapter.ACTION_REQUEST_ENABLE Intent using the start Activity ForResult() method. This launches an Activity that enables the user to choose to turn on the Bluetooth adapter. If the result is RESULT_OK, then Bluetooth has been enabled; otherwise, the user canceled the Bluetooth-enabling process.
  • Call the BluetoothAdapter enable() method. This method should only be used by applications that need to explicitly enable the Bluetooth radio and requires the BLUETOOTH_ADMIN permission. In addition, it should only be performed as the result of a direct request from the user, such as through a button, menu item, and query dialog.
  • The process of making an Android device discoverable also automatically enables Bluetooth. This can be achieved by firing off the BluetoothAdapter.ACTION_ REQUEST_ DISCOVERABLE Intent using the startActivityForResult() method. This launches an Activity that presents the user with a choice to make their device discoverable for a set amount of time.

Querying for Paired Devices

You can use the BluetoothAdapter to query for available Bluetooth devices to connect to. The getBondedDevices() method returns a set of BluetoothDevice objects that represent the devices paired to the Bluetooth adapter.

Set<BluetoothDevice> pairedBtDevices = btAdapt.getBondedDevices();

Discovering Devices

New Bluetooth devices must be discovered and paired to the adapter before use. You can use the BluetoothAdapter to start and stop the discovery process for available Bluetooth devices to connect to. The startDiscovery() method starts the discovery process asynchronously. This method requires the android. permission. BLUETOOTH_ ADMIN permission.

After you have initiated the discovery process, your application needs to register to receive broadcasts for the following Intents:

  • ACTION_DISCOVERY_STARTED: Occurs when the discovery process initiates
  • ACTION_FOUND: Occurs each time a remote Bluetooth device is found
  • ACTION_DISCOVERY_FINISHED: Occurs when the discovery process completes

The discovery process is resource and time-intensive. You can use the isDiscovering() method to test if the discovery process is currently underway. The cancel Discovery() method can be used to stop the discovery process. This method should also be used any time a connection is about to be established with a remote Bluetooth device.

Establishing Connections Between Devices

The general idea behind connecting two devices via Bluetooth is for one device to find the other device via whatever means necessary, depending upon whether it be a previously paired device or found through discovery. After it’s found, the device calls the connect() method. Both devices then have a valid BluetoothSocket object that can be used to retrieve the InputStream and OutputStream objects for initiating data communications between the two devices.

Now, that’s where the theory ends and reality sets in. If it’s the same application running on both devices, as it usually is, this means both devices should find a remote device and both should be discoverable so they can also be found, as well as open a listening socket via the BluetoothServerSocket object so they can receive incoming connection requests, and be able to connect to the other device. Add to that the fact that both the calls to the accept() method of the BluetoothServerSocket class and to the connect() method of the BluetoothSocket class are blocking synchronous calls, and you can quickly see you need to use some threads here. Discovery also uses a fair amount of the Bluetooth hardware resources, so you need to cancel and then later restart this process as appropriate. Performing discovery during a connection or even while attempting a connection likely leads to negative device performance.

Figure shows a reasonable layout for a Bluetooth implementation, as well as labeling the threads used within the SimpleBluetooth project.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics