Android Open Source - LocationSpy Wifi Connection Listener






From Project

Back to project page LocationSpy.

License

The source code is released under:

GNU General Public License

If you think the Android project LocationSpy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.mvollebregt.locationspy.wifi;
/* ww  w .  j  av  a  2 s  .com*/
// This file is part of LocationSpy.
//
// LocationSpy is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LocationSpy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LocationSpy.  If not, see <http://www.gnu.org/licenses/>.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import com.github.mvollebregt.locationspy.UserLocationDetector;
import com.github.mvollebregt.locationspy.UserLocationListener;
import com.github.mvollebregt.locationspy.Serviceable;
import com.github.mvollebregt.locationspy.UserLocation;

import java.util.Hashtable;

public class WifiConnectionListener extends BroadcastReceiver implements UserLocationDetector, Serviceable {

    private UserLocationListener listener;
    private Hashtable<String, String> ssids;

    public WifiConnectionListener(Hashtable ssids) {
        this.ssids = ssids;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.i("LOCATIONSPY", "WifiConnectionListener received an event of type: " + action);
        NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (action.equalsIgnoreCase(ConnectivityManager.CONNECTIVITY_ACTION /*WifiManager.NETWORK_STATE_CHANGED_ACTION*/) && info.getState() == NetworkInfo.State.CONNECTED) {
            UserLocation location = currentLocation(context);
            listener.locationChanged(location);
        }
    }

    public void setLocationListener(UserLocationListener listener) {
         this.listener = listener;
    }

    public UserLocation currentLocation(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String ssid = wifiInfo.getSSID();
        Log.i("LOCATIONSPY", "BSSID of network: " + wifiInfo.getBSSID());
        String locationName = ssids.get(wifiInfo.getSSID());
        return locationName != null ? new UserLocation(locationName) : null;
    }

    public void init(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(this, filter);
    }

    public void close(Context context) {
         context.unregisterReceiver(this);
    }
}




Java Source Code List

com.github.mvollebregt.locationspy.LocationSpyManagerActivity.java
com.github.mvollebregt.locationspy.LocationSpyService.java
com.github.mvollebregt.locationspy.Serviceable.java
com.github.mvollebregt.locationspy.UserLocationDetector.java
com.github.mvollebregt.locationspy.UserLocationListener.java
com.github.mvollebregt.locationspy.UserLocationPublisher.java
com.github.mvollebregt.locationspy.UserLocation.java
com.github.mvollebregt.locationspy.gtalk.GtalkStatusUserLocationPublisher.java
com.github.mvollebregt.locationspy.location.TravelListener.java
com.github.mvollebregt.locationspy.wifi.WifiConnectionListener.java
org.jivesoftware.spark.util.DummySSLSocketFactory.java