java.net.ConnectException after switching accesspoint using WifiNetworkSpecifier on Android 10

[亡魂溺海] 提交于 2021-02-10 21:26:08

问题


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) at libcore.io.IoBridge.connect(IoBridge.java:143) at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436) at java.net.Socket.connect(Socket.java:621) at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.kt:58) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:268) ... 21 more Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable) at libcore.io.Linux.connect(Native Method) at libcore.io.ForwardingOs.connect(ForwardingOs.java:95) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:136) at libcore.io.ForwardingOs.connect(ForwardingOs.java:95) at libcore.io.IoBridge.connectErrno(IoBridge.java:174) at libcore.io.IoBridge.connect(IoBridge.java:135) ... 29 more

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..
    }

})

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.


回答1:


I had the same problem now.

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

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


来源:https://stackoverflow.com/questions/58604762/java-net-connectexception-after-switching-accesspoint-using-wifinetworkspecifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!