Example usage for android.net.wifi WifiManager SCAN_RESULTS_AVAILABLE_ACTION

List of usage examples for android.net.wifi WifiManager SCAN_RESULTS_AVAILABLE_ACTION

Introduction

In this page you can find the example usage for android.net.wifi WifiManager SCAN_RESULTS_AVAILABLE_ACTION.

Prototype

String SCAN_RESULTS_AVAILABLE_ACTION

To view the source code for android.net.wifi WifiManager SCAN_RESULTS_AVAILABLE_ACTION.

Click Source Link

Document

An access point scan has completed, and results are available.

Usage

From source file:org.exobel.routerkeygen.ui.NetworksListActivity.java

private void scan() {
    if (!wifiState && !wifiOn) {
        networkListFragment.setMessage(R.string.msg_nowifi);
        return;//from  w  w w.j  a va2  s.  co m
    }
    registerReceiver(scanFinished, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    if (wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
        registerReceiver(stateChanged, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
        Toast.makeText(this, R.string.msg_wifienabling, Toast.LENGTH_SHORT).show();
    } else {
        if (wifi.startScan()) {
            //setRefreshActionItemState(true);
            mSwipeRefreshLayout.setRefreshing(true);
        } else
            networkListFragment.setMessage(R.string.msg_scanfailed);
    }
}

From source file:es.glasspixel.wlanaudit.fragments.ScanFragment.java

/**
 * Sets up the logic to execute when a scan is complete. This is done this
 * way because the SCAN_RESULTS_AVAILABLE_ACTION must be caught by a
 * BroadCastReceiver./*from  www .jav  a  2 s  . co m*/
 */
private void setupNetworkScanCallBack() {
    IntentFilter i = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    mCallBackReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Network scan complete, datasource needs to be updated and
            // ListView refreshed

            List<ScanResult> res = mWifiManager.getScanResults();
            mIsScanning = false;

            if (saved_keys_fragment != null && getSherlockActivity() != null) {
                mCallback.scanCompleted();
                if (mWifiManager.getScanResults().size() > 0) {
                    list_view.setAdapter(new WifiNetworkAdapter(getSherlockActivity(),
                            R.layout.network_list_element_layout, res));
                } else {
                    list_view.setEmptyView(getSherlockActivity().findViewById(R.id.empty));
                }
            }

        }
    };
    getSherlockActivity().registerReceiver(mCallBackReceiver, i);
}

From source file:eu.power_switch.gui.dialog.AddSsidDialog.java

@Override
public void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    // use activity directly, otherwise the intent wont be catched (alternative way below)
    getActivity().registerReceiver(broadcastReceiver, intentFilter);
    //        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver, intentFilter);
}

From source file:org.eeiiaa.wifi.WifiConnector.java

