14

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Venator85
  • 10,245
  • 7
  • 42
  • 57

3 Answers3

12

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
  • 10,997
  • 12
  • 51
  • 74
1

Care when using this method, it might be easily changed as it is hidden.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
try {
    Method method = BluetoothAdapter.class.getMethod("setScanMode", int.class);
    method.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE);
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
    Log.e(TAG, "Failed to turn off bluetooth device discoverability.", e);
}

Also usable with SCAN_MODE_NONE and SCAN_MODE_CONNECTABLE_DISCOVERABLE (uses default duration)

Source

1

cancelDiscovery() is not for this. This method can be used to stop the scanning of your device for other bluetooth devices. It is different from this to make device not visible.

Varun
  • 33,833
  • 4
  • 49
  • 42
cotalfa
  • 11
  • 1