Back to project page mitlocate.
The source code is released under:
MIT License
If you think the Android project mitlocate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package edu.mit.locate; //from w w w . ja v a2s. co m import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Handler; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.widget.Toast; public class MITLocationService extends Service { private WifiManager wifi; private SharedPreferences prefs; public static final String BROADCAST_LOCATOR = "edu.mit.locate.locator"; static final String BROADCAST_ADDLOCATION = "edu.mit.locate.addlocation"; private final Handler handler = new Handler(); protected Intent broadcastIntent; private PendingIntent pi; private AlarmManager am; private boolean add = false; private final String TAG = "MITLocationService"; private String building, desc; @Override public void onCreate() { super.onCreate(); wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); prefs = getSharedPreferences("MITLOCATE", 0); Intent MITLocationServiceIntent = new Intent(this, MITLocationService.class); pi = PendingIntent.getService(this, 1, MITLocationServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT); registerReceiver(WiFiScanBR, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if(prefs.getInt("Authorization",0)<1) stopSelf(); //am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,10*1000,prefs.getInt("UpdateFrequency",5)*60*1000,pi); if(intent != null && intent.getBooleanExtra("add",false)){ //NPE here add=true; broadcastIntent = new Intent(BROADCAST_ADDLOCATION); building = intent.getStringExtra("building"); desc = intent.getStringExtra("desc"); } else broadcastIntent = new Intent(BROADCAST_LOCATOR); handler.removeCallbacks(getLocation); handler.post(getLocation); return super.onStartCommand(intent, flags, startId); } private Runnable getLocation = new Runnable() { public void run() { wifi.startScan(); } }; //Processes the scan results from WiFiManager, hands them over to MITHttpsClient for analysis final BroadcastReceiver WiFiScanBR = new BroadcastReceiver() { public void onReceive(Context c, Intent intent) { List<ScanResult> results = wifi.getScanResults(); JSONObject params = new JSONObject(); JSONArray aps = new JSONArray(); try { params.put("UUID", prefs.getString("DeviceID",null)); for(int i = 0;i < results.size();i++){ final ScanResult result = results.get(i); JSONObject ap = new JSONObject(); ap.put("bssid", result.BSSID.replace(":", "")); ap.put("signal", Integer.toString(-result.level)); ap.put("ssid", result.SSID); aps.put(ap); } params.put("aps", aps); if(aps.length()>0){ params.put("aps", aps); new MITHttpsClient("heartbeat", getApplicationContext(), false){ @Override protected void onPostExecute(JSONObject response){ if(response!=null){ broadcastIntent.putExtra("response", response.toString()); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent); stopSelf(); } } }.execute(params); } } catch (JSONException je){ Log.e(TAG, "JSONException in WiFiScanBR"); } } }; protected void beam(JSONObject jsonParams) throws JSONException, UnsupportedEncodingException { if(add){ jsonParams.put("building", building); jsonParams.put("desc", desc); new MITHttpsClient("add", getApplicationContext(), false){ @Override protected void onPostExecute(JSONObject response){ if(response!=null){ broadcastIntent.putExtra("response", response.toString()); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent); stopSelf(); } else { Toast.makeText(MITLocationService.this, response.toString(), Toast.LENGTH_SHORT).show(); } } }.execute(jsonParams); // new MITHttpsClient(this.getApplicationContext(), "add/", jsonParams, new JsonHttpResponseHandler() { // @Override // public void onSuccess(String response) { // broadcastIntent.putExtra("response", response); // } // @Override // public void onFailure(Throwable e, String response) { // broadcastIntent.putExtra("response", "failed"); // } // @Override // public void onFinish() { // Log.e(TAG,"FINISHED"); // // } // }); } } //Processes the location result from MITHttpsClient (broadcasts to LocationServiceBR in MainActivity) // final BroadcastReceiver HTTPResponseProcessorBR = new BroadcastReceiver() { // public void onReceive(Context context, Intent intent) { // String response = intent.getStringExtra("response"); // int responseCode = intent.getIntExtra("responseCode",404); // String failure = intent.getStringExtra("failure"); // if(failure!=null){ // Log.e(TAG, "A fail: "+failure); // }else if(responseCode==200){ // BroadcastIntent.putExtra("location", response); // } else { // BroadcastIntent.putExtra("location", "Non-200 HTTP Response"); // Log.w(TAG,"HTTP error "+responseCode); // } // Log.d(TAG,response); // LocalBroadcastManager.getInstance(context).sendBroadcast(BroadcastIntent); // stopSelf(); // } // }; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { unregisterReceiver(WiFiScanBR); handler.removeCallbacks(getLocation); super.onDestroy(); } }