Answer the question
In order to leave comments, you need to log in
How are BluetoothDevice and BT adapter status (on / off) related?
Good afternoon, Habr!
Faced the following problem:
there is a piece of code like this:
private HashSet discoveredDevices;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//....
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (savedInstanceState == null) {
discoveredDevices = new HashSet();
} else {
discoveredDevices = (HashSet) savedInstanceState
.getSerializable("devices");
for (BluetoothDevice device : discoveredDevices) {
adapter.add(device.getName() + "\n" + device.getAddress());
}
}
//....
}
И код сохранения состояния:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("devices", discoveredDevices);
}
Если отключить БТ и повернуть экран (т.е. попасть в обработчик onActivityCreated), то на этом месте:
discoveredDevices = (HashSet) savedInstanceState.getSerializable("devices");
программа вылетает.
Может кто-нибудь объяснить, почему так происходит?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question