大佬教程收集整理的这篇文章主要介绍了java-获取WiFi的SSID而不连接它?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在应用程序中连接到SSID wifi网络.
码:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
但是问题是我不知道网络的SSID.如何在不连接的情况下获取WiFi网络的SSID?
解决方法:
如果您想获得所有可用的wifi:
List<ScanResult> mScanResults = mWifiManager.getScanResults();
如果要连接wifi ssid:
WifiManager wifiManager = (WifiManager) getSystemservice(WIFI_serviCE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("wifiInfo", wifiInfo.toString());
Log.d("SSID",wifiInfo.getSSID());
如果您想添加新的wifi设置,我已在下面编写了演示应用程序:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceStatE) {
super.onCreate(savedInstanceStatE);
setContentView(R.layout.activity_main);
connectToAP("12345", "12345");
WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager) getSystemservice(MainActivity.WIFI_serviCE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == 13) {
wifiConf = conf;
try {
setIpAssignment("STATIC", wifiConf); // or "DHCP" for
// dynamic setTing
setIpaddress(InetAddress.getByName("192.168.0.100"), 24,
wifiConf);
setGateway(InetAddress.getByName("4.4.4.4"), wifiConf);
setDNS(InetAddress.getByName("4.4.4.4"), wifiConf);
wifiManager.updateNetwork(wifiConf); // apply the setTing
wifiManager.saveConfiguration(); // Save it
} catch (Exception E) {
e.printStackTrace();
}
break;
}
}
}
public static void setIpAssignment(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentexception,
NoSuchFieldException, illegalaccessexception {
setEnumField(wifiConf, assign, "ipAssignment");
}
public static void setEnumField(Object obj, String value, String Name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentexception, illegalaccessexception {
Field f = obj.getClass().getField(Name);
f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
}
public static void setIpaddress(InetAddress addr, int prefixLength,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentexception, NoSuchFieldException,
illegalaccessexception, NoSuchMethodException,
ClassnotFoundException, InstantiationException,
InvocationTargetException {
Object linkProperties = getField(wifiConf, "linkProperties");
if (linkProperties == null)
return;
Class laClass = Class.forName("android.net.LinkAddress");
Constructor laConstructor = laClass.getConstructor(new Class[] {
InetAddress.class, int.class });
Object linkAddress = laConstructor.newInstance(addr, prefixLength);
ArrayList mLinkAddresses = (ArrayList) getDeclaredField(linkProperties,
"mLinkAddresses");
mLinkAddresses.clear();
mLinkAddresses.add(linkAddress);
}
public static void setGateway(InetAddress gateway,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentexception, NoSuchFieldException,
illegalaccessexception, ClassnotFoundException,
NoSuchMethodException, InstantiationException,
InvocationTargetException {
Object linkProperties = getField(wifiConf, "linkProperties");
if (linkProperties == null)
return;
Class routeInfoClass = Class.forName("android.net.RouteInfo");
Constructor routeInfoConstructor = routeInfoClass
.getConstructor(new Class[] { InetAddress.class });
Object routeInfo = routeInfoConstructor.newInstance(gateway);
ArrayList mRoutes = (ArrayList) getDeclaredField(linkProperties,
"mRoutes");
mRoutes.clear();
mRoutes.add(routeInfo);
}
public static void setDNS(InetAddress dns, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentexception,
NoSuchFieldException, illegalaccessexception {
Object linkProperties = getField(wifiConf, "linkProperties");
if (linkProperties == null)
return;
ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) getDeclaredField(
linkProperties, "mDnses");
mDnses.clear(); // or add a new dns address , here I just want to
// replace DNS1
mDnses.add(dns);
}
public static Object getField(Object obj, String Name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentexception, illegalaccessexception {
Field f = obj.getClass().getField(Name);
Object out = f.get(obj);
return out;
}
public static Object getDeclaredField(Object obj, String Name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentexception, illegalaccessexception {
Field f = obj.getClass().getDeclaredField(Name);
f.setAccessible(true);
Object out = f.get(obj);
return out;
}
@Override
public Boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public Boolean onOptionsItemSELEcted(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_setTings) {
return true;
}
return super.onOptionsItemSELEcted(item);
}
String TAG = "wifi";
WifiManager wifiManager;
public void connectToAP(String ssid, String passkey) {
Log.i(tag, "* connectToAP");
wifiManager = (WifiManager) getSystemservice(WIFI_serviCE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
String networkSSID = ssid;
String networkPass = passkey;
Log.d(tag, "# password " + networkPass);
// for (ScanResult result : scanResultList) {
// if (result.SSID.equals(networkSSID)) {
if (true) {
// String s@L_170_15@mode = getScanResultSecurity(result);
String s@L_170_15@mode = "WEP";
if (s@L_170_15@mode.equalsIgnoreCase("OPEN")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.allowedKeymanagement
.set(WifiConfiguration.Keymgmt.NONE);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(tag, "# add Network returned " + res);
Boolean b = wifiManager.enableNetwork(res, truE);
Log.d(tag, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
} else if (s@L_170_15@mode.equalsIgnoreCase("WEP")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
wifiConfiguration.wepTxKeyIndex = 0;
wifiConfiguration.allowedKeymanagement
.set(WifiConfiguration.Keymgmt.NONE);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(tag, "### 1 ### add Network returned " + res);
Boolean b = wifiManager.enableNetwork(res, truE);
Log.d(tag, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
}
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.TKIp);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.CCMp);
wifiConfiguration.allowedKeymanagement
.set(WifiConfiguration.Keymgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIp);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMp);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(tag, "### 2 ### add Network returned " + res);
wifiManager.enableNetwork(res, truE);
Boolean changeHappen = wifiManager.saveConfiguration();
if (res != -1 && changeHappen) {
Log.d(tag, "### Change happen");
// AppStaticVar.connectedSsidName = networkSSID;
} else {
Log.d(tag, "*** Change NOT happen");
}
wifiManager.setWifiEnabled(true);
}
// }
}
public String getScanResultSecurity(ScanResult scanResult) {
Log.i(tag, "* getScanResultSecurity");
final String cap = scanResult.capabilities;
final String[] s@L_170_15@modes = { "WEP", "PSK", "EAP" };
for (int i = s@L_170_15@modes.length - 1; i >= 0; i--) {
if (cap.contains(s@L_170_15@modes[i])) {
return s@L_170_15@modes[i];
}
}
return "OPEN";
}
}
以上是大佬教程为你收集整理的java-获取WiFi的SSID而不连接它?全部内容,希望文章能够帮你解决java-获取WiFi的SSID而不连接它?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。