SimpleBluetoothLeTerminal icon indicating copy to clipboard operation
SimpleBluetoothLeTerminal copied to clipboard

How to reconnect?

Open zenbooster opened this issue 3 years ago • 3 comments

How can I make automatic reconnection if the connection to the device is lost, and then reappeared?

zenbooster avatar Mar 15 '22 19:03 zenbooster

В 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);
    }

zenbooster avatar Mar 15 '22 20:03 zenbooster

looks good, but instead of sleep I recommend postDelayed to not block the UI thread and a timeout + cancel button

kai-morich avatar Mar 16 '22 07:03 kai-morich

Okay, I'll definitely try it.

zenbooster avatar Mar 16 '22 07:03 zenbooster