SSLException kills ParticleEventHandler

I quite new to programming but I have somehow managed to develop an Android app that I’m using to control the heater in my caravan using an Electron hooked up to the heaters serial interface. It works very well but I have a problem that occurs sometimes that I haven’t been able to solve. Maybe it’s easy or maybe I’m just doing things wrong. Can anyone help me out?

For my app I have a background service that’s listening for events from my Electron. In the service I’m subscribing to events using something like the following code.

public class ListenForDataThread extends Thread {
        public void run() {
            long subscriptionId;

            try {
                ParticleCloudSDK.getCloud().logIn(pref_user_id, pref_password);
            } catch (ParticleCloudException e) {
                e.printStackTrace();
            }

            try {
                myDevice = ParticleCloudSDK.getCloud().getDevice(pref_device);
            } catch (ParticleCloudException e) {
                e.printStackTrace();
            }

            if (myDevice != null) {
                try {
                    subscriptionId = myDevice.subscribeToEvents("state",
                            particleEventHandler = new ParticleEventHandler() {
                                public void onEvent(String eventName, ParticleEvent event) {
			                            //...Do some things…
                                    }
                                }

                                public void onEventError(Exception e) {
                                    e.printStackTrace();
                                }
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
   }

The problem I’m having is that sometimes I get an exception like this:

E/HttpRequestDelegateImpl: javax.net.ssl.SSLException: Read error: ssl=0x71813f7200: I/O error during system call, Software caused connection abort

I don’t know why this happens and I’m unable to catch it when it happens. I suspect that it occurs when something is happening with the network. I can force a similar situation by switching network but that I’m able to catch and act on. When it happens it seems that the ParticleEventHandler dies and I’m no longer receiving any events. Since I’m unable to catch it I don’t know how to act on it in my code and I have to manually restart my app to get it to work again.

Thank you!

I think I know why it happens, but am not sure how to fix it.

When you subscribe to events from a mobile device it uses the SSE (Server Sent Events) stream. Your mobile devices opens a TLS/SSL encrypted TCP connection to the SSE server and events are pushed down this connection as they occur.

When the connection is interrupted, it’s probably generating an exception that’s not caught, and the connection is not being reopened. I’m not sure how to fix that.

The connection could be interrupted for any number of reasons, but certainly switching networks would break the connection.

Thank you for your reply!

I think that theory is plausible. I think I would like the ParticleEventHandler to throw an exception when this happens. That way I could catch it and do what’s necessary, depending on the situation.

Has anyone else experienced this problem?