startListening method

void startListening()

Starts listening to accelerometer events

Implementation

void startListening() {
  streamSubscription = accelerometerEvents.listen((AccelerometerEvent event) {
    final x = event.x;
    final y = event.y;
    final z = event.z;

    final gX = x / 9.80665;
    final gY = y / 9.80665;
    final gZ = z / 9.80665;

    // gForce will be close to 1 when there is no movement.
    final gForce = sqrt(gX * gX + gY * gY + gZ * gZ);

    if (gForce > shakeThresholdGravity) {
      final now = DateTime.now().millisecondsSinceEpoch;
      // ignore shake events too close to each other (500ms)
      if (mShakeTimestamp + shakeSlopTimeMS > now) {
        return;
      }

      // reset the shake count after 3 seconds of no shakes
      if (mShakeTimestamp + shakeCountResetTime < now) {
        mShakeCount = 0;
      }

      mShakeTimestamp = now;
      mShakeCount++;

      onPhoneShake!();
    }
  });
}