Establishing WearOS webSocket connection when smartwatch not charging

Viewed 332

I have a wear-os standalone application written in java, which connects as client (org.java_websocket.client.WebSocketClient) via WLAN websockets to a server (org.java_websocket.server.WebSocketServer) running on the smartphone (TicWatch Pro 3). When I run the code, everything works fine (web sockets connect) as long as the smartwatch is charging. As soon as I disconnect the charger and try to connect the smartwatch client via websockets to the smartphone server no connection can be established. No error is shown, there is simply no connection established.

This is a simplified version of the Runnable class used to start the web socket client.

public class BackgroundReceiverThread  implements Runnable {

   public static void startBackgroundReceiver(){
       AsyncTask.execute(new BackgroundReceiverThread());
   }

   private BackgroundReceiverThread() {}

   //@Override
   public void run() {
       try {
           client=new BackgroundReceiverClient(new URI("ws://localhost:8887"));
           client.connect();
       } catch (URISyntaxException e) {
           e.printStackTrace();
       }
   }
}

The BackgroundReceiverClient class looks the following:

public class BackgroundReceiverClient extends WebSocketClient {

    public BackgroundReceiverClient(URI serverUri) {
        super(serverUri);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        Log.i("mytag","On Open");
    }

    @Override
    public void onError(Exception e) {
        Log.i("mytag","On Error:"+e.getMessage());
    }
}

When the smartwatch is charging and BackgroundReceiverThread.startBackgroundReceiver() is invoked, the onOpen() method is invoked. When the charger is disconnected the onOpen() method is not invoked.

I assume this is due to energy saving precautions of the smartwatch, which are activated when not charging. I have already tried to identify the respective setting on the smartwatch but couldn't find it.

How can I get the client connecting to the server when the smartwatch is not charging?

1 Answers

I had a fairly similar problem, I was trying to connect via Socket.IO with my Ticwatch E2 and it only worked when charging. After searching for all kinds of energy saving precautions that I could disable the real problem after all was this:

My Ticwatch automatically connects with my phone via Bluetooth and uses the wi-fi my phone is connected to through my phone (You can see the connection status in the Wear OS app on your phone). In order to establish a Socket Connection, the watch needs to be connected to the wi-fi itself. It does not do that by default when connected to your phone.

You can easily test this hypothesis by enabling flight mode on your phone and try the application on your Ticwatch again, it worked for my socket.IO application.

Related