代码之家  ›  专栏  ›  技术社区  ›  jaumard

Android Wear 2无线局域网接入

  •  4
  • jaumard  · 技术社区  · 7 年前

    我已经做了一些改进,在我的本地WiFi网络上访问本地IP地址,在移动端工作得很好,但一旦在独立的wear应用程序上,我就出现了这个错误:

    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)
    

    如果我用在线IP地址替换本地IP地址,它在手表上也能正常工作。在移动应用程序上,我可以在本地或远程正确访问相同的URL。两台设备都在同一个WiFi网络下。 知道吗?

    3 回复  |  直到 7 年前
        1
  •  3
  •   Cuberob    6 年前

    这个问题也一直困扰着我。事实似乎是,当安卓Wear通过蓝牙(以及手机)连接到互联网时,http/web呼叫以某种方式被代理。

    一种解决方案是禁用手机的蓝牙连接,这迫使Android Wear设备直接连接到您的本地wifi(如果它首先支持wifi),然后本地API请求神奇地开始工作。

    NodeApi 要检查生成的电话节点是否在附近(使用 Node.isNearby() )如果是这样,可以使用MessageClient将API请求代理到您的手机上,然后在手机上发出请求,最后回复结果。这很麻烦,但对我来说,这是调用本地API的唯一可靠方式。。。

    如果有人知道谷歌为什么通过蓝牙代理这些通话,或者有其他解决这个问题的建议,我也想知道。

        2
  •  0
  •   amrik    7 年前

    当戴与手机连接,然后使用移动互联网在这种情况下API代码不工作。请断开手机的连接,然后再试一次,它会给你回复。 请查看此链接,我认为它对您有帮助: https://www.sitepoint.com/connecting-to-web-services-with-android-wear/

        3
  •  0
  •   Liang HAN    6 年前

    adb shell dumpsys netstats

    一种解决方案是使服务器具有公共IP或主机名。另一种是使用NetworkRequest在访问API之前更改wifi的连接。请看谷歌的样本: https://github.com/googlesamples/android-WearHighBandwidthNetworking