【问题标题】:How to make Bluetooth Undiscoverable?如何使蓝牙不可发现?
【发布时间】:2017-11-23 18:37:45
【问题描述】:

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

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

那么,一旦我使蓝牙可被发现,我如何才能人为地使其无法被发现?

感谢大家的帮助!

【问题讨论】:

  • 一个骇人听闻的(到目前为止我知道的唯一存在的)解决方案是将可发现时间限制设置为 1 秒。看看这个答案:stackoverflow.com/questions/2938421/…
  • 这是一个很好的解决方案,谢谢!

标签: android android-bluetooth


【解决方案1】:

我找到了一个方法,虽然我还不明白,但它在我的项目中有效!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多