fix: set EventChannel stream handler to emit location updates to Flutter

This commit is contained in:
fiatcode 2026-04-30 11:16:55 +07:00
parent 45da511f87
commit 1241174705
No known key found for this signature in database
2 changed files with 26 additions and 0 deletions

View file

@ -20,10 +20,25 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
methodChannel.setMethodCallHandler(this)
eventChannel = EventChannel(binding.binaryMessenger, "com.traccar.client/location_updates")
eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
eventSink = events
}
override fun onCancel(arguments: Any?) {
eventSink = null
}
})
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
methodChannel.setMethodCallHandler(null)
eventChannel.setStreamHandler(null)
eventSink = null
}
companion object {
var eventSink: EventChannel.EventSink? = null
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {

View file

@ -13,6 +13,7 @@ import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import androidx.core.app.NotificationCompat
import com.traccar.traccar_client.BridgeModule
import com.traccar.traccar_client.MainActivity
import com.traccar.traccar_client.R
import com.traccar.traccar_client.location.DistanceFilterProcessor
@ -165,6 +166,16 @@ class LocationTrackingService : Service() {
updateNotification(location)
BridgeModule.eventSink?.success(mapOf(
"latitude" to location.latitude,
"longitude" to location.longitude,
"accuracy" to if (location.hasAccuracy()) location.accuracy else null,
"speed" to if (location.hasSpeed()) location.speed else null,
"heading" to if (location.hasBearing()) location.bearing else null,
"altitude" to if (location.hasAltitude()) location.altitude else null,
"timestamp" to location.time
))
if (isNetworkAvailable) {
sendLocationToServer(traccarLocation)
} else {