Android
Wi-Fi-anslutningar
Sök…
Anslut med WEP-kryptering
Det här exemplet ansluts till en Wi-Fi-åtkomstpunkt med WEP-kryptering, givet en SSID och lösenord.
public boolean ConnectToNetworkWEP(String networkSSID, String password)
{
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain SSID in quotes
conf.wepKeys[0] = "\"" + password + "\""; //Try it with quotes first
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.AuthAlgorithm.OPEN);
conf.allowedGroupCiphers.set(WifiConfiguration.AuthAlgorithm.SHARED);
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.addNetwork(conf);
if (networkId == -1){
//Try it again with no quotes in case of hex password
conf.wepKeys[0] = password;
networkId = wifiManager.addNetwork(conf);
}
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
//WiFi Connection success, return true
return true;
} catch (Exception ex) {
System.out.println(Arrays.toString(ex.getStackTrace()));
return false;
}
}
Anslut med WPA2-kryptering
Det här exemplet ansluts till en Wi-Fi-åtkomstpunkt med WPA2-kryptering.
public boolean ConnectToNetworkWPA(String networkSSID, String password) {
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain SSID in quotes
conf.preSharedKey = "\"" + password + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
Log.d("connecting", conf.SSID + " " + conf.preSharedKey);
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
Log.d("after connecting", conf.SSID + " " + conf.preSharedKey);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
Log.d("re connecting", i.SSID + " " + conf.preSharedKey);
break;
}
}
//WiFi Connection success, return true
return true;
} catch (Exception ex) {
System.out.println(Arrays.toString(ex.getStackTrace()));
return false;
}
}
Sök efter åtkomstpunkter
Detta exempel söker efter tillgängliga åtkomstpunkter och ad hoc-nätverk. btnScan
aktiverar en skanning initierad med WifiManager.startScan()
-metoden. Efter skanningen WifiManager
SCAN_RESULTS_AVAILABLE_ACTION
avsikten SCAN_RESULTS_AVAILABLE_ACTION
och klassen WifiScanReceiver
bearbetar skanningsresultatet. Resultaten visas i en TextView
.
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
TextView txtWifiInfo;
WifiManager wifi;
WifiScanReceiver wifiReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
txtWifiInfo = (TextView)findViewById(R.id.txtWifiInfo);
Button btnScan = (Button)findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "Start scan...");
wifi.startScan();
}
});
}
protected void onPause() {
unregisterReceiver(wifiReceiver);
super.onPause();
}
protected void onResume() {
registerReceiver(
wifiReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
);
super.onResume();
}
private class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifi.getScanResults();
txtWifiInfo.setText("");
for(int i = 0; i < wifiScanList.size(); i++){
String info = ((wifiScanList.get(i)).toString());
txtWifiInfo.append(info+"\n\n");
}
}
}
}
Behörigheter
Följande behörigheter måste definieras i AndroidManifest.xml :
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
android.permission.ACCESS_WIFI_STATE
är nödvändig för att ringa WifiManager.getScanResults()
. Utan android.permission.CHANGE_WIFI_STATE
du inte initiera en skanning med WifiManager.startScan()
.
När du sammanställer projektet för api nivå 23 eller högre (Android 6.0 och senare) android.permission.ACCESS_FINE_LOCATION
antingen android.permission.ACCESS_FINE_LOCATION
eller android.permission.ACCESS_COARSE_LOCATION
sättas in. Dessutom behöver tillstånd begäras, t.ex. i onCreate
metoden för din huvudaktivitet:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] PERMS_INITIAL={
Manifest.permission.ACCESS_FINE_LOCATION,
};
ActivityCompat.requestPermissions(this, PERMS_INITIAL, 127);
}