我有一个360 Insta Camera,它使用OKHttp
与WIFI
连接到android手机,并且应该连接到Local Ip (192.....)
!
问题:
当移动数据和wifi都启用时,在android 11和12中,当用户启用Automatic Switch connection
选项时,操作系统为Mobile Data
设置了优先级,问题发生了!摄像头无法连接到手机,因为当设备有互联网时,它会在网络中查找IP!
如何使用OKHttp
解决此问题?
您可以使用类似以下的东西来固定网络
OkHttp路由流量抛出wifi和SocketFactory
final Network[] networks = connectivityManager.getAllNetworks();
for (final Network network : networks) {
final NetworkInfo netInfo = connectivityManager.getNetworkInfo(network);
if (netInfo.getType() == ConnectivityManager.TYPE_WIFI && netInfo.getState() == NetworkInfo.State.CONNECTED) {
// just for example HttpURLConnection works as expected and route traffic throw wifi
connection = (HttpURLConnection) network.openConnection(url);
// not works since >=3.5.0, route traffic in same way
builder.socketFactory(network.getSocketFactory());
}
}