Android Open Source - Look Wifi Service






From Project

Back to project page Look.

License

The source code is released under:

====================== LOOK! LICENSING TERMS ====================== look! is licensed under the BSD 3-Clause (also known as "BSD New" or "BSD Simplified"), as follows: Copyright (c) 2010-2012, Look...

If you think the Android project Look 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

/**
*-----------------------------------------------------------------------------
* Copyright (c) 2012, Look! Development Team
* All rights reserved.//w  w w .  j av a  2  s .  c o  m
*
* Distributed under the terms of the BSD Simplified License.
*
* The full license is in the LICENSE file, distributed with this software.
*-----------------------------------------------------------------------------
*/
package es.ucm.look.locationProviderWifi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
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 android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import es.ucm.look.locationProviderWifi.util.DateUtils;
import es.ucm.look.locationProviderWifi.util.DeviceReader;
import es.ucm.look.locationProviderWifi.util.DeviceWriter;
import es.ucm.look.locationProviderWifi.wifi.Lugar;
import es.ucm.look.locationProviderWifi.wifi.Lugares;
import es.ucm.look.locationProviderWifi.wifi.NodoWifi;

/**
 * Location Service implemented as Android Service.
 * 
 * @author Jorge Creixell Rojo
 * 
 */
public class WifiService extends Service {

  /**
   * Unique identification number for the notification.
   */
  public int NOTIFICATION = 777;
  
  /**
   * Service name.
   */
  private static final String TAG = "LocationService";
  
  /**
   * Service Binder.
   */
  private final IBinder mBinder = new ServicioBinder();

  /**
   * Fingerprints file.
   */
  public static final String ENTRENAMIENTO = "/sdcard/Entrenamiento.txt";
  
  /**
   * Access points file.
   */
  public static final String APS = "/sdcard/APs.txt";
  
  /**
   * Log file.
   */
  public static final String LOG_FILE = "/sdcard/log.txt";
  
  /**
   * Nunmber of scans for each measurement.
   */
  public static final int MAX_COUNT = 5;
  
  /**
   * Whether to enable logging or not.
   */
  public static final boolean LOG = true;

  /**
   * Log buffer.
   */
  private StringBuffer log_data;
  
  /**
   * Device writer object.
   */
  private DeviceWriter out;

  /**
   * Whether scanning is active or not.
   */
  private boolean scan_activo = false;

  /**
   * Timer to control time intervals between scans.
   */
  private Timer timer = null;
  
  /**
   * Number of scans already performed in the current measurement.
   */
  private int count = 1;
  
  /**
   * Data of the scans of the current measurement.
   */
  private HashMap<String, ArrayList<Integer>> tmpData;

  /**
   * Nunmber of seconds between scans.
   */
  private static final int REFRESH_RATE = 1; // segundos

  /**
   * Android Wifi Manager.
   */
  private WifiManager mWifiManager;
  
  /**
   * Android Wifi Receiver.
   */
  private WifiReceiver mWifiReceiver;

  /**
   * Fingerprints file data.
   */
  private HashMap<String, ArrayList<NodoWifi>> datosEntrenamiento;
  
  /**
   * Access porints file data.
   */
  private Set<String> aps;

  /**
   * Current location information.
   */
  private Lugar lugarActual;

  @Override
  public IBinder onBind(Intent arg0) {
    return mBinder;
  }

  /**
   * Service binder class.
   */
  public class ServicioBinder extends Binder {
    public WifiService getService() {
      return WifiService.this;
    }
  }

  @Override
  public void onCreate() {
    Log.i(TAG, "Servicio iniciado");

    out = new DeviceWriter(LOG_FILE);

    datosEntrenamiento = new HashMap<String, ArrayList<NodoWifi>>();
    aps = Collections.synchronizedSet(new HashSet<String>());
    tmpData = new HashMap<String, ArrayList<Integer>>();

    count = 1;
    lugarActual = new Lugar();

    log_data = new StringBuffer();

    leeDatosEntrenamiento();
    leeAPs();

    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mWifiManager.setWifiEnabled(true);
    mWifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY,
        "Scan Only");
    mWifiReceiver = new WifiReceiver();

