如何使蓝牙无法发现?

时间:2017-11-23 09:33:13

标签: android android-bluetooth

我已经学会了在Android中可以发现蓝牙的代码,例如:

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

那么,一旦我发现它可以被发现,我怎么能人为地制造蓝牙呢?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我找到了一种方法,虽然我还不了解它,但它适用于我的项目!

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