Android 将蓝牙设置为不可发现
我制作了一个使用蓝牙的应用程序Android 将蓝牙设置为不可发现,android,bluetooth,discoverability,Android,Bluetooth,Discoverability,我制作了一个使用蓝牙的应用程序 protected void onDestroy() { // TODO Auto-generated method stub BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if(adapter.isEnabled()) { adapter.disable(); } super.onDestroy(); } 在oncreat
protected void onDestroy() {
// TODO Auto-generated method stub
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter.isEnabled()) {
adapter.disable();
}
super.onDestroy();
}
在oncreate()
方法中,它启用蓝牙并将设备设置为无限期可见
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(!adapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivity(discoverableIntent);
}
}
在onDestroy()
中,它禁用蓝牙
protected void onDestroy() {
// TODO Auto-generated method stub
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter.isEnabled()) {
adapter.disable();
}
super.onDestroy();
}
但当我在退出应用程序后再次手动启用蓝牙时,它会自动设置为“可发现”无限期
在使用onDestroy()
功能禁用蓝牙之前,如何将蓝牙设置为Undisfoverable
仅在Nexus 5上测试这将启用可发现性1秒,并将使您免于无限期被发现
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1);
startActivity(discoverableIntent);
- 似乎没有您可以生成的意图
无法发现的装置李>
- 所以,你需要解决这个问题。启动一秒钟的新的可发现性意图就可以了李>
- 它将从无穷大降低可发现时间 到一秒钟,这是我们能达到的最接近的时间 无法发现李>
- 我知道这是一个黑客,但没有其他的 Android文档中提供的方法
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1); startActivity(discoverableIntent);
- 所以,你需要解决这个问题。启动一秒钟的新的可发现性意图就可以了李>
这可能是问题的重复:你能分享onCreate()和onDestroy()的代码吗@android\u Muncher添加了
onCreate()
和onDestroy()的代码discoverableIntent.putExtra(BluetoothAdapter.EXTRA\u DISCOVERABLE\u DURATION,0)代码>应用程序可以设置的最长持续时间为3600秒,值为0表示设备始终可被发现。任何低于0或高于3600的值都会自动设置为120秒。你想让它一直被发现吗?@android\u Muncher我想让它在应用程序运行时一直被发现这不是我想要的。我希望只要应用程序运行,它就可以被发现。在这种情况下,请将上述代码添加到您的onDestroy()中这就行了。问题是,一旦你发现了它,它就会保持在你的情况下发生的那种状态,除非你做了这种巧妙的破解,否则就没有办法让它不被发现。不会有同样的问题。我认为这是因为应用程序在1秒钟完成之前退出。如何让它等待1秒,然后禁用蓝牙<代码>睡眠(1000)
在300秒或任何需要的秒数时开关1不工作。这应该可以解决同样的问题。我认为这是因为应用程序在1秒钟完成之前退出。如何让它等待1秒,然后禁用蓝牙<代码>睡眠(1000)
不起作用请尝试在onPause()中写入。但是使用必要的标志,以便仅在需要时关闭可发现性,而不是在所有onPause()回调上关闭可发现性。这在你的应用程序的上下文中有意义吗?你能解释一下我如何使用必要的标志,以便只有当用户停止应用程序时才关闭可发现性吗