2016年6月24日 星期五

wifi app source code


MainActivity.java

package com.farproc.wifi.connecter;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Floating {
public static final String EXTRA_HOTSPOT = "com.farproc.wifi.connecter.extra.HOTSPOT";
private ScanResult mScanResult;
private Floating.Content mContent;
private WifiManager mWifiManager;
@Override
protected void onNewIntent (Intent intent) {
setIntent(intent);
// This activity has "singleInstance" launch mode.
// Update content to reflect the newest intent.
doNewIntent(intent);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
doNewIntent(getIntent());
}
private boolean isAdHoc(final ScanResult scanResule) {
return scanResule.capabilities.indexOf("IBSS") != -1;
}
private void doNewIntent(final Intent intent) {
mScanResult = intent.getParcelableExtra(EXTRA_HOTSPOT);
if(mScanResult == null) {
Toast.makeText(this, "No data in Intent!", Toast.LENGTH_LONG).show();
finish();
return;
}
if(isAdHoc(mScanResult)) {
Toast.makeText(this, R.string.adhoc_not_supported_yet, Toast.LENGTH_LONG).show();
finish();
return;
}
final String security = Wifi.ConfigSec.getScanResultSecurity(mScanResult);
final WifiConfiguration config = Wifi.getWifiConfiguration(mWifiManager, mScanResult, security);
if(config == null) {
mContent = new NewNetworkContent(this, mWifiManager, mScanResult);
} else {
final boolean isCurrentNetwork_ConfigurationStatus = config.status == WifiConfiguration.Status.CURRENT;
final WifiInfo info = mWifiManager.getConnectionInfo();
final boolean isCurrentNetwork_WifiInfo = info != null
&& android.text.TextUtils.equals(info.getSSID(), mScanResult.SSID)
&& android.text.TextUtils.equals(info.getBSSID(), mScanResult.BSSID);
if(isCurrentNetwork_ConfigurationStatus || isCurrentNetwork_WifiInfo) {
mContent = new CurrentNetworkContent(this, mWifiManager, mScanResult);
} else {
mContent = new ConfiguredNetworkContent(this, mWifiManager, mScanResult);
}
}
setContent(mContent);
}
}


