5

I am able to turn on/off Bluetooth without any prompt using the following code. It requires BLUETOOTH and BLUETOOTH_ADMIN permissions.

boolean isEnabled = bluetoothAdapter.isEnabled();
if (enable && !isEnabled) {
    return bluetoothAdapter.enable();
} else if (!enable && isEnabled) {
    return bluetoothAdapter.disable();
}

But didn't find any way to set Bluetooth discoverable without user prompt. It's wired to prompt every time to user. There is no "don't ask me again" feature I afraid. Is there any good way to make Bluetooth device discoverable? I don't care about the duration. Also my device is not rooted.

More Info

I found source code of BluetoothAdapter.java and it has a public method named setDiscoverableDuration. But why I can't access it? Why some public methods are hidden in Api documentations? How did they even do that? all methods are public.

Community
  • 1
  • 1
shantanu
  • 2,408
  • 2
  • 26
  • 56
  • Not sure if i understand you correctly, but discoverable applies to the remote device, it must be discoverable in able to find it with a scan. – JPS May 14 '15 at 19:54
  • @JPS Using discoverable I mean that my device should be found by other device when they will scan. – shantanu May 14 '15 at 20:34
  • 1
    Checkout this, seems to be hidden: http://stackoverflow.com/questions/3190623/make-bluetooth-on-android-2-1-discoverable-indefinitely – JPS May 14 '15 at 21:35

1 Answers1

17

Finally I have found a way to do this using reflection.

Method method;
try {
    method = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
    method.invoke(bluetoothAdapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,120);
    Log.e("invoke","method invoke successfully");
}
catch (Exception e){
    e.printStackTrace();
}

Warning: Above method is trying to invoke hidden method. So in future maybe it will not work.

shantanu
  • 2,408
  • 2
  • 26
  • 56