我实际上正在编程一些不会让用户访问android原生wifi连接页面的东西,所以我在我的android应用程序中进行编程。它扫描可用的Wifi网络,将其显示在ListView中,然后单击要连接的网络。
事实上,除了我的连接功能之外,一切都很好,这会让我的应用程序崩溃,所以如果你有任何想法。。。这是代码(这是用户输入网络通行证后调用的函数(和堆栈跟踪:
private void connectToNetwork(String networkSSID, String networkPass, String networkCapabilities) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID =networkSSID;
if (networkCapabilities.toUpperCase().contains("WEP")) { // WEP Network.
Toast.makeText(this, "WEP Network", Toast.LENGTH_SHORT).show();
wifiConfig.wepKeys[0] = networkPass;
wifiConfig.wepTxKeyIndex = 0;
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (networkCapabilities.toUpperCase().contains("WPA")) { // WPA Network
Toast.makeText(this, "WPA Network", Toast.LENGTH_SHORT).show();
wifiConfig.preSharedKey = networkPass;
} else { // OPEN Network.
Toast.makeText(this, "OPEN Network", Toast.LENGTH_SHORT).show();
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
@SuppressLint("MissingPermission")
List<WifiConfiguration> list = this.wifiManager.getConfiguredNetworks();
for (WifiConfiguration config : list) {
if (config.SSID != null && config.SSID.equals(networkSSID)) {
this.wifiManager.disconnect();
this.wifiManager.enableNetwork(config.networkId, true);
this.wifiManager.reconnect();
break;
}
}
}
堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.net.wifi.WifiManager.getConfiguredNetworks()' on a null object reference
at solutions.#####.############.WifiActivity.connectToNetwork(WifiActivity.java:148)
下面是在@LevM的帮助下现在可以工作的完整代码。还有一点自我研究。我发布它是因为我没有找到太多的解决方案来连接到互联网上安卓系统中的特定Wifi网络(SO,Youtube或其他什么(,所以如果它能在某些日子里帮助至少一个开发人员,我会很高兴的!:D(注意:我是一名学生程序员,我并不是说这个解决方案是最好的,但它至少是功能性的(
事实上,我的wifiManager没有初始化,这是一个问题。然后我删除了wifi配置列表循环,我意识到我不需要它。最后,我在SSID和密码上添加了字符串格式,因为它们需要在反斜杠("\"(中。
private boolean connectToNetwork(String networkSSID, String networkPass, String networkCapabilities) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format(""%s"", networkSSID);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
if (networkCapabilities.toUpperCase().contains("WEP")) { // WEP Network.
Toast.makeText(this, "WEP Network", Toast.LENGTH_SHORT).show();
wifiConfig.wepKeys[0] = String.format(""%s"", networkPass);
;
wifiConfig.wepTxKeyIndex = 0;
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (networkCapabilities.toUpperCase().contains("WPA")) { // WPA Network
Toast.makeText(this, "WPA Network", Toast.LENGTH_SHORT).show();
wifiConfig.preSharedKey = String.format(""%s"", networkPass);
;
} else { // OPEN Network.
Toast.makeText(this, "OPEN Network", Toast.LENGTH_SHORT).show();
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
int netId = wifiManager.addNetwork(wifiConfig);//
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
return wifiManager.reconnect();
}