package com.ericsson.uecontrol.core.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

import com.ericsson.uecontrol.gui.MainActivity;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Created by ezivkoc on 2014-11-05.
 */
public class WifiScanner {
    private static final Logger log = Logger.getLogger(WifiScanner.class);

    private WifiManager mainWifi;
    private WifiReceiver receiverWifi;

    public WifiScanner() {
        mainWifi = (WifiManager) MainActivity.getContext().getSystemService(MainActivity.getContext().WIFI_SERVICE);
        receiverWifi = new WifiReceiver();
    }

    protected void start() {
        MainActivity.getContext().registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mainWifi.startScan();
    }

    protected void stop() {
        MainActivity.getContext().unregisterReceiver(receiverWifi);
    }


    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> scanList = mainWifi.getScanResults();
            for(ScanResult item : scanList){
                log.debug("WiFi scan found SSID: "+item);
            }
        }
    }
}
