首页 > 解决方案 > 如何在不通过蓝牙确认对话框的情况下使我的设备能够被附近的设备发现?

问题描述

我在 Android 中做了一个与蓝牙连接相关的应用程序,每次启用蓝牙时,它都会显示一个确认对话框,使您的设备无法在设备附近发现。

有什么方法可以在没有确认对话框的情况下使设备发现。

我用过这个:

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

标签: androidandroid-intentbluetoothandroid-bluetooth

解决方案


我发现这很有帮助:

try {
  Method bluetoothDeviceVisibility;
  bluetoothDeviceVisibility = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
  bluetoothDeviceVisibility.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0);
} catch (Exception e) {
  e.printStackTrace();
}

推荐阅读