=================================== wifi.java package com.farproc.wifi.connecter;
import java.util.Comparator;
import java.util.List;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
public class Wifi {
public static final ConfigurationSecurities ConfigSec = ConfigurationSecurities.newInstance();
private static final String TAG = "Wifi Connecter";
/**
* Change the password of an existing configured network and connect to it
* @param wifiMgr
* @param config
* @param newPassword
* @return
*/
public static boolean changePasswordAndConnect(final Context ctx, final WifiManager wifiMgr, final WifiConfiguration config, final String newPassword, final int numOpenNetworksKept) {
ConfigSec.setupSecurity(config, ConfigSec.getWifiConfigurationSecurity(config), newPassword);
final int networkId = wifiMgr.updateNetwork(config);
if(networkId == -1) {
// Update failed.
return false;
}
// Force the change to apply.
wifiMgr.disconnect();
return connectToConfiguredNetwork(ctx, wifiMgr, config, true);
}
/**
* Configure a network, and connect to it.
* @param wifiMgr
* @param scanResult
* @param password Password for secure network or is ignored.
* @return
*/
public static boolean connectToNewNetwork(final Context ctx, final WifiManager wifiMgr, final ScanResult scanResult, final String password, final int numOpenNetworksKept) {
final String security = ConfigSec.getScanResultSecurity(scanResult);
if(ConfigSec.isOpenNetwork(security)) {
checkForExcessOpenNetworkAndSave(wifiMgr, numOpenNetworksKept);
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = convertToQuotedString(scanResult.SSID);
config.BSSID = scanResult.BSSID;
ConfigSec.setupSecurity(config, security, password);
int id = -1;
try {
id = wifiMgr.addNetwork(config);
} catch(NullPointerException e) {
Log.e(TAG, "Weird!! Really!! What's wrong??", e);
// Weird!! Really!!
// This exception is reported by user to Android Developer Console(https://market.android.com/publish/Home)
}
if(id == -1) {
return false;
}
if(!wifiMgr.saveConfiguration()) {
return false;
}
config = getWifiConfiguration(wifiMgr, config, security);
if(config == null) {
return false;
}
return connectToConfiguredNetwork(ctx, wifiMgr, config, true);
}
/**
* Connect to a configured network.
* @param wifiManager
* @param config
* @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT
* @return
*/
public static boolean connectToConfiguredNetwork(final Context ctx, final WifiManager wifiMgr, WifiConfiguration config, boolean reassociate) {
final String security = ConfigSec.getWifiConfigurationSecurity(config);
int oldPri = config.priority;
// Make it the highest priority.
int newPri = getMaxPriority(wifiMgr) + 1;
if(newPri > MAX_PRIORITY) {
newPri = shiftPriorityAndSave(wifiMgr);
config = getWifiConfiguration(wifiMgr, config, security);
if(config == null) {
return false;
}
}
// Set highest priority to this configured network
config.priority = newPri;
int networkId = wifiMgr.updateNetwork(config);
if(networkId == -1) {
return false;
}
// Do not disable others
if(!wifiMgr.enableNetwork(networkId, false)) {
config.priority = oldPri;
return false;
}
if(!wifiMgr.saveConfiguration()) {
config.priority = oldPri;
return false;
}
// We have to retrieve the WifiConfiguration after save.
config = getWifiConfiguration(wifiMgr, config, security);
if(config == null) {
return false;
}
ReenableAllApsWhenNetworkStateChanged.schedule(ctx);
// Disable others, but do not save.
// Just to force the WifiManager to connect to it.
if(!wifiMgr.enableNetwork(config.networkId, true)) {
return false;
}
final boolean connect = reassociate ? wifiMgr.reassociate() : wifiMgr.reconnect();
if(!connect) {
return false;
}
return true;
}
private static void sortByPriority(final List<WifiConfiguration> configurations) {
java.util.Collections.sort(configurations, new Comparator<WifiConfiguration>() {
@Override
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 static boolean checkForExcessOpenNetworkAndSave(final WifiManager wifiMgr, final int numOpenNetworksKept) {
final List<WifiConfiguration> configurations = wifiMgr.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;
wifiMgr.removeNetwork(config.networkId);
}
}
}
if(modified) {
return wifiMgr.saveConfiguration();
}
return true;
}
private static final int MAX_PRIORITY = 99999;
private static int shiftPriorityAndSave(final WifiManager wifiMgr) {
final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
sortByPriority(configurations);
final int size = configurations.size();
for(int i = 0; i < size; i++) {
final WifiConfiguration config = configurations.get(i);
config.priority = i;
wifiMgr.updateNetwork(config);
}
wifiMgr.saveConfiguration();
return size;
}
private static int getMaxPriority(final WifiManager wifiManager) {
final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
int pri = 0;
for(final WifiConfiguration config : configurations) {
if(config.priority > pri) {
pri = config.priority;
}
}
return pri;
}
private static final String BSSID_ANY = "any";
public static WifiConfiguration getWifiConfiguration(final WifiManager wifiMgr, 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 = wifiMgr.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_ANY.equals(config.BSSID) || bssid.equals(config.BSSID)) {
final String configSecurity = ConfigSec.getWifiConfigurationSecurity(config);
if(hotspotSecurity.equals(configSecurity)) {
return config;
}
}
}
return null;
}
public static WifiConfiguration getWifiConfiguration(final WifiManager wifiMgr, final 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 = wifiMgr.getConfiguredNetworks();
for(final WifiConfiguration config : configurations) {
if(config.SSID == null || !ssid.equals(config.SSID)) {
continue;
}
if(config.BSSID == null || BSSID_ANY.equals(config.BSSID) || 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 + "\"";
}
}

Ref : https://github.com/mkch/android-wifi-connecter/blob/master/src/com/farproc/wifi/connecter/MainActivity.java

沒有留言:

張貼留言