在 Android 10 上使用 WifiNetworkSpecifier 切换接入点后出现 java.net.ConnectException [英] java.net.ConnectException after switching accesspoint using WifiNetworkSpecifier on Android 10

查看:281
本文介绍了在 Android 10 上使用 WifiNetworkSpecifier 切换接入点后出现 java.net.ConnectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 WifiNetworkSpecifier 配置 wifi 接入点后无法建立连接,以下是跟踪

Unable to make connection after configuring wifi accespoint using WifiNetworkSpecifier, Below is the trace

Caused by: java.net.ConnectException: failed to connect to/10.123.45.1 (port 443) from/:: (port 0) after 120000ms: connect failed: ENETUNREACH (Network is unreachable)在 libcore.io.IoBridge.connect(IoBridge.java:143)在 java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)在 java.net.Socket.connect(Socket.java:621)在 okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.kt:58)在 okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:268)……还有 21 个引起:android.system.ErrnoException:连接失败:ENETUNREACH(网络无法访问)在 libcore.io.Linux.connect(本机方法)在 libcore.io.ForwardingOs.connect(ForwardingOs.java:95)在 libcore.io.BlockGuardOs.connect(BlockGuardOs.java:136)在 libcore.io.ForwardingOs.connect(ForwardingOs.java:95)在 libcore.io.IoBridge.connectErrno(IoBridge.java:174)在 libcore.io.IoBridge.connect(IoBridge.java:135)... 29个

在使用 wifiManager 连接我的 IOT 设备时.我开始收到针对 Android 10 的 App sdk 连接错误.最初,我开始使用 WifiNetworkSpecifier 连接到 SSID,如下所示,

While using the wifiManager to connect my IOT device. I started receiving a connection error for App sdk targeting to Android 10. Initially, I started connect to SSID using WifiNetworkSpecifier as below,

val specifier = WifiNetworkSpecifier.Builder()
            .setSsid(ssid)
            .build()
val request = NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
            .setNetworkSpecifier(specifier)
            .build()

    connManager.requestNetwork(request, object : ConnectivityManager.NetworkCallback() {

    override fun onAvailable(network: Network?) {
         startLocalConnection(network)
    }

    override fun onUnavailable() {
        // do failure processing here..
    }

})

在 onAvailable() 回调中连接到 SSID 后,使用如下所示的自签名证书与我的本地 IOT 设备 http 服务器建立 okhttp 连接,

after connection to the SSID in onAvailable() callback makes an okhttp connection to my local IOT device http server using self-signed certificate as below,

fun startLocalConnection(){
    val trusted = KeyStore.getInstance("BKS")
    val rawResource = context.resources.openRawResource(R.raw.device)
    trusted.load(rawResource, "password")
    val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
    trustManagerFactory.init(trusted)
    val trustManager: X509TrustManager = trustManagerFactory.trustManagers
    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(null, trustManagers, null)
    val socketFactory: SSLSocketFactory = sslContext.socketFactory
    val trustManager: X509TrustManager = trustManagers[0] as X509TrustManager

    var okHttpClient: OkHttpClient = OkHttpClient.Builder()
            .connectionSpecs(listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
            .build()

    val request = Request.Builder()
            .url("https://192.168.40.23/device/json")
            .build()

    okHttpClient.newCall(request).enqueue(object : Callback {
        override fun onResponse(call: Call, response: Response) {
            // Success
        }

        override fun onFailure(call: Call, e: IOException) {
            // Causing java.net.ConnectException: Failed to connect to /192.168.40.23
        }
   }}

请帮助我追踪并解决问题.

Please help me to trace and fix the issue.

推荐答案

我现在遇到了同样的问题.

I had the same problem now.

在您的 onAvailable 回调中,您必须将您的网络与您的设备绑定:

In your onAvailable callback, you have to bind your network with your device with:

((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))
                .bindProcessToNetwork(network);

这篇关于在 Android 10 上使用 WifiNetworkSpecifier 切换接入点后出现 java.net.ConnectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