    registerReceiver(mWifiReceiver, new IntentFilter(
        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Received start id " + startId + ": " + intent);

    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    Log.i(TAG, "Servicio detenido");
    this.unregisterReceiver(mWifiReceiver);
    out.close();

  }

  public void start() {

    if (!scan_activo) {

      scan_activo = true;
      timer = new Timer();
      timer.schedule(new TimerTask() {
        public void run() {
          mWifiManager.startScan();
        }

      }, 0, REFRESH_RATE * 1000);
    }
  }

  public void stop() {
    if (scan_activo) {
      scan_activo = false;
      timer.cancel();
    }
  }
  
  public Lugar getPosicion() {
    return lugarActual;
  }

  private void leeDatosEntrenamiento() {
    DeviceReader in = new DeviceReader(ENTRENAMIENTO);

    String ap = null;
    String bssid, clave;
    int id;
    int level;

    do {
      ap = in.readln();
      if (ap != null) {

        String[] data = ap.split(" ");
        bssid = data[0];
        level = Integer.parseInt(data[1]);
        id = Integer.parseInt(data[2]);

        NodoWifi nodo = new NodoWifi(bssid, level, id);

        clave = Integer.toString(id);

        if (datosEntrenamiento.containsKey(clave)) {
          datosEntrenamiento.get(clave).add(nodo);
        } else {
          ArrayList<NodoWifi> l = new ArrayList<NodoWifi>();
          l.add(nodo);
          datosEntrenamiento.put(clave, l);
        }
      }

    } while (ap != null);
    in.close();
  }

  private void leeAPs() {

    DeviceReader in = new DeviceReader(APS);
    String bssid = null;

    do {
      bssid = in.readln();
      if (bssid != null) {

        aps.add(bssid);
      }

    } while (bssid != null);
    in.close();

    /*Iterator<String> it = aps.iterator();
    while (it.hasNext()) {
      String key = it.next();
    }*/

  }

  private synchronized void evaluaPosicion() {

    String maxPuntKey = null;
    double minDistEuclidea = Double.MAX_VALUE;

    Iterator<String> itAps = aps.iterator();

    // annadimos los nodos no detectados con sennal minima para tener igual
    // num de aps
    while (itAps.hasNext()) {
      String key = itAps.next();
      if (!tmpData.containsKey(key)) {
        ArrayList<Integer> lista = new ArrayList<Integer>();
        lista.add(-100);
        tmpData.put(key, lista);
      }
    }

    double acum = 0;

    Set<String> keys = datosEntrenamiento.keySet();
    Iterator<String> it = keys.iterator();

    while (it.hasNext()) {
      String key = it.next();
      acum = 0;

      ArrayList<NodoWifi> listaNodos = datosEntrenamiento.get(key);

      for (int i = 0; i < listaNodos.size(); i++) {

        NodoWifi nodo = listaNodos.get(i);

        Set<String> tmpKeys = tmpData.keySet();
        Iterator<String> it2 = tmpKeys.iterator();

        while (it2.hasNext()) {
          String bssid = it2.next();

          if (bssid.equals(nodo.getBssid()) && aps.contains(bssid)) {
            acum += Math
                .pow((NodoWifi.averageLevel(tmpData.get(bssid)) - nodo
                    .getLevel()), 2);

            log("\t " + bssid + " Ent: " + nodo.getLevel()
                + " Scan: "
                + NodoWifi.averageLevel(tmpData.get(bssid))
                + "\n");

          }
        }

      }

      double distanciaEuclidea = Math.sqrt(acum);

      if (distanciaEuclidea < minDistEuclidea) {
        minDistEuclidea = distanciaEuclidea;
        maxPuntKey = key;
      }
      log("Time: " + DateUtils.now() + " Nodo: " + key + " punt: "
          + distanciaEuclidea + "\n");

    }
    log("MaxPunt: " + minDistEuclidea + "\n");

    if (maxPuntKey != null) {

      final int idFinal = datosEntrenamiento.get(maxPuntKey).get(0)
          .getId();

      Lugar lugar = Lugares.getInstance().getLugar(idFinal);

      if (lugar != null) {
        lugarActual = lugar;
        log("Nodo seleccionado: " + idFinal + "\n");
        log("\n");
      }
    }
  }

  private void log(String s) {
    log_data.append(s);
    //Log.i(TAG, "log: " + s);

  }

  private class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
      if (scan_activo) {

        if (count == 0) {
          Log.i(TAG, "Evaluando posicion");

        }
        WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> scanResult = mWifiManager.getScanResults();

        if (count == 0) {

          evaluaPosicion();

          tmpData = new HashMap<String, ArrayList<Integer>>();

          for (int i = 0; i < scanResult.size(); i++) {

            ArrayList<Integer> signals = new ArrayList<Integer>();
            signals.add(scanResult.get(i).level);
            tmpData.put(scanResult.get(i).BSSID, signals);
          }

        } else {
          for (int i = 0; i < scanResult.size(); i++) {

            if (tmpData.containsKey(scanResult.get(i).BSSID)) {
              tmpData.get(scanResult.get(i).BSSID).add(
                  scanResult.get(i).level);
            } else {
              ArrayList<Integer> signals = new ArrayList<Integer>();
              signals.add(scanResult.get(i).level);
              tmpData.put(scanResult.get(i).BSSID, signals);
            }

          }
        }

        count = (count + 1) % (MAX_COUNT + 1);

      }
    }
  }

}




Java Source Code List

