Search in sources :

Example 1 with WifiReceiver

use of net.wigle.wigleandroid.listener.WifiReceiver in project wigle-wifi-wardriving by wiglenet.

the class MainActivity method setupWifi.

private void setupWifi() {
    // warn about turning off network notification
    final String notifOn = Settings.Secure.getString(getContentResolver(), Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);
    if (notifOn != null && "1".equals(notifOn) && state.wifiReceiver == null && !isFinishing()) {
        WiGLEToast.showOverActivity(this, R.string.app_name, getString(R.string.best_results));
    }
    final WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    final SharedPreferences prefs = getSharedPreferences(ListFragment.SHARED_PREFS, 0);
    final Editor edit = prefs.edit();
    // keep track of for later
    boolean turnedWifiOn = false;
    if (!wifiManager.isWifiEnabled()) {
        // tell user, cuz this takes a little while
        if (!isFinishing()) {
            WiGLEToast.showOverActivity(this, R.string.app_name, getString(R.string.turn_on_wifi));
        }
        // save so we can turn it back off when we exit
        edit.putBoolean(ListFragment.PREF_WIFI_WAS_OFF, true);
        // just turn it on, but not in emulator cuz it crashes it
        if (!state.inEmulator) {
            MainActivity.info("turning on wifi");
            wifiManager.setWifiEnabled(true);
            MainActivity.info("wifi on");
            turnedWifiOn = true;
        }
    } else {
        edit.putBoolean(ListFragment.PREF_WIFI_WAS_OFF, false);
    }
    edit.apply();
    if (state.wifiReceiver == null) {
        MainActivity.info("new wifiReceiver");
        // wifi scan listener
        // this receiver is the main workhorse of the entire app
        state.wifiReceiver = new WifiReceiver(this, state.dbHelper);
        state.wifiReceiver.setupWifiTimer(turnedWifiOn);
    }
    // register wifi receiver
    setupWifiReceiverIntent();
    if (state.wifiLock == null) {
        MainActivity.info("lock wifi radio on");
        // lock the radio on
        state.wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, ListFragment.WIFI_LOCK_NAME);
        state.wifiLock.acquire();
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiReceiver(net.wigle.wigleandroid.listener.WifiReceiver) SharedPreferences(android.content.SharedPreferences) Editor(android.content.SharedPreferences.Editor)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 Editor (android.content.SharedPreferences.Editor)1 WifiManager (android.net.wifi.WifiManager)1 WifiReceiver (net.wigle.wigleandroid.listener.WifiReceiver)1