@Override
    public void onReceive(Context context, Intent intent) {
        // handle stuff related to scanning requested when:
        // normal scan is issued
        // when forgetting a network and re-connecting to a previously configured
        // existing one
        // when connecting and need a scan to verify connection can be established
        Log.i(TAG, "action: " + intent.getAction());
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) && (mWaitingScan || mConnecting)) {
            // forgetting current and reconnecting to highest priority existing
            // network

            Log.i(TAG, "ACTION SCAN: mWaitingScan:" + mWaitingScan + " connecting " + mConnecting);

            // normal scan request
            if (mWaitingScan) {
                String json = new String();
                switch (scanResultOption) {
                case GET_ALL:
                    json = getScannedNetworksAllJSON();
                    break;
                case GET_BSSID_ONLY:
                    json = getScannedNetworksJSON();
                    break;
                }/* w ww. jav  a  2 s.c o m*/
                mScanListener.scanResultsJSON(json);
                mWaitingScan = false;

                // scan is done - unregister
                mContext.unregisterReceiver(this);
                Log.i(TAG, "processing scan results as JSON");

            }

            if (mConnecting && !mWaitingConnection) {
                // look for the network we want to connect to in the scanned results
                ScanResult scannedNet = searchNetwork(mNetssid);
                if (scannedNet == null) {
                    Log.i(TAG, "ssid not found...: " + mNetssid);
                    mConnecting = false;
                    mContext.unregisterReceiver(this);
                    notifyConnectionFailed();
                    return; // didn't find requested network noting to connect
                }

                WifiConfiguration configuredNet = searchConfiguration(scannedNet);
                boolean result_ok = false;

                if (configuredNet != null) {
                    // configuration exits connect to a configured network
                    mWaitingConnection = true;
                    result_ok = Wifi.connectToConfiguredNetwork(mContext, mWifiMgr, configuredNet, false);
                    Log.i(TAG, "configuration exits connect to a configured network");
                } else {
                    // configure and connect to network
                    mWaitingConnection = true;
                    result_ok = Wifi.connectToNewNetwork(mContext, mWifiMgr, scannedNet, mPwd, MAX_OPEN_NETS);
                    Log.i(TAG, "configure and connect to network");
                }

                // failed to connect - unregister and notify
                if (!result_ok) {
                    mContext.unregisterReceiver(this);
                    mWaitingConnection = false;
                    mConnecting = false;
                    Log.e(TAG, "error connecting Wifi.connect returned error");
                    notifyConnectionFailed();
                }
            }
        }

        else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)
                && (mConnected || (mConnecting && mWaitingConnection))) {
            Log.i(TAG, "ACTION CONNECTIVITY: mWaitingScan:" + mWaitingScan + " forgetting: " + forgetting_
                    + " connecting " + mConnecting);

            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

            if (!mConnected && networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && networkInfo.isConnected()) {

                if (mWifiMgr.getConnectionInfo().getSupplicantState() == SupplicantState.COMPLETED) {

                    Log.i(TAG, " getConnectionInfo: " + mWifiMgr.getConnectionInfo() + " getSSID: "
                            + mWifiMgr.getConnectionInfo().getSSID());
                    String connectionSsid = unQuote(mWifiMgr.getConnectionInfo().getSSID());
                    // when phone turns into AP mode, wifimgr returns null...
                    // fail protection for such cases...
                    if (connectionSsid != null) {
                        Log.i(TAG, "mNetssid: |" + mNetssid + "| connectionSsid |" + connectionSsid + "|");
                        if (connectionSsid.equals(mNetssid)) {
                            mConnected = true;
                            mConnecting = false;
                            mWaitingConnection = false;
                            forgetting_ = false;

                            Log.i(TAG, "VERIFIED SUCCESSFUL CONNECTION to: " + mNetssid);
                            // connection done notify - do not unregister to get disconnection detected
                            notifyConnectionListener();
                        }
                    }
                }
            } else if (mConnected && networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && !networkInfo.isConnected()) {

                Log.i(TAG, "network is disconnected...");

                // Wifi is disconnected
                mConnected = false;
                mWaitingConnection = false;
                if (mDataListener != null && used_ != null) {
                    used_.updateUsageCounters();
                    transmitted = used_.getWifiTransmitted();
                    received = used_.getWifiReceived();
                }
                // network lost was not expected (when forgetting we expect network to
                // be lost)
                if (!forgetting_) {
                    Log.i(TAG, "network lost " + networkInfo);
                    notifyNetworkLost(networkInfo);
                    mContext.unregisterReceiver(this);
                } else {
                    Log.i(TAG, "network lost when forgetting " + networkInfo);
                }
            }

            Log.i(TAG, "end of CONNECTIVITY_ACTION");
        }

    }

From source file:com.android.development.Connectivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.connectivity);

    mWm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mPm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mCm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
    mNetd = INetworkManagementService.Stub.asInterface(b);

    findViewById(R.id.enableWifi).setOnClickListener(mClickListener);
    findViewById(R.id.disableWifi).setOnClickListener(mClickListener);

    findViewById(R.id.startDelayedCycle).setOnClickListener(mClickListener);
    findViewById(R.id.stopDelayedCycle).setOnClickListener(mClickListener);
    mDCOnDurationEdit = (EditText) findViewById(R.id.dc_wifi_on_duration);
    mDCOnDurationEdit.setText(Long.toString(mDCOnDuration));
    mDCOffDurationEdit = (EditText) findViewById(R.id.dc_wifi_off_duration);
    mDCOffDurationEdit.setText(Long.toString(mDCOffDuration));
    mDCCycleCountView = (TextView) findViewById(R.id.dc_wifi_cycles_done);
    mDCCycleCountView.setText(Integer.toString(mDCCycleCount));

    findViewById(R.id.startScreenCycle).setOnClickListener(mClickListener);
    findViewById(R.id.stopScreenCycle).setOnClickListener(mClickListener);
    mSCOnDurationEdit = (EditText) findViewById(R.id.sc_wifi_on_duration);
    mSCOnDurationEdit.setText(Long.toString(mSCOnDuration));
    mSCOffDurationEdit = (EditText) findViewById(R.id.sc_wifi_off_duration);
    mSCOffDurationEdit.setText(Long.toString(mSCOffDuration));
    mSCCycleCountView = (TextView) findViewById(R.id.sc_wifi_cycles_done);
    mSCCycleCountView.setText(Integer.toString(mSCCycleCount));

    mScanButton = (Button) findViewById(R.id.startScan);
    mScanButton.setOnClickListener(mClickListener);
    mScanCyclesEdit = (EditText) findViewById(R.id.sc_scan_cycles);
    mScanCyclesEdit.setText(Long.toString(mScanCycles));
    mScanDisconnect = (CheckBox) findViewById(R.id.scanDisconnect);
    mScanDisconnect.setChecked(true);// w  w w .j  a v  a2  s .c  o  m
    mScanResults = (TextView) findViewById(R.id.sc_scan_results);
    mScanResults.setVisibility(View.INVISIBLE);

    mScanRecv = new WifiScanReceiver();
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

    findViewById(R.id.startTdls).setOnClickListener(mClickListener);
    findViewById(R.id.stopTdls).setOnClickListener(mClickListener);

    findViewById(R.id.start_mms).setOnClickListener(mClickListener);
    findViewById(R.id.stop_mms).setOnClickListener(mClickListener);
    findViewById(R.id.start_hipri).setOnClickListener(mClickListener);
    findViewById(R.id.stop_hipri).setOnClickListener(mClickListener);
    findViewById(R.id.crash).setOnClickListener(mClickListener);

    findViewById(R.id.add_default_route).setOnClickListener(mClickListener);
    findViewById(R.id.remove_default_route).setOnClickListener(mClickListener);
    findViewById(R.id.bound_http_request).setOnClickListener(mClickListener);
    findViewById(R.id.bound_socket_request).setOnClickListener(mClickListener);
    findViewById(R.id.routed_http_request).setOnClickListener(mClickListener);
    findViewById(R.id.routed_socket_request).setOnClickListener(mClickListener);
    findViewById(R.id.default_request).setOnClickListener(mClickListener);
    findViewById(R.id.default_socket).setOnClickListener(mClickListener);

    registerReceiver(mReceiver, new IntentFilter(CONNECTIVITY_TEST_ALARM));
}

