Managing Device Discoverability

In order for remote Android Devices to find your local Bluetooth Adapter during a discovery scan, you need to ensure that it is discoverable.

The Bluetooth Adapter's discoverability is indicated by its scan mode. You can find the adapter's scan mode by calling getScanMode on the BluetoothAdapter object. It will return one of the following BluetoothAdapter constants:

> SCAN_MODE_CONNECTABLE_DISCOVERABLE Inquiry scan and page scan are both enabled, meaning that the device is discoverable from any Bluetooth device performing a discovery scan.

> scan_mode_connectable Page Scan is enabled but inquiry scan is not. This means that devices that have previously connected and bonded to the local device can find it during discovery, but new devices can't.

> scan_mode_none Discoverability is turned off. No remote devices can find the local adapter during discovery.

For privacy reasons, Android devices will default to having discoverability disabled. To turn on discovery you need to obtain explicit permission from the user; you do this by starting a new Activity using the ACTION_REQUEST_DISCOVERABLE action:

String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;

startActivityForResult(new Intent(aDiscoverable),

DISCOVERY_REQUEST);

By default discoverability will be enabled for two minutes. You can modify this setting by adding an extra_discoverable_duration extra to the launch Intent, specifying the number of seconds you want discoverability to last.

When the Intent is broadcast the user will be prompted by the dialog shown in Figure 13-2 to turn discoverability on for the specified duration.

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.

FIGURE 13-2

To learn if the user has allowed or rejected your discovery request, override the onActivityResult handler, as shown in Listing 13-4. The returned resultCode parameter indicates the duration of discoverability, or a negative number if the user has rejected your request.

LISTING 13-4: Monitoring discoverability modes

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == DISCOVERY_REQUEST) { boolean isDiscoverable = resultCode > 0; int discoverableDuration = resultCode;

Alternatively you can monitor changes in discoverability by receiving the action_scan_mode_changed broadcast action, as shown in Listing 13-5. The broadcast Intent includes the current and previous scan modes as extras.

LISTING 13-5: Monitoring discoverability modes registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {

String prevScanMode = BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE; String scanMode = BluetoothAdapter.EXTRA_SCAN_MODE;

int scanMode = intent.getIntExtra(scanMode, —1);

int prevMode = intent.getIntExtra(prevScanMode, —1);

}, new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED));

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