Discovering Remote Devices

In this section you'll now learn how to initiate discovery from your local adapter to find discoverable devices nearby.

The discovery process can take some time to complete (up to 12 seconds). During this time, performance of your Bluetooth Adapter communications will be seriously degraded. Use the techniques in this section to check and monitor the discovery status of the Bluetooth Adapter, and avoid doing high-bandwidth operations (including connecting to a new remote Bluetooth Device) while discovery is in progress.

You can check to see if the local Bluetooth Adapter is already performing a discovery scan using the isDiscovering method.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

To initiate the discovery process call startDiscovery on the Bluetooth Adapter. To cancel a discovery in progress call cancelDiscovery.

bluetooth.startDiscovery(); bluetooth.cancelDiscovery();

The discovery process is asynchronous. Android uses broadcast Intents to notify you of the start and end of discovery as well as remote devices discovered during the scan.

You can monitor changes in the discovery process by creating Broadcast Receivers to listen for the action_discovery_started and action_discovery_finished broadcast Intents, as shown in Listing 13-6.

LISTING 13-6: Monitoring discovery

BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {

String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED; String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;

©Override public void onReceive(Context context, Intent intent) { if (dStarted.equals(intent.getAction())) { // Discovery has started. Toast.makeText(getApplicationContext(),

"Discovery Started ... ", Toast.LENGTH_SHORT).show();

else if (dFinished.equals(intent.getAction())) { // Discovery has completed. Toast.makeText(getApplicationContext(),

"Discovery Completed ... ", Toast.LENGTH_SHORT).show();

registerReceiver(discoveryMonitor, new IntentFilter(dStarted)); registerReceiver(discoveryMonitor, new IntentFilter(dFinished));

Discovered Bluetooth Devices are returned via broadcast Intents by means of the action_found broadcast action.

As shown in Listing 13-7, each broadcast Intent includes the name of the remote device in an extra indexed as BluetoothDevice.EXTRA_NAME, and an immutable representation of the remote Bluetooth device as a BluetoothDevice parcelable object stored under the BluetoothDevice.EXTRA_DEVICE extra.

LISTING 13-7: Discovering remote Bluetooth Devices

BroadcastReceiver discoveryResult = new BroadcastReceiver() { ©Override public void onReceive(Context context, Intent intent) { String remoteDeviceName =

intent.getStringExtra(BluetoothDevice.EXTRA_NAME);

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

BluetoothDevice remoteDevice;

remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

Toast.makeText(getApplicationContext() ,

"Discovered: " + remoteDeviceName, Toast.LENGTH_SHORT).show();

// TODO Do something with the remote Bluetooth Device.

registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));

if (!bluetooth.isDiscovering()) bluetooth.startDiscovery();

The BluetoothDevice object returned through the discovery broadcast represents the remote Bluetooth Device discovered. In the following sections it will be used to create a connection, bond, and ultimately transfer data between the local Bluetooth Adapter and the remote Bluetooth Device.

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