Java Code Examples for android.hardware.Sensor#TYPE_STEP_DETECTOR

The following examples show how to use android.hardware.Sensor#TYPE_STEP_DETECTOR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StepDetector.java    From NewFastFrame with Apache License 2.0 11 votes vote down vote up
public void dealSensorEvent(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        detectorNewStep(sensorEvent);
    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
        int step = (int) sensorEvent.values[0];
        if (systemStepCount == 0) {
            systemStepCount = step;
        } else {
            int add = step - systemStepCount - tempStep;
            stepCount += add;
            tempStep = step - systemStepCount;
            callBack.countStep(stepCount);
        }
    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        if (sensorEvent.values[0] == 1.0f) {
            stepCount++;
            callBack.countStep(stepCount);
        }
    }
}
 
Example 2
Source File: HardwareStepDetectorService.java    From privacy-friendly-pedometer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_STEP_DETECTOR:
            this.onStepDetected(1);
            break;
        case Sensor.TYPE_STEP_COUNTER:
            if (this.mStepOffset < 0) {
                this.mStepOffset = event.values[0];
            }
            if (this.mStepOffset > event.values[0]) {
                // this should never happen?
                return;
            }
            // publish difference between last known step count and the current ones.
            this.onStepDetected((int) (event.values[0] - mStepOffset));
            // Set offset to current value so we know it at next event
            mStepOffset = event.values[0];
            break;
    }
}
 
Example 3
Source File: StepService.java    From LLApp with Apache License 2.0 6 votes vote down vote up
/**
 * 添加传感器监听
 * 1. TYPE_STEP_COUNTER API的解释说返回从开机被激活后统计的步数,当重启手机后该数据归零,
 * 该传感器是一个硬件传感器所以它是低功耗的。
 * 为了能持续的计步,请不要反注册事件,就算手机处于休眠状态它依然会计步。
 * 当激活的时候依然会上报步数。该sensor适合在长时间的计步需求。
 * <p>
 * 2.TYPE_STEP_DETECTOR翻译过来就是走路检测,
 * API文档也确实是这样说的,该sensor只用来监监测走步,每次返回数字1.0。
 * 如果需要长事件的计步请使用TYPE_STEP_COUNTER。
 */
private void addCountStepListener() {
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    Sensor detectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
    if (countSensor != null) {
        stepSensorType = Sensor.TYPE_STEP_COUNTER;
        Log.v(TAG, "Sensor.TYPE_STEP_COUNTER");
        sensorManager.registerListener(StepService.this, countSensor, SensorManager.SENSOR_DELAY_NORMAL);
    } else if (detectorSensor != null) {
        stepSensorType = Sensor.TYPE_STEP_DETECTOR;
        Log.v(TAG, "Sensor.TYPE_STEP_DETECTOR");
        sensorManager.registerListener(StepService.this, detectorSensor, SensorManager.SENSOR_DELAY_NORMAL);
    } else {
        Log.v(TAG, "Count sensor not available!");
        addBasePedometerListener();
    }
}
 
Example 4
Source File: HardwareStepDetectorService.java    From privacy-friendly-pedometer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("InlinedApi")
@Override
public int getSensorType() {
    if (AndroidVersionHelper.supportsStepDetector(getPackageManager())) {
        return Sensor.TYPE_STEP_DETECTOR;
    } else {
        return 0;
    }
}
 
Example 5
Source File: StepService.java    From LLApp with Apache License 2.0 5 votes vote down vote up
/**
     * 传感器监听回调
     * 记步的关键代码
     * 1. TYPE_STEP_COUNTER API的解释说返回从开机被激活后统计的步数,当重启手机后该数据归零,
     * 该传感器是一个硬件传感器所以它是低功耗的。
     * 为了能持续的计步,请不要反注册事件,就算手机处于休眠状态它依然会计步。
     * 当激活的时候依然会上报步数。该sensor适合在长时间的计步需求。
     * <p>
     * 2.TYPE_STEP_DETECTOR翻译过来就是走路检测,
     * API文档也确实是这样说的,该sensor只用来监监测走步,每次返回数字1.0。
     * 如果需要长事件的计步请使用TYPE_STEP_COUNTER。
     *
     * @param event
     */
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (stepSensorType == Sensor.TYPE_STEP_COUNTER) {
            //获取当前传感器返回的临时步数
            int tempStep = (int) event.values[0];
            //首次如果没有获取手机系统中已有的步数则获取一次系统中APP还未开始记步的步数
            if (!hasRecord) {
                hasRecord = true;
                hasStepCount = tempStep;
            } else {
                //获取APP打开到现在的总步数=本次系统回调的总步数-APP打开之前已有的步数
                int thisStepCount = tempStep - hasStepCount;
                //本次有效步数=(APP打开后所记录的总步数-上一次APP打开后所记录的总步数)
                int thisStep = thisStepCount - previousStepCount;
                //总步数=现有的步数+本次有效步数
                CURRENT_STEP += (thisStep);
                //记录最后一次APP打开到现在的总步数
                previousStepCount = thisStepCount;
            }
//            Logger.d("tempStep" + tempStep);
        } else if (stepSensorType == Sensor.TYPE_STEP_DETECTOR) {
            if (event.values[0] == 1.0) {
                CURRENT_STEP++;
            }
        }
        updateNotification();
    }
 
Example 6
Source File: MainActivity.java    From journaldev with MIT License 5 votes vote down vote up
public void stepCounterOnClick(View view) {
    if (checkSensorAvailability(Sensor.TYPE_STEP_DETECTOR)) {
        currentSensor = Sensor.TYPE_STEP_DETECTOR;
    } else {
        textView.setText("Step Counter Sensor not available");
    }
}
 
Example 7
Source File: BatchStepSensorFragment.java    From sensors-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    // BEGIN_INCLUDE(sensorevent)
    // store the delay of this event
    recordDelay(event);
    final String delayString = getDelayString();

    if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        // A step detector event is received for each step.
        // This means we need to count steps ourselves

        mSteps += event.values.length;

        // Update the card with the latest step count
        getCardStream().getCard(CARD_COUNTING)
                .setTitle(getString(R.string.counting_title, mSteps))
                .setDescription(getString(R.string.counting_description,
                        getString(R.string.sensor_detector), mMaxDelay, delayString));

        Log.i(TAG,
                "New step detected by STEP_DETECTOR sensor. Total step count: " + mSteps);

    } else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {

        /*
        A step counter event contains the total number of steps since the listener
        was first registered. We need to keep track of this initial value to calculate the
        number of steps taken, as the first value a listener receives is undefined.
         */
        if (mCounterSteps < 1) {
            // initial value
            mCounterSteps = (int) event.values[0];
        }

        // Calculate steps taken based on first counter value received.
        mSteps = (int) event.values[0] - mCounterSteps;

        // Add the number of steps previously taken, otherwise the counter would start at 0.
        // This is needed to keep the counter consistent across rotation changes.
        mSteps = mSteps + mPreviousCounterSteps;

        // Update the card with the latest step count
        getCardStream().getCard(CARD_COUNTING)
                .setTitle(getString(R.string.counting_title, mSteps))
                .setDescription(getString(R.string.counting_description,
                        getString(R.string.sensor_counter), mMaxDelay, delayString));
        Log.i(TAG, "New step detected by STEP_COUNTER sensor. Total step count: " + mSteps);
        // END_INCLUDE(sensorevent)
    }
}
 
Example 8
Source File: PStep.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
public PStep(AppRunner appRunner) {
    super(appRunner);
    type = Sensor.TYPE_STEP_DETECTOR;
}
 
Example 9
Source File: MainActivity.java    From journaldev with MIT License 4 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == currentSensor) {

        if (currentSensor == Sensor.TYPE_LIGHT) {
            float valueZ = event.values[0];
            textView.setText("Brightness " + valueZ);
        } else if (currentSensor == Sensor.TYPE_PROXIMITY) {
            float distance = event.values[0];
            textView.setText("Proximity " + distance);
        } else if (currentSensor == Sensor.TYPE_STEP_DETECTOR) {
            float steps = event.values[0];
            textView.setText("Steps : " + steps);
        } else if (currentSensor == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            long curTime = System.currentTimeMillis();

            if ((curTime - lastUpdate) > 100) {
                long diffTime = (curTime - lastUpdate);
                lastUpdate = curTime;

                float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;

                if (speed > SHAKE_THRESHOLD) {
                    Toast.makeText(getApplicationContext(), "Your phone just shook", Toast.LENGTH_LONG).show();
                }

                last_x = x;
                last_y = y;
                last_z = z;
            }
        } else if (currentSensor == Sensor.TYPE_GYROSCOPE) {
            if (event.values[2] > 0.5f) {
                textView.setText("Anti Clock");
            } else if (event.values[2] < -0.5f) {
                textView.setText("Clock");
            }
        } else if (currentSensor == Sensor.TYPE_AMBIENT_TEMPERATURE) {
            textView.setText("Ambient Temp in Celsius :" + event.values[0]);
        }

    }

}
 
Example 10
Source File: BatchStepSensorFragment.java    From android-BatchStepSensor with Apache License 2.0 4 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    // BEGIN_INCLUDE(sensorevent)
    // store the delay of this event
    recordDelay(event);
    final String delayString = getDelayString();

    if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        // A step detector event is received for each step.
        // This means we need to count steps ourselves

        mSteps += event.values.length;

        // Update the card with the latest step count
        getCardStream().getCard(CARD_COUNTING)
                .setTitle(getString(R.string.counting_title, mSteps))
                .setDescription(getString(R.string.counting_description,
                        getString(R.string.sensor_detector), mMaxDelay, delayString));

        Log.i(TAG,
                "New step detected by STEP_DETECTOR sensor. Total step count: " + mSteps);

    } else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {

        /*
        A step counter event contains the total number of steps since the listener
        was first registered. We need to keep track of this initial value to calculate the
        number of steps taken, as the first value a listener receives is undefined.
         */
        if (mCounterSteps < 1) {
            // initial value
            mCounterSteps = (int) event.values[0];
        }

        // Calculate steps taken based on first counter value received.
        mSteps = (int) event.values[0] - mCounterSteps;

        // Add the number of steps previously taken, otherwise the counter would start at 0.
        // This is needed to keep the counter consistent across rotation changes.
        mSteps = mSteps + mPreviousCounterSteps;

        // Update the card with the latest step count
        getCardStream().getCard(CARD_COUNTING)
                .setTitle(getString(R.string.counting_title, mSteps))
                .setDescription(getString(R.string.counting_description,
                        getString(R.string.sensor_counter), mMaxDelay, delayString));
        Log.i(TAG, "New step detected by STEP_COUNTER sensor. Total step count: " + mSteps);
        // END_INCLUDE(sensorevent)
    }
}
 
Example 11
Source File: SensorUtil.java    From Sensor-Disabler with MIT License 4 votes vote down vote up
@Nullable
public static String getHumanStringType(Sensor sensor) {
    switch (sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            return "Accelerometer";

        case Sensor.TYPE_AMBIENT_TEMPERATURE:
            return "Ambient Temperature";

        case Sensor.TYPE_GAME_ROTATION_VECTOR:
            return "Game Rotation Vector";

        case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
            return "Geomagnetic Rotation Vector";

        case Sensor.TYPE_GRAVITY:
            return "Gravity";

        case Sensor.TYPE_GYROSCOPE:
            return "Gyroscope";

        case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
            return "Gyroscope (Uncalibrated)";

        case Sensor.TYPE_HEART_RATE:
            return "Heart Rate";

        case Sensor.TYPE_LIGHT:
            return "Light";

        case Sensor.TYPE_LINEAR_ACCELERATION:
            return "Linear Acceleration";

        case Sensor.TYPE_MAGNETIC_FIELD:
            return "Magnetic Field";

        case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
            return "Magnetic Field (Uncalibrated)";

        case Sensor.TYPE_PRESSURE:
            return "Pressure";

        case Sensor.TYPE_PROXIMITY:
            return "Proximity";

        case Sensor.TYPE_RELATIVE_HUMIDITY:
            return "Relative Humidity";

        case Sensor.TYPE_ROTATION_VECTOR:
            return "Rotation Vector";

        case Sensor.TYPE_SIGNIFICANT_MOTION:
            return "Significant Motion";

        case Sensor.TYPE_STEP_COUNTER:
            return "Step Counter";

        case Sensor.TYPE_STEP_DETECTOR:
            return "Step Detector";

        case Sensor.TYPE_ORIENTATION:
            return "Orientation";

        case Sensor.TYPE_TEMPERATURE:
            return "Temperature";
    }
    return null;
}
 
Example 12
Source File: SensorDetailFragment.java    From AndroidDemoProjects with Apache License 2.0 4 votes vote down vote up
private void populateTypeField( int type ) {
	if( type == 0 || mTypeRow == null || mType == null )
		return;

	String typeName;

	switch( type ) {
		case Sensor.TYPE_ACCELEROMETER: {
			typeName = "Accelerometer";
			break;
		}
		case Sensor.TYPE_AMBIENT_TEMPERATURE: {
			typeName = "Ambient Temperature";
			break;
		}
		case Sensor.TYPE_GAME_ROTATION_VECTOR: {
			typeName = "Game Rotation Vector";
			break;
		}
		case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: {
			typeName = "Geomagnetic Rotation Vector";
			break;
		}
		case Sensor.TYPE_GRAVITY: {
			typeName = "Gravity";
			break;
		}
		case Sensor.TYPE_GYROSCOPE: {
			typeName = "Gyroscope";
			break;
		}
		case Sensor.TYPE_GYROSCOPE_UNCALIBRATED: {
			typeName = "Uncalibrated Gyroscope";
			break;
		}
		case Sensor.TYPE_LIGHT: {
			typeName = "Light";
			break;
		}
		case Sensor.TYPE_LINEAR_ACCELERATION: {
			typeName = "Linear Acceleration";
			break;
		}
		case Sensor.TYPE_MAGNETIC_FIELD: {
			typeName = "Magnetic Field";
			break;
		}
		case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: {
			typeName = "Uncalibrated Magnetic Field";
			break;
		}
		case Sensor.TYPE_PRESSURE: {
			typeName = "Pressure";
			break;
		}
		case Sensor.TYPE_PROXIMITY: {
			typeName = "Proximity";
			break;
		}
		case Sensor.TYPE_RELATIVE_HUMIDITY: {
			typeName = "Relative Humidity";
			break;
		}
		case Sensor.TYPE_ROTATION_VECTOR: {
			typeName = "Rotation Vector";
			break;
		}
		case Sensor.TYPE_SIGNIFICANT_MOTION: {
			typeName = "Significant Motion";
			break;
		}
		case Sensor.TYPE_STEP_COUNTER: {
			typeName = "Step Counter";
			break;
		}
		case Sensor.TYPE_STEP_DETECTOR: {
			typeName = "Step Detector";
			break;
		}
		default: {
			typeName = "Other";
		}
	}
	mType.setText( typeName );
	mTypeRow.setVisibility( View.VISIBLE );
}
 
