下面列出了android.hardware.Sensor#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public float[] handleListener(Sensor s, VirtualSensorListener listener, float[] values, int accuracy, long timestamp, float accResolution, float magResolution) {
if (s.getType() == Sensor.TYPE_ACCELEROMETER) {
if (Util.checkSensorResolution(this.accelerometerValues, values, accResolution)) {
this.accelerometerValues = values;
}
if (listener.getSensor() != null || listener.isDummyGyroListener) {
return this.getSensorValues(listener, timestamp);
}
} else if (s.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
if (Util.checkSensorResolution(this.magneticValues, values, magResolution)) {
this.magneticValues = values;
}
}
return null;
}
@Override
public void onAccuracyChanged(final Sensor pSensor, final int pAccuracy) {
if(this.mRunning) {
switch(pSensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
if(this.mAccelerationData != null) {
this.mAccelerationData.setAccuracy(pAccuracy);
this.mAccelerationListener.onAccelerationAccuracyChanged(this.mAccelerationData);
} else if(this.mOrientationData != null) {
this.mOrientationData.setAccelerationAccuracy(pAccuracy);
this.mOrientationListener.onOrientationAccuracyChanged(this.mOrientationData);
}
break;
case Sensor.TYPE_MAGNETIC_FIELD:
this.mOrientationData.setMagneticFieldAccuracy(pAccuracy);
this.mOrientationListener.onOrientationAccuracyChanged(this.mOrientationData);
break;
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_PRESSURE) {
if (accuracy == SensorManager.SENSOR_STATUS_NO_CONTACT || accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
setValue(Gauge.TYPE_ELEVATION, Float.NaN);
}
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor sensor = sensorEvent.sensor;
if (sensor.getType() == TYPE_ACCELEROMETER) {
mAccelerometerMatrix = sensorEvent.values;
} else if (sensor.getType() == TYPE_GYROSCOPE) {
mGyroscopeMatrix = sensorEvent.values;
}
}
/**
* {@inheritDoc}
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor == null) throw new NullPointerException();
if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
Log.e(TAG, "Compass data unreliable");
}
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
Log.i(TAG, "onSensorChanged");
if (mySensor.getType() == Sensor.TYPE_STEP_COUNTER) {
WritableMap map = Arguments.createMap();
long curTime = System.currentTimeMillis();
//i++;
if ((curTime - lastUpdate) > delay) {
final Object o = sensorEvent.values[0];
Log.i("History", "Data point:" + sensorEvent.values[0]);
map.putDouble("steps", sensorEvent.values[0]);
sendEvent(this.mReactContext, "StepSensorChangedEvent", map);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mReactContext.getApplicationContext(), "" + o, Toast.LENGTH_SHORT).show();
}
});
lastUpdate = curTime;
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mHasInterference = (accuracy < SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
notifyAccuracyChanged();
}
}
/**
* Called when the accuracy of the sensor has changed.
*
* @param sensor
* @param accuracy
*/
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Only look at accelerometer events
if (sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
}
// If not running, then just return
if (this.status == AccelListener.STOPPED) {
return;
}
this.accuracy = accuracy;
}
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_LIGHT) {
Log.i(TAG, "Light sensor connected");
mSensorManager.registerListener(LuxActivity.this,
sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
private void checkAndRegisterSensor(int sensorType){
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
Sensor sensor;
for (int index = 0; index < sensors.size(); ++index) {
sensor = sensors.get(index);
if (sensor.getType() == sensorType) {
mSensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
}
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
synchronized (this) {
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
calc_step(event);
}
}
}
@Override
public void onSensorChanged(final SensorEvent pEvent) {
if (this.mRunning) {
final Sensor sensor = pEvent.sensor;
final int sensorType = sensor.getType();
switch (sensorType) {
case Sensor.TYPE_ACCELEROMETER:
if (this.mAccelerationData != null) {
this.mAccelerationData.setDisplayRotation(this.getDisplayOrientation());
this.mAccelerationData.setValues(pEvent.values);
this.mAccelerationListener.onAccelerationChanged(this.mAccelerationData);
} else if (this.mOrientationData != null) {
this.mOrientationData.setDisplayRotation(this.getDisplayOrientation());
this.mOrientationData.setAccelerationValues(pEvent.values);
this.mOrientationListener.onOrientationChanged(this.mOrientationData);
}
break;
case Sensor.TYPE_MAGNETIC_FIELD:
this.mOrientationData.setDisplayRotation(this.getDisplayOrientation());
this.mOrientationData.setMagneticFieldValues(pEvent.values);
this.mOrientationListener.onOrientationChanged(this.mOrientationData);
break;
default:
throw new IllegalArgumentException("Unexpected " + Sensor.class.getSimpleName() + " of Type: '" + sensorType + "'.");
}
}
}
public static String generateUniqueSensorKey(Sensor sensor) {
//TODO Maybe use Sensor.toString?
return sensor.getName()
+ SENSOR_SEPARATOR
+ sensor.getVendor()
+ SENSOR_SEPARATOR
+ sensor.getVersion()
+ SENSOR_SEPARATOR
+ sensor.getType();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
int SensorType = sensor.getType();
switch (SensorType) {
case Sensor.TYPE_GRAVITY:
m_GravityAccuracy = accuracy;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
m_MagneticFieldAccuracy = accuracy;
break;
}
if (m_parent != null) m_parent.onAccuracyChanged(sensor, accuracy);
}
public static float getMinimumValueForSensor(Sensor sensor) {
float minimumValue;
switch (sensor.getType()) {
case Sensor.TYPE_HEART_RATE:
case Sensor.TYPE_LIGHT:
case Sensor.TYPE_PROXIMITY:
case Sensor.TYPE_STEP_COUNTER:
case Sensor.TYPE_PRESSURE:
minimumValue = 0;
break;
default:
minimumValue = -sensor.getMaximumRange();
}
return minimumValue;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == sensorId) {
}
}
@Override
public synchronized boolean getSnapshot(SensorMetrics snapshot) {
Utilities.checkNotNull(snapshot, "Null value passed to getSnapshot!");
if (!mEnabled) {
return false;
}
long currentTimeMs = SystemClock.elapsedRealtime();
snapshot.set(mCumulativeMetrics);
for (int i = 0, l = mActiveSensors.size(); i < l; i++) {
Sensor sensor = mActiveSensors.keyAt(i);
SensorData data = mActiveSensors.valueAt(i);
if (data.activeCount <= 0) {
continue;
}
long sensorActiveTimeMs = currentTimeMs - data.startTimeMs;
double sensorPowerMah = energyConsumedMah(sensor, sensorActiveTimeMs);
snapshot.total.activeTimeMs += sensorActiveTimeMs;
snapshot.total.powerMah += sensorPowerMah;
boolean isWakeupSensor = Util.isWakeupSensor(sensor);
if (isWakeupSensor) {
snapshot.total.wakeUpTimeMs += sensorActiveTimeMs;
}
if (snapshot.isAttributionEnabled) {
int type = sensor.getType();
SensorMetrics.Consumption consumption = snapshot.sensorConsumption.get(type);
if (consumption == null) {
consumption = new SensorMetrics.Consumption();
snapshot.sensorConsumption.put(type, consumption);
}
consumption.activeTimeMs += sensorActiveTimeMs;
consumption.powerMah += sensorPowerMah;
if (isWakeupSensor) {
consumption.wakeUpTimeMs += sensorActiveTimeMs;
}
}
}
return true;
}
private List<AdvertisedSensor> buildSensors() {
List<AdvertisedSensor> sensors = new ArrayList<>();
final List<Sensor> deviceSensors = getSensorManager().getSensorList(Sensor.TYPE_ALL);
for (final Sensor sensor : deviceSensors) {
final SensorAppearanceResources appearance = new SensorAppearanceResources();
String name = sensor.getName();
final SensorBehavior behavior = new SensorBehavior();
behavior.loggingId = name;
behavior.settingsIntent =
DeviceSettingsPopupActivity.getPendingIntent(AllNativeSensorProvider.this, sensor);
final int sensorType = sensor.getType();
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
appearance.iconId = android.R.drawable.ic_media_ff;
appearance.units = "ms/2";
appearance.shortDescription = "Not really a 3-axis accelerometer";
behavior.shouldShowSettingsOnConnect = true;
}
if (isTemperature(sensorType)) {
appearance.iconId = android.R.drawable.star_on;
String unitString =
TemperatureSettingsPopupActivity.getUnitString(AllNativeSensorProvider.this);
appearance.units = unitString;
appearance.shortDescription = "Ambient temperature (settings to change units!)";
behavior.settingsIntent =
TemperatureSettingsPopupActivity.getPendingIntent(AllNativeSensorProvider.this, sensor);
}
String sensorAddress = "" + sensorType;
sensors.add(
new AdvertisedSensor(sensorAddress, name) {
private SensorEventListener mSensorEventListener;
@Override
protected SensorAppearanceResources getAppearance() {
return appearance;
}
@Override
protected SensorBehavior getBehavior() {
return behavior;
}
@Override
protected boolean connect() throws Exception {
unregister();
return true;
}
@Override
protected void streamData(final DataConsumer c) {
final int index =
DeviceSettingsPopupActivity.getIndexForSensorType(
sensorType, AllNativeSensorProvider.this);
mSensorEventListener = new HardwareEventListener(sensorType, index, c);
getSensorManager()
.registerListener(mSensorEventListener, sensor, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void disconnect() {
unregister();
}
private void unregister() {
if (mSensorEventListener != null) {
getSensorManager().unregisterListener(mSensorEventListener);
mSensorEventListener = null;
}
}
});
}
return sensors;
}
@SuppressWarnings({ "unchecked", "deprecation" })
public EnvironmentalSucker(Context context) {
super(context);
setSucker(this);
sm = (SensorManager)context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
availableSensors = sm.getSensorList(Sensor.TYPE_ALL);
for(Sensor s : availableSensors) {
switch(s.getType()) {
case Sensor.TYPE_AMBIENT_TEMPERATURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_PRESSURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_LIGHT:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_TEMPERATURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
}
}
setTask(new TimerTask() {
@Override
public void run() {
if(currentAmbientTemp != null)
sendToBuffer(currentAmbientTemp);
if(currentDeviceTemp != null)
sendToBuffer(currentDeviceTemp);
if(currentHumidity != null)
sendToBuffer(currentHumidity);
if(currentPressure != null)
sendToBuffer(currentPressure);
if(currentLight != null)
sendToBuffer(currentLight);
}
});
getTimer().schedule(getTask(), 0, Environment.LOG_RATE);
}
public static String getDescription(Sensor sensor) {
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.";
case Sensor.TYPE_AMBIENT_TEMPERATURE:
return "Measures the ambient room temperature in degrees Celsius (°C).";
case Sensor.TYPE_GRAVITY:
return "Measures the force of gravity in m/s² that is applied to a device on all three physical axes (x, y, z).";
case Sensor.TYPE_GYROSCOPE:
return "Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).";
case Sensor.TYPE_HEART_RATE:
return "Measures heart rate.";
case Sensor.TYPE_LIGHT:
return "Measures the ambient light level (illumination) in lx.";
case Sensor.TYPE_LINEAR_ACCELERATION:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.";
case Sensor.TYPE_MAGNETIC_FIELD:
return "Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT.";
case Sensor.TYPE_PRESSURE:
return "Measures the ambient air pressure in hPa or mbar.";
case Sensor.TYPE_PROXIMITY:
return "Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.";
case Sensor.TYPE_RELATIVE_HUMIDITY:
return "Measures the relative ambient humidity in percent (%).";
case Sensor.TYPE_ROTATION_VECTOR:
return "Measures the orientation of a device by providing the three elements of the device's rotation vector.";
case Sensor.TYPE_ORIENTATION:
return "Measures degrees of rotation that a device makes around all three physical axes (x, y, z). ";
case Sensor.TYPE_TEMPERATURE:
return "Measures the temperature of the device in degrees Celsius (°C). ";
default:
return "Information about this sensor is unavailable.";
}
}