-1
我一直試圖通過使用WifiManager類連接到在另一個設備上創建的HotSpot。我已經完成並使用了這個answer的代碼。所以最後我的代碼看起來是這樣的:無法以編程方式連接到WIFI網絡
for(ScanResult i : list) {
Toast.makeText(context, i.SSID, Toast.LENGTH_SHORT).show();
if(i.SSID != null && (i.SSID.startsWith("SMSKCM877-") && i.SSID.endsWith("-MSDBP2016"))) {
// Toast.makeText(context, "Found Match", Toast.LENGTH_SHORT);
conf.SSID="\""+i.SSID+"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
int networkID=wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(networkID, true);
wifiManager.reconnect();
String ipStr="";
DhcpInfo sinfo=wifiManager.getDhcpInfo();
int ip=sinfo.serverAddress;
ipStr = String.format("%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
Log.e("sg", ipStr);
Toast.makeText(context, ipStr+"", Toast.LENGTH_LONG).show();
startConnect s=new startConnect(ipStr,context);
s.execute();
return true;
}
}
但我面臨的問題是,當我試圖連接的第一次產生的ipstr
始終是0.0.0.0。重試幾次給了我正確的地址,之後一切順利。任何想法爲什麼會出現此問題?
在此先感謝。