简体   繁体   中英

Connection Refused in WearableListenerService / Android Wear when phone in sleep mode

I am developing an Android Wear Watchface that accesses the Internet by sending a MessageAPI request from wear to my mobile app, where I am running a WearableListenerService, and on my mobile app, I receive the request from wear in onMessageReceived, and in onMessageReceived I start an AsyncTask to retrieve an image from the Internet.

This works all fine normally .

However, if the phone is inactive for a few minutes (screen black, not connected to my mac via USB) I receive an "ECONNREFUSED - Connection refused by server", when the AsyncTask tries to retrieve an inputstream on this line:

InputStream is = (InputStream) new URL(mUrl).getContent();

Here is the Exception:

java.net.ConnectException: failed to connect to dl.dropboxusercontent.com/xx.xx.xxx.xx (port 443): connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:118)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
at java.net.Socket.connect(Socket.java:838)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
at com.android.okhttp.Connection.connect(Connection.java:101)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:143)
at java.net.URLConnection.getContentType(URLConnection.java:326)
at java.net.URLConnection.getContent(URLConnection.java:193)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:169)
at java.net.URL.getContent(URL.java:455)
at wear.webcam.MobileRequestListenerService$MyImageLoaderTask.doInBackground(MobileRequestListenerService.java:479)
at wear.webcam.MobileRequestListenerService$MyImageLoaderTask.doInBackground(MobileRequestListenerService.java:451)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:131)
at libcore.io.IoBridge.connect(IoBridge.java:116)
... 23 more
  • My app retries a minute later, and when the phone is on (screen on), it works!
  • It has nothing to do with the server/the url I am calling, as I am getting the same error with different servers.
  • It has nothing to do with Wifi, because it happens regardless if in Wifi or normal network.
  • This happens in the PROD/Signed and DEBUG version
  • I am checking if the network is available right before the call, and I am getting back "true", so the network is indeed on!

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    // if no network is available networkInfo will be null
    // otherwise check if we are connected
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

It looks like this problem can be solved by acquiring a wakelock - however this did not solve the problem so far.

Any help is appreciated!!!

Android 4.4.4 Sony Xperia Z1; Android Wear 5.0.2.

求助:事实证明,原因是Xperia上的“节能选项”:设置为“ STAMINA-Modus”,这导致设备无法唤醒。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM