4

I have made some REST call with retrofit that access a local IP address on my local WiFi network, work great on mobile side, but once on the standalone wear app I have this error:

java.net.SocketTimeoutException: connect timed out
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.PlainSocketImpl.socketConnect(Native Method)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.Socket.connect(Socket.java:605)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:195)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at com.mylisabox.network.interceptors.HostSelectionInterceptor.intercept(HostSelectionInterceptor.kt:33)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at com.mylisabox.network.interceptors.TokenInterceptor.intercept(TokenInterceptor.kt:25)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.RealCall.execute(RealCall.java:69)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Single.subscribe(Single.java:2700)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at hu.akarnokd.rxjava2.debug.SingleOnAssembly.subscribeActual(SingleOnAssembly.java:41)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Single.subscribe(Single.java:2700)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.lang.Thread.run(Thread.java:761)

If I replace the local IP address by an online one it works correctly on the watch too. On the mobile application no problem I can access the same URL local or remote correctly. Both devices are under same WiFi network. Any idea ?

jaumard
  • 8,202
  • 3
  • 40
  • 63
  • Hey, did you ever find a better solution then the currently selected answer? Enabling Wi-Fi is much slower than just using the Bluetooth connection for a simple network request. Data layer API also won't really work as it is not supported on iPhones. Thanks, Thomas – Thomas Vos Jan 01 '19 at 15:50

3 Answers3

4

This issue has been bugging me too. What seems to be the case is that http/web calls are somehow proxied when Android Wear connects to the internet over bluetooth (and thus the phone).

A solution is to disable the bluetooth connection of your phone, this forces the Android Wear device to connect directly to your local wifi (if it supports wifi in the first place) and then the local API requests magically start working.

A more proper workaround (since you can't really ask your users to break the bluetooth connection with their phone every time they use your app), seems to be to use the NodeApi to check whether the resulting phone node is nearby (using Node.isNearby()) and if it is, proxy the API request to your phone yourself by using the MessageClient, then making the request on the phone and finally replying the result. Quite the hassle but for me that has been the only reliable way to call local APIs...

If someone knows why Google proxies these calls over bluetooth or has other suggestions to solve this I would love to know as well.

Cuberob
  • 156
  • 1
  • 6
  • yeah that what I was afraid of :( but no choice until Google fix this "issue" (or feature as they will call it lol) thanks ! – jaumard Jan 24 '18 at 11:11
  • Is this still the best solution? What about watches connected to an iPhone (no data layer api)? – Thomas Vos Aug 05 '18 at 19:39
0

When wear connected with mobile then used the mobile internet in that case API code not working. Please disconnected the wear from mobile then try again it give response to you. Please look this link I thnk it help for you: https://www.sitepoint.com/connecting-to-web-services-with-android-wear/

amrik
  • 1
  • 4
0

Wear will use the shared bluetooth connection, and the active interface type will be PROXY but not WIFI. you can check it by adb shell dumpsys netstats. In this case, wear isn't in the same local network with server, and the socket to the server will be timeout.

One solution is making your server has public IP or hostname. Another is using NetworkRequest to change connectivity to wifi before accessing API. Please look google's sample: https://github.com/googlesamples/android-WearHighBandwidthNetworking

Liang HAN
  • 39
  • 2