gpt4 book ai didi

android - wifiManager.addNetwork 失败

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:54 31 4
gpt4 key购买 nike

我有一台服务器,它是另一台安卓设备。服务器启动 wifi 热点。在我的客户端上,我尝试自动连接到热点。

我在 onStart() 的新线程中调用此函数:

private void enableWifi(){
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
Log.i("Wifi", "Wifi enabled");
}
if(getNetworkId() == -1){
addNetwork();
}
while(!isConnected(context)){
wifiManager.disconnect();
wifiManager.enableNetwork(getNetworkId(),true);
wifiManager.reconnect();
Log.i("Wifi", "Connecting to Hotspot");
try {
Thread.sleep(TIMEOUT);
} catch (InterruptedException e) {
Log.e("Wifi", "Thread interrupted: " + e);
}
}

Log.i("Wifi", "Connected to Hotspot");
}

private int getNetworkId(){
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + wifiName + "\"")) {
return i.networkId;
}
}
return -1;
}

private void addNetwork(){
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\\" + wifiName + "\\";
wifiConfiguration.preSharedKey = "\\" + wifiPassword + "\\";
wifiManager.addNetwork(wifiConfiguration);
Log.i("Wifi", "Added Hotspot to configured Networks");
}

wifiManager.addNetwork() 返回 -1

最佳答案

您的问题似乎是您在 SSID 和密码中使用了反斜杠而不是引号。

试试这个:

wifiConfiguration.SSID = "\"" + wifiName + "\"";
wifiConfiguration.preSharedKey = "\"" + wifiPassword + "\"";

关于android - wifiManager.addNetwork 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021699/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com