Skip to content

Instantly share code, notes, and snippets.

@CoffeeCode
Created March 13, 2014 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoffeeCode/9529503 to your computer and use it in GitHub Desktop.
Save CoffeeCode/9529503 to your computer and use it in GitHub Desktop.
package com.ap.wificam.wifiservice;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.ScanResult;
import android.os.Handler;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.ap.wificam.util.DisplayToast;
import java.util.List;
/**
* Created by AND1 on 23.01.14.
*/
public class ScanNetworks extends IntentService {
private Boolean run;
private WifiManager manager;
private WifiConfiguration configuration;
private List<ScanResult> results;
String ssid;
String psk;
Handler mHandler;
// No-arg constructor is required
public ScanNetworks(){
super("ScanNetworks");
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("and1", "Service Wifi started onHandleIntent");
run = true;
manager = (WifiManager) getSystemService(WIFI_SERVICE);
configuration = new WifiConfiguration();
Context context = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
ssid = prefs.getString("ssid", "default_no");
psk = prefs.getString("psk", "default_no");
Log.d("and1 : ", ssid);
Log.d("and1 : ", psk);
Boolean networkFound = false;
while(!networkFound && run ){
if(scanNetworks()){
if(networkExists((ssid))){
connectTo(ssid, psk);
networkFound = true;
}
else{
SystemClock.sleep(5000);
mHandler.post(new DisplayToast(this, "WiFi not Found Waiting!"));
}
}
}
//wait for network and start downloadservice
try{
while(!isConnectedTo(ssid) && run){
mHandler.post(new DisplayToast(this, "not connected waiting!"));
SystemClock.sleep(4000);
}
mHandler.post(new DisplayToast(this, "connected :) now start download service"));
} catch (Exception e){
}
}
public void onDestroy(){
run = false;
super.onDestroy();
}
public boolean isConnectedTo(String ssid){
WifiInfo wifiInfo = manager.getConnectionInfo();
String connectedTo = wifiInfo.getSSID().toString();
//if(("\"".concat(ssid).concat("\"")) == wifiInfo.getSSID().toString()){
if(connectedTo.toLowerCase().contains(ssid.toLowerCase())){
mHandler.post(new DisplayToast(this, "connected :) to the right ssid"));
return true;
}
else if("0x" == wifiInfo.getSSID()){
mHandler.post(new DisplayToast(this, "0x!"));
return false;
}
mHandler.post(new DisplayToast(this, "connected to : " + wifiInfo.getSSID() + " but want : " + ("\"".concat(ssid).concat("\""))));
return false;
}
public boolean checkWifi(){
if(manager.isWifiEnabled()){
Log.d("and1 wifi","wifi-enabled");
return true;
}
else{
Log.d("and1 wifi","wifi-disabled");
return false;
}
}
public Boolean scanNetworks(){
boolean scan = manager.startScan();
if(scan) {
this.results = manager.getScanResults();
//Toast.makeText(context, "Wifi State Enabled", Toast.LENGTH_SHORT).show();
return true;
} else
switch(manager.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLING:
Toast.makeText(this, "WIFI_STATE_DISABLING", Toast.LENGTH_LONG).show();
break;
case WifiManager.WIFI_STATE_DISABLED:
Toast.makeText(this, "WIFI_STATE_DISABLED", Toast.LENGTH_LONG).show();
break;
case WifiManager.WIFI_STATE_ENABLING:
Toast.makeText(this, "wifi_enabling", Toast.LENGTH_LONG).show();
break;
case WifiManager.WIFI_STATE_ENABLED:
Toast.makeText(this, "wifi_enabled", Toast.LENGTH_LONG).show();
break;
case WifiManager.WIFI_STATE_UNKNOWN:
Toast.makeText(this, "wifi_unknown_state", Toast.LENGTH_LONG).show();
break;
}
return false;
}
//http://stackoverflow.com/questions/9725830/android-using-wifimanager-to-connect-to-wpa-psk-secured-network
public Boolean networkExists(String networkSSID){
for (ScanResult r : this.results) {
if (r.SSID.contains(networkSSID)) {
Toast.makeText(this, "wifi with name " + networkSSID + " found", Toast.LENGTH_LONG).show();
return true;
}
}
Toast.makeText(this, "wifi with name " + networkSSID + " not found", Toast.LENGTH_LONG).show();
return false;
}
public void connectTo(String networkSSID, String psk){
configuration.SSID = "\"".concat(networkSSID).concat("\"");
configuration.status = WifiConfiguration.Status.DISABLED;
configuration.priority = 40;
configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
configuration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
configuration.preSharedKey = "\"".concat(psk).concat("\"");
int res = manager.addNetwork(configuration);
Log.d("WifiPreference", "add Network returned " + res ); //9 if success
if( manager.enableNetwork(res, true))
Toast.makeText(this, "Connected!" , Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Couldn't connect! Try again." , Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment