使用 WifiNetworkSpecifier 配置 wifi 接入点后无法建立连接,以下是跟踪
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
在使用 wifiManager 连接我的 IOT 设备时。我开始收到针对 Android 10 的 App sdk 的连接错误。最初,我开始使用 WifiNetworkSpecifier 连接到 SSID,如下所示,
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 连接,如下所示,
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
}
}}
请帮助我追踪并解决问题。