使用 java 连接到 adhoc wifi 或手动连接的区别
Difference between using java to connect to an adhoc wifi or do it manually
我遇到的奇怪问题。
我正在开发一个应用程序,可以从相机连接到无线临时网络。
(所以wifi中没有网络连接)。
这是我使用的 java 代码,能够自动将 Android phone 连接到相机 wifi:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
LocalBroadcastManager.getInstance(context).registerReceiver(mWifiScanReceiver, new IntentFilter(TAG_WIFI_CONNECTED));
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.contains(networkSSID)) {
if (old_networkId != i.networkId && wifiManager.getConnectionInfo() != null)
old_networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.disconnect();
sicilia_ssid = networkSSID;
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
此代码有效:设备确实连接到我想要的 wifi,但是:
问题来了
我正在使用特定的 API 与相机对话。
如果我使用此代码连接到 wifi,API 将不起作用。但如果我手动连接到 wifi(使用设置应用程序),就没有问题......
如果我禁用蜂窝数据,我可以使用代码自动连接到相机。
所以看起来蜂窝数据优先于 adhoc wifi 但只有当连接是在 java 代码中建立时...
那么这两种方式有什么区别呢?
我需要添加更多代码吗?
非常感谢!
我找到了解决方案 here :
"From Lollipop onwards the OS became a little more intelligent, allowing multiple network connections and not routing data to networks that don’t have Internet connectivity"
所以我在建立连接后添加了这段代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network current_network = null;
for (Network n : cm.getAllNetworks())
{
if (cm.getNetworkInfo(n).getTypeName().equals("WIFI")) {
current_network = n;
break;
}
}
if (current_network != null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
cm.bindProcessToNetwork(current_network);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager.setProcessDefaultNetwork(current_network);
}
}
}
我遇到的奇怪问题。
我正在开发一个应用程序,可以从相机连接到无线临时网络。 (所以wifi中没有网络连接)。
这是我使用的 java 代码,能够自动将 Android phone 连接到相机 wifi:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
LocalBroadcastManager.getInstance(context).registerReceiver(mWifiScanReceiver, new IntentFilter(TAG_WIFI_CONNECTED));
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.contains(networkSSID)) {
if (old_networkId != i.networkId && wifiManager.getConnectionInfo() != null)
old_networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.disconnect();
sicilia_ssid = networkSSID;
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
此代码有效:设备确实连接到我想要的 wifi,但是:
问题来了
我正在使用特定的 API 与相机对话。 如果我使用此代码连接到 wifi,API 将不起作用。但如果我手动连接到 wifi(使用设置应用程序),就没有问题...... 如果我禁用蜂窝数据,我可以使用代码自动连接到相机。 所以看起来蜂窝数据优先于 adhoc wifi 但只有当连接是在 java 代码中建立时...
那么这两种方式有什么区别呢? 我需要添加更多代码吗? 非常感谢!
我找到了解决方案 here :
"From Lollipop onwards the OS became a little more intelligent, allowing multiple network connections and not routing data to networks that don’t have Internet connectivity"
所以我在建立连接后添加了这段代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network current_network = null;
for (Network n : cm.getAllNetworks())
{
if (cm.getNetworkInfo(n).getTypeName().equals("WIFI")) {
current_network = n;
break;
}
}
if (current_network != null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
cm.bindProcessToNetwork(current_network);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager.setProcessDefaultNetwork(current_network);
}
}
}