Skip to content

Commit

Permalink
feat(sensor_plus): Add support for plateform timestamp Android
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Now all events have a `DateTime` property `timestamp` as constructor 3rd constructor parameter
  • Loading branch information
Ortes committed Jan 4, 2024
1 parent 5e5c9db commit 87483fe
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.SystemClock
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink

Expand All @@ -15,6 +16,8 @@ internal class StreamHandlerImpl(

private var sensor: Sensor? = null

private var timestampMicroAtBoot: Long = System.currentTimeMillis() * 1000 - SystemClock.elapsedRealtimeNanos() / 1000

var samplingPeriod = 200000
set(value) {
field = value
Expand Down Expand Up @@ -64,10 +67,14 @@ internal class StreamHandlerImpl(
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}

override fun onSensorChanged(event: SensorEvent) {
val sensorValues = DoubleArray(event.values.size)
val sensorValues = DoubleArray(event.values.size + 1)
event.values.forEachIndexed { index, value ->
sensorValues[index] = value.toDouble()
}

val timestampMicro = timestampMicroAtBoot + (event.timestamp / 1000)
sensorValues[event.values.size] = timestampMicro.toDouble()

events.success(sensorValues)
}
}
Expand Down
12 changes: 8 additions & 4 deletions packages/sensors_plus/sensors_plus/lib/src/web_sensors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class WebSensorsPlugin extends SensorsPlatform {
accelerometer.x as double,
accelerometer.y as double,
accelerometer.z as double,
DateTime.now(),
),
);
},
Expand All @@ -93,7 +94,7 @@ class WebSensorsPlugin extends SensorsPlatform {
apiName: 'Accelerometer()',
permissionName: 'accelerometer',
onError: () {
_accelerometerStreamController!.add(AccelerometerEvent(0, 0, 0));
_accelerometerStreamController!.add(AccelerometerEvent(0, 0, 0, DateTime.now()));
},
);
_accelerometerResultStream =
Expand Down Expand Up @@ -131,6 +132,7 @@ class WebSensorsPlugin extends SensorsPlatform {
gyroscope.x as double,
gyroscope.y as double,
gyroscope.z as double,
DateTime.now(),
),
);
},
Expand All @@ -148,7 +150,7 @@ class WebSensorsPlugin extends SensorsPlatform {
apiName: 'Gyroscope()',
permissionName: 'gyroscope',
onError: () {
_gyroscopeEventStreamController!.add(GyroscopeEvent(0, 0, 0));
_gyroscopeEventStreamController!.add(GyroscopeEvent(0, 0, 0, DateTime.now()));
},
);
_gyroscopeEventResultStream =
Expand Down Expand Up @@ -187,6 +189,7 @@ class WebSensorsPlugin extends SensorsPlatform {
linearAccelerationSensor.x as double,
linearAccelerationSensor.y as double,
linearAccelerationSensor.z as double,
DateTime.now(),
),
);
},
Expand All @@ -205,7 +208,7 @@ class WebSensorsPlugin extends SensorsPlatform {
permissionName: 'accelerometer',
onError: () {
_userAccelerometerStreamController!
.add(UserAccelerometerEvent(0, 0, 0));
.add(UserAccelerometerEvent(0, 0, 0, DateTime.now()));
},
);
_userAccelerometerResultStream =
Expand Down Expand Up @@ -243,6 +246,7 @@ class WebSensorsPlugin extends SensorsPlatform {
magnetometerSensor.x as double,
magnetometerSensor.y as double,
magnetometerSensor.z as double,
DateTime.now(),
),
);
},
Expand All @@ -260,7 +264,7 @@ class WebSensorsPlugin extends SensorsPlatform {
apiName: 'Magnetometer()',
permissionName: 'magnetometer',
onError: () {
_magnetometerStreamController!.add(MagnetometerEvent(0, 0, 0));
_magnetometerStreamController!.add(MagnetometerEvent(0, 0, 0, DateTime.now()));
},
);
_magnetometerResultStream =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// a particular direction.
class AccelerometerEvent {
/// Constructs an instance with the given [x], [y], and [z] values.
AccelerometerEvent(this.x, this.y, this.z);
AccelerometerEvent(this.x, this.y, this.z, this.timestamp);

/// Acceleration force along the x axis (including gravity) measured in m/s^2.
///
Expand All @@ -30,6 +30,14 @@ class AccelerometerEvent {
/// towards the user and negative mean it is moving away from them.
final double z;

/// timestamp of the event
///
/// This is the timestamp of the event in microseconds, as provided by the
/// underlying platform. For Android, this is the uptimeMillis provided by
/// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion.
final DateTime timestamp;

@override
String toString() => '[AccelerometerEvent (x: $x, y: $y, z: $z)]';
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// the device in 3D space.
class GyroscopeEvent {
/// Constructs an instance with the given [x], [y], and [z] values.
GyroscopeEvent(this.x, this.y, this.z);
GyroscopeEvent(this.x, this.y, this.z, this.timestamp);

/// Rate of rotation around the x axis measured in rad/s.
///
Expand All @@ -30,6 +30,14 @@ class GyroscopeEvent {
/// on.
final double z;

/// timestamp of the event
///
/// This is the timestamp of the event in microseconds, as provided by the
/// underlying platform. For Android, this is the uptimeMillis provided by
/// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion.
final DateTime timestamp;

@override
String toString() => '[GyroscopeEvent (x: $x, y: $y, z: $z)]';
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ class MagnetometerEvent {
/// Constructs a new instance with the given [x], [y], and [z] values.
///
/// See [MagnetometerEvent] for more information.
MagnetometerEvent(this.x, this.y, this.z);
MagnetometerEvent(this.x, this.y, this.z, this.timestamp);

/// The ambient magnetic field in this axis surrounding the sensor in
/// microteslas ***μT***.
final double x, y, z;

/// timestamp of the event
///
/// This is the timestamp of the event in microseconds, as provided by the
/// underlying platform. For Android, this is the uptimeMillis provided by
/// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion.
final DateTime timestamp;

@override
String toString() => '[MagnetometerEvent (x: $x, y: $y, z: $z)]';
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MethodChannelSensors extends SensorsPlatform {
.receiveBroadcastStream()
.map((dynamic event) {
final list = event.cast<double>();
return AccelerometerEvent(list[0]!, list[1]!, list[2]!);
return AccelerometerEvent(list[0]!, list[1]!, list[2]!, DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()));
});
return _accelerometerEvents!;
}
Expand All @@ -77,7 +77,7 @@ class MethodChannelSensors extends SensorsPlatform {
_gyroscopeEvents ??=
_gyroscopeEventChannel.receiveBroadcastStream().map((dynamic event) {
final list = event.cast<double>();
return GyroscopeEvent(list[0]!, list[1]!, list[2]!);
return GyroscopeEvent(list[0]!, list[1]!, list[2]!, DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()));
});
return _gyroscopeEvents!;
}
Expand All @@ -104,7 +104,7 @@ class MethodChannelSensors extends SensorsPlatform {
.receiveBroadcastStream()
.map((dynamic event) {
final list = event.cast<double>();
return UserAccelerometerEvent(list[0]!, list[1]!, list[2]!);
return UserAccelerometerEvent(list[0]!, list[1]!, list[2]!, DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()));
});
return _userAccelerometerEvents!;
}
Expand All @@ -129,7 +129,7 @@ class MethodChannelSensors extends SensorsPlatform {
_magnetometerEvents ??=
_magnetometerEventChannel.receiveBroadcastStream().map((dynamic event) {
final list = event.cast<double>();
return MagnetometerEvent(list[0]!, list[1]!, list[2]!);
return MagnetometerEvent(list[0]!, list[1]!, list[2]!, DateTime.fromMicrosecondsSinceEpoch(list[3]!.toInt()));
});
return _magnetometerEvents!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// [AccelerometerEvent], this event does not include the effects of gravity.
class UserAccelerometerEvent {
/// Constructs an instance with the given [x], [y], and [z] values.
UserAccelerometerEvent(this.x, this.y, this.z);
UserAccelerometerEvent(this.x, this.y, this.z, this.timestamp);

/// Acceleration force along the x axis (excluding gravity) measured in m/s^2.
///
Expand All @@ -29,6 +29,14 @@ class UserAccelerometerEvent {
/// towards the user and negative mean it is moving away from them.
final double z;

/// timestamp of the event
///
/// This is the timestamp of the event in microseconds, as provided by the
/// underlying platform. For Android, this is the uptimeMillis provided by
/// the SensorEvent. For iOS, this is the timestamp provided by the CMDeviceMotion.
final DateTime timestamp;

@override
String toString() => '[UserAccelerometerEvent (x: $x, y: $y, z: $z)]';
}

0 comments on commit 87483fe

Please sign in to comment.