onHeartbeat static method
Subscribe to periodic heartbeat events.
Your callback
will be executed for each Config.heartbeatInterval while the device is in stationary state (iOS requires Config.preventSuspend: true as well).
NOTE: The Location provided to the HeartbeatEvent is only the last-known location. The heartbeat event does not actively engage location-services. If you wish to get the current location in your callback
, use getCurrentPosition.
Example:
BackgroundGeolocation.ready(Config(
heartbeatInterval: 60
));
BackgroundGeolocation.onHeartbeat((HeartbeatEvent event) {
print('[onHeartbeat] ${event}');
// You could request a new location if you wish.
BackgroundGeolocation.getCurrentPosition(
samples: 1,
persist: true
).then((Location location) {
print('[getCurrentPosition] ${location}');
});
})
Implementation
static void onHeartbeat(Function(HeartbeatEvent) callback) {
if (_eventsHeartbeat == null) {
_eventsHeartbeat = _eventChannelHeartbeat
.receiveBroadcastStream()
.map((dynamic event) => HeartbeatEvent(event));
}
_registerSubscription(_eventsHeartbeat!.listen(callback), callback);
}