/** * Configure a network, and connect to it. * * @param wifiMgr * @param scanResult * @param password Password for secure network or is ignored. * @return */ public boolean connectToNewNetwork( ScanResult scanResult, String password, int numOpenNetworksKept) { final String security = ConfigSec.getScanResultSecurity(scanResult); if (ConfigSec.isOpenNetwork(security)) { checkForExcessOpenNetworkAndSave(numOpenNetworksKept); } WifiConfiguration config = new WifiConfiguration(); config.SSID = convertToQuotedString(scanResult.SSID); config.BSSID = scanResult.BSSID; ConfigSec.setupSecurity(config, security, password); int id = -1; try { id = mWifiManager.addNetwork(config); } catch (NullPointerException e) { Log.e(TAG, "Weird!! Really!! What's wrong??", e); } if (id == -1) { return false; } if (!mWifiManager.saveConfiguration()) { return false; } config = getWifiConfiguration(config, security); if (config == null) { return false; } return connectToConfiguredNetwork(config, true); }
public WifiConfiguration getWifiConfiguration(WifiConfiguration configToFind, String security) { final String ssid = configToFind.SSID; if (ssid.length() == 0) { return null; } final String bssid = configToFind.BSSID; if (security == null) { security = ConfigSec.getWifiConfigurationSecurity(configToFind); } final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); for (final WifiConfiguration config : configurations) { if (config.SSID == null || !ssid.equals(config.SSID)) { continue; } if (config.BSSID == null || bssid == null || bssid.equals(config.BSSID)) { final String configSecurity = ConfigSec.getWifiConfigurationSecurity(config); if (security.equals(configSecurity)) { return config; } } } return null; }
public WifiConfiguration getWifiConfiguration(final ScanResult hotsopt, String hotspotSecurity) { final String ssid = convertToQuotedString(hotsopt.SSID); if (ssid.length() == 0) { return null; } final String bssid = hotsopt.BSSID; if (bssid == null) { return null; } if (hotspotSecurity == null) { hotspotSecurity = ConfigSec.getScanResultSecurity(hotsopt); } final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); if (configurations == null) { return null; } for (final WifiConfiguration config : configurations) { if (config.SSID == null || !ssid.equals(config.SSID)) { continue; } if (config.BSSID == null || bssid.equals(config.BSSID)) { final String configSecurity = ConfigSec.getWifiConfigurationSecurity(config); if (hotspotSecurity.equals(configSecurity)) { return config; } } } return null; }
/** * Change the password of an existing configured network and connect to it * * @param wifiMgr * @param config * @param newPassword * @return */ public boolean changePasswordAndConnect( WifiConfiguration config, String newPassword, int numOpenNetworksKept) { ConfigSec.setupSecurity(config, ConfigSec.getWifiConfigurationSecurity(config), newPassword); final int networkId = mWifiManager.updateNetwork(config); if (networkId == -1) { // Update failed. return false; } // Force the change to apply. mWifiManager.disconnect(); return connectToConfiguredNetwork(config, true); }
/** * Ensure no more than numOpenNetworksKept open networks in configuration list. * * @param wifiMgr * @param numOpenNetworksKept * @return Operation succeed or not. */ private boolean checkForExcessOpenNetworkAndSave(int numOpenNetworksKept) { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); sortByPriority(configurations); boolean modified = false; int tempCount = 0; for (int i = configurations.size() - 1; i >= 0; i--) { final WifiConfiguration config = configurations.get(i); if (ConfigSec.isOpenNetwork(ConfigSec.getWifiConfigurationSecurity(config))) { tempCount++; if (tempCount >= numOpenNetworksKept) { modified = true; mWifiManager.removeNetwork(config.networkId); } } } if (modified) { return mWifiManager.saveConfiguration(); } return true; }
/** * Connect to a configured network. * * @param wifiManager * @param config * @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT * @return */ public boolean connectToConfiguredNetwork(WifiConfiguration config, boolean reassociate) { final String security = ConfigSec.getWifiConfigurationSecurity(config); int oldPri = config.priority; // Make it the highest priority. int newPri = getMaxPriority() + 1; if (newPri > MAX_PRIORITY) { newPri = shiftPriorityAndSave(); config = getWifiConfiguration(config, security); if (config == null) { return false; } } // Set highest priority to this configured network config.priority = newPri; int networkId = mWifiManager.updateNetwork(config); if (networkId == -1) { return false; } // Do not disable others if (!mWifiManager.enableNetwork(networkId, false)) { config.priority = oldPri; return false; } if (!mWifiManager.saveConfiguration()) { config.priority = oldPri; return false; } // We have to retrieve the WifiConfiguration after save. config = getWifiConfiguration(config, security); if (config == null) { return false; } // ReenableAllApsWhenNetworkStateChanged.schedule(mContext); // Disable others, but do not save. // Just to force the WifiManager to connect to it. if (!mWifiManager.enableNetwork(config.networkId, true)) { return false; } final boolean connect = reassociate ? mWifiManager.reassociate() : mWifiManager.reconnect(); if (!connect) { return false; } return true; }
public class WifiAdmin { private WifiManager mWifiManager; private WifiInfo mWifiInfo; private List<ScanResult> mWifiList; private List<WifiConfiguration> mWifiConfiguration; WifiLock mWifiLock; private static WifiAdmin mInstance; private Context mContext; public static final ConfigurationSecurities ConfigSec = ConfigurationSecurities.newInstance(); private static final String TAG = "USDK_WIFI"; /** * Change the password of an existing configured network and connect to it * * @param wifiMgr * @param config * @param newPassword * @return */ public boolean changePasswordAndConnect( WifiConfiguration config, String newPassword, int numOpenNetworksKept) { ConfigSec.setupSecurity(config, ConfigSec.getWifiConfigurationSecurity(config), newPassword); final int networkId = mWifiManager.updateNetwork(config); if (networkId == -1) { // Update failed. return false; } // Force the change to apply. mWifiManager.disconnect(); return connectToConfiguredNetwork(config, true); } /** * Configure a network, and connect to it. * * @param wifiMgr * @param scanResult * @param password Password for secure network or is ignored. * @return */ public boolean connectToNewNetwork( ScanResult scanResult, String password, int numOpenNetworksKept) { final String security = ConfigSec.getScanResultSecurity(scanResult); if (ConfigSec.isOpenNetwork(security)) { checkForExcessOpenNetworkAndSave(numOpenNetworksKept); } WifiConfiguration config = new WifiConfiguration(); config.SSID = convertToQuotedString(scanResult.SSID); config.BSSID = scanResult.BSSID; ConfigSec.setupSecurity(config, security, password); int id = -1; try { id = mWifiManager.addNetwork(config); } catch (NullPointerException e) { Log.e(TAG, "Weird!! Really!! What's wrong??", e); } if (id == -1) { return false; } if (!mWifiManager.saveConfiguration()) { return false; } config = getWifiConfiguration(config, security); if (config == null) { return false; } return connectToConfiguredNetwork(config, true); } /** * Connect to a configured network. * * @param wifiManager * @param config * @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT * @return */ public boolean connectToConfiguredNetwork(WifiConfiguration config, boolean reassociate) { final String security = ConfigSec.getWifiConfigurationSecurity(config); int oldPri = config.priority; // Make it the highest priority. int newPri = getMaxPriority() + 1; if (newPri > MAX_PRIORITY) { newPri = shiftPriorityAndSave(); config = getWifiConfiguration(config, security); if (config == null) { return false; } } // Set highest priority to this configured network config.priority = newPri; int networkId = mWifiManager.updateNetwork(config); if (networkId == -1) { return false; } // Do not disable others if (!mWifiManager.enableNetwork(networkId, false)) { config.priority = oldPri; return false; } if (!mWifiManager.saveConfiguration()) { config.priority = oldPri; return false; } // We have to retrieve the WifiConfiguration after save. config = getWifiConfiguration(config, security); if (config == null) { return false; } // ReenableAllApsWhenNetworkStateChanged.schedule(mContext); // Disable others, but do not save. // Just to force the WifiManager to connect to it. if (!mWifiManager.enableNetwork(config.networkId, true)) { return false; } final boolean connect = reassociate ? mWifiManager.reassociate() : mWifiManager.reconnect(); if (!connect) { return false; } return true; } private void sortByPriority(List<WifiConfiguration> configurations) { java.util.Collections.sort( configurations, new Comparator<WifiConfiguration>() { public int compare(WifiConfiguration object1, WifiConfiguration object2) { return object1.priority - object2.priority; } }); } /** * Ensure no more than numOpenNetworksKept open networks in configuration list. * * @param wifiMgr * @param numOpenNetworksKept * @return Operation succeed or not. */ private boolean checkForExcessOpenNetworkAndSave(int numOpenNetworksKept) { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); sortByPriority(configurations); boolean modified = false; int tempCount = 0; for (int i = configurations.size() - 1; i >= 0; i--) { final WifiConfiguration config = configurations.get(i); if (ConfigSec.isOpenNetwork(ConfigSec.getWifiConfigurationSecurity(config))) { tempCount++; if (tempCount >= numOpenNetworksKept) { modified = true; mWifiManager.removeNetwork(config.networkId); } } } if (modified) { return mWifiManager.saveConfiguration(); } return true; } private static final int MAX_PRIORITY = 99999; private int shiftPriorityAndSave() { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); sortByPriority(configurations); final int size = configurations.size(); for (int i = 0; i < size; i++) { final WifiConfiguration config = configurations.get(i); config.priority = i; mWifiManager.updateNetwork(config); } mWifiManager.saveConfiguration(); return size; } private int getMaxPriority() { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); int pri = 0; for (final WifiConfiguration config : configurations) { if (config.priority > pri) { pri = config.priority; } } return pri; } public WifiConfiguration getWifiConfiguration(final ScanResult hotsopt, String hotspotSecurity) { final String ssid = convertToQuotedString(hotsopt.SSID); if (ssid.length() == 0) { return null; } final String bssid = hotsopt.BSSID; if (bssid == null) { return null; } if (hotspotSecurity == null) { hotspotSecurity = ConfigSec.getScanResultSecurity(hotsopt); } final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); if (configurations == null) { return null; } for (final WifiConfiguration config : configurations) { if (config.SSID == null || !ssid.equals(config.SSID)) { continue; } if (config.BSSID == null || bssid.equals(config.BSSID)) { final String configSecurity = ConfigSec.getWifiConfigurationSecurity(config); if (hotspotSecurity.equals(configSecurity)) { return config; } } } return null; } public WifiConfiguration getWifiConfiguration(WifiConfiguration configToFind, String security) { final String ssid = configToFind.SSID; if (ssid.length() == 0) { return null; } final String bssid = configToFind.BSSID; if (security == null) { security = ConfigSec.getWifiConfigurationSecurity(configToFind); } final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); for (final WifiConfiguration config : configurations) { if (config.SSID == null || !ssid.equals(config.SSID)) { continue; } if (config.BSSID == null || bssid == null || bssid.equals(config.BSSID)) { final String configSecurity = ConfigSec.getWifiConfigurationSecurity(config); if (security.equals(configSecurity)) { return config; } } } return null; } public static String convertToQuotedString(String string) { if (TextUtils.isEmpty(string)) { return ""; } final int lastPos = string.length() - 1; if (lastPos > 0 && (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) { return string; } return "\"" + string + "\""; } // ������ private WifiAdmin(Context context) { // ȡ��WifiManager���� mContext = context; mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); } public static WifiAdmin getInstance(Context context) { if (mInstance == null) { mInstance = new WifiAdmin(context); } return mInstance; } // ��WIFI public void openWifi() { if (!mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(true); } } // �ر�WIFI public void closeWifi() { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); } } // ��鵱ǰWIFI״̬ public int checkState() { return mWifiManager.getWifiState(); } // ��WifiLock public void acquireWifiLock() { mWifiLock.acquire(); } // ����WifiLock public void releaseWifiLock() { // �ж�ʱ���� if (mWifiLock.isHeld()) { mWifiLock.acquire(); } } // ����һ��WifiLock public void creatWifiLock() { mWifiLock = mWifiManager.createWifiLock("Test"); } // �õ����úõ����� public List<WifiConfiguration> getConfiguration() { return mWifiConfiguration; } // ָ�����úõ������������ public void connectConfiguration(int index) { // ����������úõ���������� if (index > mWifiConfiguration.size()) { return; } // �������úõ�ָ��ID������ mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true); } public void startScan() { mWifiManager.startScan(); // �õ�ɨ���� mWifiList = mWifiManager.getScanResults(); // �õ����úõ��������� mWifiConfiguration = mWifiManager.getConfiguredNetworks(); } // �õ������б� public List<ScanResult> getWifiList() { startScan(); return mWifiList; } public List<ScanResult> getScanResult() { return mWifiManager.getScanResults(); } // �鿴ɨ���� public StringBuilder lookUpScan() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < mWifiList.size(); i++) { stringBuilder.append("Index_" + new Integer(i + 1).toString() + ":"); // ��ScanResult��Ϣת����һ���ַ�� // ���аѰ�����BSSID��SSID��capabilities��frequency��level stringBuilder.append((mWifiList.get(i)).toString()); stringBuilder.append("/n"); } return stringBuilder; } // �õ�MAC��ַ public String getMacAddress() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress(); } // �õ�������BSSID public String getBSSID() { return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID(); } // �õ�IP��ַ public int getIPAddress() { return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress(); } // �õ����ӵ�ID public int getNetworkId() { return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId(); } // �õ�WifiInfo��������Ϣ�� public String getWifiInfo() { // ȡ��WifiInfo���� mWifiInfo = mWifiManager.getConnectionInfo(); return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString(); } public WifiInfo getCurrentWifiInfo() { // ȡ��WifiInfo���� return mWifiManager.getConnectionInfo(); } // ���һ�����粢���� public Boolean addNetwork(WifiConfiguration wcg) { int wcgID = mWifiManager.addNetwork(wcg); return mWifiManager.enableNetwork(wcgID, true); } // �Ͽ�ָ��ID������ public void disconnectWifi(int netId) { mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); } private WifiConfiguration IsExsits(String SSID) { List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; } }