Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reconnect? #31

Open
zenbooster opened this issue Mar 15, 2022 · 3 comments
Open

How to reconnect? #31

zenbooster opened this issue Mar 15, 2022 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@zenbooster
Copy link

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

@zenbooster
Copy link
Author

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

@kai-morich
Copy link
Owner

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

@zenbooster
Copy link
Author

Okay, I'll definitely try it.

@kai-morich kai-morich added enhancement New feature or request question Further information is requested labels Mar 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants