Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android上打开/关闭蓝牙时永久删除可发现性检查_Java_Android_Bluetooth_Alert_Discoverability - Fatal编程技术网

Java 在Android上打开/关闭蓝牙时永久删除可发现性检查

Java 在Android上打开/关闭蓝牙时永久删除可发现性检查,java,android,bluetooth,alert,discoverability,Java,Android,Bluetooth,Alert,Discoverability,我正在开发一款Android应用程序,它被认为可以长时间运行。在某些时刻,我需要与一些Blutooth设备(3.0和BLE)进行通信,因此,我的想法是在需要时打开蓝牙,然后在其余时间将其关闭 我的问题出现在我打开BT时,一个警报总是弹出,告诉我们我的设备的可发现性,我希望删除这个警报 在四处寻找解决方案时,我尝试: Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); disco

我正在开发一款Android应用程序,它被认为可以长时间运行。在某些时刻,我需要与一些Blutooth设备(3.0和BLE)进行通信,因此,我的想法是在需要时打开蓝牙,然后在其余时间将其关闭

我的问题出现在我打开BT时,一个警报总是弹出,告诉我们我的设备的可发现性,我希望删除这个警报

在四处寻找解决方案时,我尝试:

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivityForResult(discoverableIntent, GlobalConfiguration.REQUEST_DISCOVERABLE);

这两个都没有成功,在BT状态从ON->OFF->ON切换后,关于可发现性的对话框再次出现

有一种方法可以永远删除此警报,也可以在蓝牙打开/关闭后删除


提前感谢

您不应该以编程方式打开或关闭蓝牙。请记住,您的应用程序可能不是唯一在设备上使用蓝牙的应用程序。这应该由用户在系统设置中完成,问题是:我会让这个应用运行很长时间(几个月),用户将无法管理蓝牙设置,没有BT应用程序就没有意义存在,所以我按照我的需要来管理它;决定打开/关闭有两个原因:1)我不认为让蓝牙始终打开是最佳做法2)有时我会有一些奇怪的行为,让BT始终打开,所以我更喜欢在需要时关闭并再次打开。我的目标是不要反复向用户询问蓝牙启用和可发现性,可能吗?谢谢:)
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Method method;
try {

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

   e.printStackTrace();
}