1

I have learned the codes to make blue-tooth discoverable in Android,like:

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

So, how can i make blue-tooth un-discoverable artificially as soon as i make it discoverable?

Thanks for all help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
钱子轩
  • 51
  • 5
  • 2
    A hackish (and so far the only that I am aware exists) solution is to set discoverability time limit to 1 second. Have a look at this answer: https://stackoverflow.com/questions/2938421/disable-bluetooth-discoverable-mode-on-android – lidkxx Nov 23 '17 at 09:57
  • that's a good solution,thanks! – 钱子轩 Nov 23 '17 at 10:30

1 Answers1

4

I have found a method,although i don't understand it yet,it works in my project!

public void closeDiscoverableTimeout() {  
    BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();  
    try {  
        Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);  
        setDiscoverableTimeout.setAccessible(true);  
        Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);  
        setScanMode.setAccessible(true);  

        setDiscoverableTimeout.invoke(adapter, 1);  
        setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE,1);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  
钱子轩
  • 51
  • 5