Example 13
Source File: SKAbstractNativeSensorModule.java    From SensingKit-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressLint("InlinedApi")  // There is a check in STEP_DETECTOR and STEP_COUNTER
private static int getSensorType(SKSensorModuleType sensorType) throws SKException{

    switch (sensorType) {

        case ACCELEROMETER:
            return Sensor.TYPE_ACCELEROMETER;

        case GRAVITY:
            return Sensor.TYPE_GRAVITY;

        case LINEAR_ACCELERATION:
            return Sensor.TYPE_LINEAR_ACCELERATION;

        case GYROSCOPE:
            return Sensor.TYPE_GYROSCOPE;

        case ROTATION:
            return Sensor.TYPE_ROTATION_VECTOR;

        case MAGNETOMETER:
            return Sensor.TYPE_MAGNETIC_FIELD;

        case AMBIENT_TEMPERATURE:
            return Sensor.TYPE_AMBIENT_TEMPERATURE;

        case STEP_DETECTOR:

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return Sensor.TYPE_STEP_DETECTOR;
            }
            else
            {
                throw new SKException(TAG, "STEP_DETECTOR requires Android KitKat or greater.", SKExceptionErrorCode.UNKNOWN_ERROR);
            }

        case STEP_COUNTER:

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return Sensor.TYPE_STEP_COUNTER;
            }
            else
            {
                throw new SKException(TAG, "STEP_COUNTER requires Android KitKat or greater.", SKExceptionErrorCode.UNKNOWN_ERROR);
            }

        case LIGHT:
            return Sensor.TYPE_LIGHT;

        case LOCATION:
        case ACTIVITY:
        case BATTERY:
            throw new SKException(TAG, "Not a native SensorModule.", SKExceptionErrorCode.UNKNOWN_ERROR);

        default:
            throw new SKException(TAG, "Unknown SensorModule", SKExceptionErrorCode.UNKNOWN_ERROR);

    }
}
 
Example 14
Source File: XSensorManager.java    From XPrivacy with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private boolean isRestricted(XParam param, int type) throws Throwable {
	if (type == Sensor.TYPE_ALL)
		return false;
	else if (type == Sensor.TYPE_ACCELEROMETER || type == Sensor.TYPE_LINEAR_ACCELERATION) {
		if (isRestricted(param, "acceleration"))
			return true;
	} else if (type == Sensor.TYPE_GRAVITY) {
		if (isRestricted(param, "gravity"))
			return true;
	} else if (type == Sensor.TYPE_RELATIVE_HUMIDITY) {
		if (isRestricted(param, "humidity"))
			return true;
	} else if (type == Sensor.TYPE_LIGHT) {
		if (isRestricted(param, "light"))
			return true;
	} else if (type == Sensor.TYPE_MAGNETIC_FIELD || type == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
		if (isRestricted(param, "magnetic"))
			return true;
	} else if (type == Sensor.TYPE_SIGNIFICANT_MOTION) {
		if (isRestricted(param, "motion"))
			return true;
	} else if (type == Sensor.TYPE_ORIENTATION || type == Sensor.TYPE_GYROSCOPE
			|| type == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) {
		if (isRestricted(param, "orientation"))
			return true;
	} else if (type == Sensor.TYPE_PRESSURE) {
		if (isRestricted(param, "pressure"))
			return true;
	} else if (type == Sensor.TYPE_PROXIMITY) {
		if (isRestricted(param, "proximity"))
			return true;
	} else if (type == Sensor.TYPE_GAME_ROTATION_VECTOR || type == Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR
			|| type == Sensor.TYPE_ROTATION_VECTOR) {
		if (isRestricted(param, "rotation"))
			return true;
	} else if (type == Sensor.TYPE_TEMPERATURE || type == Sensor.TYPE_AMBIENT_TEMPERATURE) {
		if (isRestricted(param, "temperature"))
			return true;
	} else if (type == Sensor.TYPE_STEP_COUNTER || type == Sensor.TYPE_STEP_DETECTOR) {
		if (isRestricted(param, "step"))
			return true;
	} else if (type == Sensor.TYPE_HEART_RATE) {
		if (isRestricted(param, "heartrate"))
			return true;
	} else if (type == 22) {
		// 22 = TYPE_TILT_DETECTOR
		// Do nothing
	} else if (type == 23 || type == 24 || type == 25) {
		// 23 = TYPE_WAKE_GESTURE
		// 24 = TYPE_GLANCE_GESTURE
		// 25 = TYPE_PICK_UP_GESTURE
		// 23/24 This sensor is expected to only be used by the system ui
		// 25 Expected to be used internally for always on display
	} else
		Util.log(this, Log.WARN, "Unknown sensor type=" + type);
	return false;
}