es.ucm.look.ar.LookAR.java
es.ucm.look.ar.Preview.java
es.ucm.look.ar.ar2D.AR2D.java
es.ucm.look.ar.ar2D.Drawable2D.java
es.ucm.look.ar.ar2D.HUDElement.java
es.ucm.look.ar.ar2D.drawables.Circle2D.java
es.ucm.look.ar.ar2D.drawables.Image2D.java
es.ucm.look.ar.ar2D.drawables.Text2D.java
es.ucm.look.ar.ar3D.Drawable3D.java
es.ucm.look.ar.ar3D.Renderer3D.java
es.ucm.look.ar.ar3D.core.Color4.java
es.ucm.look.ar.ar3D.core.TextureFactory.java
es.ucm.look.ar.ar3D.core.camera.Camera3D.java
es.ucm.look.ar.ar3D.core.camera.OrientedCamera.java
es.ucm.look.ar.ar3D.core.drawables.DrawablesDataBase.java
es.ucm.look.ar.ar3D.core.drawables.Entity3D.java
es.ucm.look.ar.ar3D.core.drawables.Mesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.CirclePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Cube.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Grid.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinesLoopPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.ObjMesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.PointPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Ring.java
es.ucm.look.ar.ar3D.core.drawables.primitives.SquarePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.TrianglePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.extra.ImagePrimitive.java
es.ucm.look.ar.ar3D.parser.MeshObjParser.java
es.ucm.look.ar.hud.ActionListener.java
es.ucm.look.ar.hud.BasicHud.java
es.ucm.look.ar.hud.Button.java
es.ucm.look.ar.hud.HUD.java
es.ucm.look.ar.listeners.CameraListener.java
es.ucm.look.ar.listeners.TouchListener.java
es.ucm.look.ar.math.collision.Armature.java
es.ucm.look.ar.math.collision.SphericalArmature.java
es.ucm.look.ar.math.collision.SquareArmature.java
es.ucm.look.ar.math.collision.debug.DebugArmature.java
es.ucm.look.ar.math.collision.debug.SphericalDebugArmature.java
es.ucm.look.ar.math.collision.debug.SquareDebugArmature.java
es.ucm.look.ar.math.geom.Matrix3.java
es.ucm.look.ar.math.geom.Plane.java
es.ucm.look.ar.math.geom.Point2.java
es.ucm.look.ar.math.geom.Point3.java
es.ucm.look.ar.math.geom.Ray.java
es.ucm.look.ar.math.geom.Triangle.java
es.ucm.look.ar.math.geom.Vector3.java
es.ucm.look.ar.util.CameraParametersHelper.java
es.ucm.look.ar.util.DeviceOrientation.java
es.ucm.look.ar.util.LookARUtil.java
es.ucm.look.ar.util.PositionTimerTask.java
es.ucm.look.data.EntityData.java
es.ucm.look.data.LookData.java
es.ucm.look.data.WorldEntityFactory.java
es.ucm.look.data.WorldEntity.java
es.ucm.look.data.World.java
es.ucm.look.data.filesManager.LookFilesManager.java
es.ucm.look.data.interfaces.DataGetter.java
es.ucm.look.data.interfaces.DataHandler.java
es.ucm.look.data.interfaces.DataSetter.java
es.ucm.look.data.local.BasicDataHandler.java
es.ucm.look.data.local.DBDataHandler.java
es.ucm.look.data.local.contentprovider.LookContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLHelper.java
es.ucm.look.data.remote.ConfigNet.java
es.ucm.look.data.remote.LookProperties.java
es.ucm.look.data.remote.RemoteDBHandler.java
es.ucm.look.data.remote.restful.LookService.java
es.ucm.look.data.remote.restful.RestMethod.java
es.ucm.look.data.remote.restful.ServiceManager.java
es.ucm.look.location.LocationManager.java
es.ucm.look.locationProvider.DeviceSensor.java
es.ucm.look.locationProvider.InertialNavigationSystem.java
es.ucm.look.locationProvider.LocationProvider.java
es.ucm.look.locationProvider.Motion.java
es.ucm.look.locationProvider.Positioning.java
es.ucm.look.locationProvider.Util.java
es.ucm.look.locationProviderWifi.Cliente.java
es.ucm.look.locationProviderWifi.WifiLocation.java
es.ucm.look.locationProviderWifi.WifiService.java
es.ucm.look.locationProviderWifi.util.DateUtils.java
es.ucm.look.locationProviderWifi.util.DeviceReader.java
es.ucm.look.locationProviderWifi.util.DeviceWriter.java
es.ucm.look.locationProviderWifi.wifi.Lugar.java
es.ucm.look.locationProviderWifi.wifi.Lugares.java
es.ucm.look.locationProviderWifi.wifi.NodoWifi.java
es.ucm.look.locationProvider.map.Mapa.java
es.ucm.look.locationProvider.test.java