Recipe Discovering Bluetooth Devices

After Bluetooth is activated, to discover paired or available Bluetooth devices, use the BluetoothAdapter instance's startdiscovery() method as an asynchronous call.This requires registering a BroadcastReceiver to listen for ACTION_FOUND events that tell the application whenever a new remote Bluetooth device is discovered. This is shown in the example code in Listing 7.10.

Listing 7.10 Example Code for Discovering Bluetooth Devices private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) {

// Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(

BluetoothDevice.EXTRA_DEVICE); Log.v("BlueTooth Testing",device.getName() + "\n" + device.getAddress());

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(mReceiver, filter);


The broadcast receiver can also listen for ACTION_DISCOVERY_STARTED events and action_discovery_finished events that tell the application when the discovery starts and ends.

For other Bluetooth devices to discover the current device, the application can enable discoverability using the action_request_discoverable intent.This activity displays another dialog on top of the application to ask users whether or not they want to make the current device discoverable:

Intent discoverablelntent

= new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivity(discoverablelntent);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment