钱子轩
钱子轩

Reputation: 51

How to make Bluetooth Undiscoverable?

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!

Upvotes: 1

Views: 879

Answers (1)

钱子轩
钱子轩

Reputation: 51

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();  
    }  
}  

Upvotes: 4

Related Questions