【问题标题】:Disable Bluetooth discoverable mode on Android在 Android 上禁用蓝牙可发现模式
【发布时间】:2018-03-01 20:12:55
【问题描述】:

我在 Android 文档中找到了如何打开蓝牙可发现模式:

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

这将使设备在 300 秒内可被发现 (documentation)。

我的问题是:如何在此超时发生之前关闭可发现性?我想在设置|无线和网络|蓝牙设置小程序中复制相应的设置,以便通过单击打开和关闭可发现性。

有什么帮助吗?

【问题讨论】:

标签: android bluetooth


【解决方案1】:

只需发送一个持续时间为 1 的新可发现请求(或者 0 甚至可以工作):

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

【讨论】:

  • 同意,但这是我能找到的最好/唯一的解决方案。那么你的应用是做什么的呢?
  • 这只是一个简单的小部件来启用/禁用可发现性,没什么花哨的:)
  • 是的 1 有效。但是,将再次要求用户确认:(还有其他方式吗?
  • 根据documentation,使用 0 将无限期地置于可发现模式。
【解决方案2】:

cancelDiscovery() 不适合这个。此方法可用于停止扫描您的设备以查找其他蓝牙设备。与此不同的是,使设备不可见。

【讨论】:

    【解决方案3】:

    使用此方法时要小心,因为它是隐藏的,所以很容易更改。

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
    try {
        Method method = BluetoothAdapter.class.getMethod("setScanMode", int.class);
        method.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE);
    } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
        Log.e(TAG, "Failed to turn off bluetooth device discoverability.", e);
    }
    

    也可用于SCAN_MODE_NONESCAN_MODE_CONNECTABLE_DISCOVERABLE(使用默认持续时间)

    Source

    【讨论】:

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