我制作了一个使用蓝牙的应用程序
在该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();
}
但是当我退出应用程序后再次手动启用蓝牙时,它会自动设置为无限期可发现。
如何在功能Undiscoverable
中禁用蓝牙之前将其设置为onDestroy()
仅在 Nexus 5 上测试