From source file:com.davidmascharka.lips.MainActivity.java

@Override
public void onResume() {
    super.onResume();

    // Set building textview to the building the user has selected
    TextView buildingText = (TextView) findViewById(R.id.text_building);
    buildingText.setText("Building: " + building);

    // Set room size textview to the room size the user has selected
    TextView roomSizeText = (TextView) findViewById(R.id.text_room_size);
    roomSizeText.setText("Room size: " + roomWidth + " x " + roomLength);

    // Set grid options
    GridView grid = (GridView) findViewById(R.id.gridView);
    grid.setGridSize(roomWidth, roomLength);
    grid.setDisplayMap(displayMap);/*  www .j  a va 2 s . com*/

    // Register to get sensor updates from all the available sensors
    sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
    for (Sensor sensor : sensorList) {
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
    }

    // Enable wifi if it is not
    if (!wifiManager.isWifiEnabled()) {
        Toast.makeText(this, "WiFi not enabled. Enabling...", Toast.LENGTH_SHORT).show();
        wifiManager.setWifiEnabled(true);
    }

    // Request location updates from gps and the network
    //@author Mahesh Gaya added permission if-statment
    if (ActivityCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permissions have NOT been granted. Requesting permissions.");
        requestMyPermissions();
    } else {
        Log.i(TAG, "Permissions have already been granted. Getting location from GPS and Network");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

From source file:com.tml.sharethem.receiver.ReceiverActivity.java

/**
 * Registers for {@link WifiManager#SCAN_RESULTS_AVAILABLE_ACTION} action and also calls a method to start Wifi Scan action.
 *///from   ww w .j a va2  s .c  o  m
private void registerAndScanForWifiResults() {
    if (null == mWifiScanReceiver)
        mWifiScanReceiver = new WifiScanner();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    registerReceiver(mWifiScanReceiver, intentFilter);
    m_p2p_connection_status.setText(getString(R.string.p2p_receiver_scanning_hint));
    startWifiScan();
}

From source file:fr.inria.ucn.collectors.NetworkStateCollector.java

private JSONObject getWifi(Context c) throws JSONException {
    WifiManager wm = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();

    // start a wifi AP scan
    Helpers.acquireWifiLock(c);/*from   ww w  .j  ava 2s.co  m*/
    IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    c.registerReceiver(this, filter);
    wm.startScan();

    JSONObject o = new JSONObject();
    o.put("link_speed", wi.getLinkSpeed());
    o.put("link_speed_units", WifiInfo.LINK_SPEED_UNITS);
    o.put("signal_level", WifiManager.calculateSignalLevel(wi.getRssi(), 100));
    o.put("rssi", wi.getRssi());
    o.put("bssid", wi.getBSSID());
    o.put("ssid", wi.getSSID().replaceAll("\"", ""));
    o.put("mac", wi.getMacAddress());

    int ip = wi.getIpAddress();
    String ipstr = String.format(Locale.US, "%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff),
            (ip >> 24 & 0xff));
    o.put("ip", ipstr);

    return o;
}

From source file:org.wahtod.wififixer.ui.KnownNetworksFragment.java

private void registerReceiver() {
    IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    BroadcastHelper.registerReceiver(getContext(), receiver, filter, false);
    scanhandler.sendEmptyMessage(SCAN_MESSAGE);
}

From source file:edu.drake.research.android.lipswithmaps.activity.MapsActivity.java

/**
 * get wifi scan/*from w ww . ja v  a  2s.  c  om*/
 */
private void wifiScan() {
    if (mWifiManager == null) {
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        registerReceiver(mWifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }
    mWifiManager.startScan();
}