SimpleBluetoothLeTerminal
SimpleBluetoothLeTerminal copied to clipboard
How to reconnect?
How can I make automatic reconnection if the connection to the device is lost, and then reappeared?
В TerminalFragment сделать что-то вроде этого:
public void onSerialIoError(Exception e) {
status("connection lost: " + e.getMessage());
disconnect();
do {
try {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
status("connecting...");
connected = Connected.Pending;
SerialSocket socket = new SerialSocket(getActivity().getApplicationContext(), device);
service.connect(socket);
} catch (Exception ex) {
SystemClock.sleep(250);
continue;
}
} while(false);
}
looks good, but instead of sleep I recommend postDelayed to not block the UI thread and a timeout + cancel button
Okay, I'll definitely try it.