⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phonestumblermanager.java

📁 一个基于PlaceLab的室内和室外的智能导航系统
💻 JAVA
字号:
package org.placelab.midp.stumbler;import java.util.Enumeration;import java.util.Vector;import org.placelab.core.BeaconMeasurement;import org.placelab.core.BeaconReading;import org.placelab.core.GPSMeasurement;import org.placelab.core.Measurement;import org.placelab.midp.EventLogger;import org.placelab.midp.GSMReading;import org.placelab.midp.GSMSpotter;import org.placelab.spotter.InquiryBluetoothGPSSpotter;import org.placelab.spotter.Spotter;import org.placelab.spotter.SpotterException;import org.placelab.spotter.SpotterListener;/** * This class manages the GSM and Bluetooth spotters for the phone stumbler. * It registers as a listener with each spotter and sends appropriate * updates its respective listeners. */public class PhoneStumblerManager implements SpotterListener{	InquiryBluetoothGPSSpotter bgps;	GSMSpotter gsm;	Vector listeners;	Vector allListeners;	Measurement latestMeasurement = null;		boolean sendNext = false;	boolean sent = false;		boolean usingGPS = true;		public PhoneStumblerManager(boolean useGPS) {		listeners = new Vector();		allListeners = new Vector();				if(useGPS) {			bgps = new InquiryBluetoothGPSSpotter();			bgps.addListener(this);			usingGPS = true;		} else {			usingGPS = false;		}		gsm = new GSMSpotter(1000);		gsm.addListener(this);	}	public String getGPSStatus() {		return usingGPS ? bgps.getState() : "not using gps";	}	/*	 * Add a listener that is only notified of changes in the spotters	 * such as a GSM cell changing	 */	public synchronized void addListener(StumblerListener sl) {		listeners.addElement(sl);	}	/*	 * Remove a listener that is only notified of changes in the spotters	 * such as a GSM cell changing	 */	public synchronized void removeListener(StumblerListener sl) {		listeners.removeElement(sl);	}		/*	 * Add a listener that listens to every update from the spotters	 */	public synchronized void addAllListener(StumblerListener sl) {		allListeners.addElement(sl);	}	/*	 * Remove a listener that listens to every update from the spotters	 */	public synchronized void removeAllListener(StumblerListener sl) {		allListeners.removeElement(sl);	}		/*	 * Notify listeners who have registered to listen to every	 * update the manager receives	 */	public synchronized void notifyAllListeners(Measurement[] m) {		for(Enumeration i = allListeners.elements();i.hasMoreElements();) {			StumblerListener sl = (StumblerListener) i.nextElement();			sl.gotMeasurement(m);		}	}	/*	 * Notify listeners who have registered to listen to unique	 * updates the manager receives	 */	public synchronized void notifyListeners(Measurement[] m) {		notifyAllListeners(m);		for(Enumeration i = listeners.elements();i.hasMoreElements();) {			StumblerListener sl = (StumblerListener) i.nextElement();			sl.gotMeasurement(m);		}	}		public synchronized void gotMeasurement(Spotter sender, Measurement m) { 		if(m == null)			return;				if(sender == bgps) {			if(!((GPSMeasurement) m).isValid())				return;						Measurement[] ms = new Measurement[2];							ms[0] = latestMeasurement;			ms[1] = m;							if(latestMeasurement != null && m != null) {				notifyListeners(ms);				sendNext = true;			}		} else if(sender == gsm) {			//filter out bad gsm readings			BeaconReading[] readings = ((BeaconMeasurement) m).getReadings();			GSMReading reading = (GSMReading) readings[0];			if(!reading.isValid()) {				return;			}			//continue on			Measurement[] ms = new Measurement[1];			ms[0] = m;						if(latestMeasurement == null || sendNext) {				latestMeasurement = m;				notifyListeners(ms);				sendNext = false;			} else {				BeaconReading[] latestReadings = ((BeaconMeasurement) latestMeasurement).getReadings();				GSMReading latestReading = (GSMReading) latestReadings[0];				BeaconReading[] brs = ((BeaconMeasurement) m).getReadings();				GSMReading thisReading = (GSMReading) brs[0];								if (!latestReading.getId().equals(thisReading.getId())) {					latestMeasurement = m;										notifyListeners(ms);				} else {					notifyAllListeners(ms);				}			}		}	}	public void open() {		try {			if(usingGPS) bgps.open();			gsm.open();		} catch(SpotterException se) {			StumblerMidlet.alert(se.getMessage());		}	}		public synchronized void close() {		try {			if(bgps != null) bgps.close();			if(gsm != null) gsm.close();		} catch(SpotterException se) {			StumblerMidlet.alert(se.getMessage());		}	}		public synchronized void start() {		try {			gsm.startScanning();			if(usingGPS) bgps.startScanning();				} catch(Exception e) {			EventLogger.logError(e);			e.printStackTrace();		}	}		public synchronized void stop() {		if(usingGPS) bgps.stopScanning();		gsm.stopScanning();	}	public void spotterExceptionThrown(Spotter sender, SpotterException ex) {	}}		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -