views:

454

answers:

2

Hi, I found in the Android documentation how to turn Bluetooth discoverability mode on:

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

This will make the device discoverable for 300 seconds (documentation).

My question is: how to turn discoverability OFF before this timeout occurs? I'd like to replicate the corresponding setting in Settings|Wireless and networks|Bluetooth settings applet, that allows discoverability to be turned on and off with a click.

Any help?

Thanks ;)

A: 

With cancelDiscovery()

http://developer.android.com/intl/zh-CN/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery%28%29

Brad Hein
Thanks, but that is not what I'm trying to do. The snippet I posted allows other devices to detect the phone and, for example, to send files to it. I'd like to revert the phone to its natural "undetectable" mode, so that other devices couldn't see it any longer.The method you suggested stops a device discovery process initiated by the phone itself to search for nearby devices, which can be started with the startDiscovery() method (see http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery() ).Bye
Venator85
Aah. My apologies.
Brad Hein
+1  A: 

Just send a new discoverable request with duration 1 (or 0 might even work):

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(discoverableIntent);
Brad Hein
Lol, hackish, but it works (with 1 only).Thanks ;)
Venator85
Agreed, but it's the best/only solution I could find. So what does your app do?
Brad Hein
It's just a simple widget to enable/disable discoverability, nothing fancy :)
Venator85