我有一个部署的应用程序在Android 9上失败了。它的部分功能是通过接入点网络配置一个模块,以允许该模块连接到用户的家庭网络。
我的代码可以检测并连接到正确的WIFI网络,但当我尝试打开设备的套接字时,它失败了-只有在Android 9上,而且只有在启用了移动数据的情况下。如果我手动禁用设备上的移动数据,一切运行正常。
Socket open() {
Socket sock = new Socket(Proxy.NO_PROXY);
try {
sock.bind(new InetSocketAddress(localIpAddress(), 50000));
} catch (IOException e) {
activity.logContent("Warning: Failed to bind socket : " + e.toString());
}
try {
sock.connect(new InetSocketAddress("192.168.17.1", 5555), (int)5000);
} catch (IOException e) {
// This catch fires when Mobile Data is on.
activity.logContent("Connected to " + activity.mWifiManager.getConnectionInfo().getSSID());
activity.logContent("Couldn't open socket : " + e.toString());
}
return sock;
}
我在使用和不使用Proxy.NO_PROXY
以及使用和不使用bind()
调用的情况下都尝试过这种方法。如果bind调用丢失,则该错误表示套接字正试图通过cell网络进行连接。(注意:activity.logContent()
是屏幕上的日志,因此更容易看到未连接到调试器时发生的情况)。
你知道哪里出问题了吗?
发布于 2019-05-10 10:55:12
在Android9中有一个关于网络的安全配置:Android security config
在network_security_config中添加域可能会解决您的问题。我的network_security_config.xml里有这个:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">119.xxx.xxx.xxx</domain>
</domain-config>
</network-security-config>
https://stackoverflow.com/questions/56066632
复制相似问题