fix: set EventChannel stream handler to emit location updates to Flutter
This commit is contained in:
parent
45da511f87
commit
1241174705
2 changed files with 26 additions and 0 deletions
|
|
@ -20,10 +20,25 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
|
||||||
methodChannel.setMethodCallHandler(this)
|
methodChannel.setMethodCallHandler(this)
|
||||||
|
|
||||||
eventChannel = EventChannel(binding.binaryMessenger, "com.traccar.client/location_updates")
|
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) {
|
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||||
methodChannel.setMethodCallHandler(null)
|
methodChannel.setMethodCallHandler(null)
|
||||||
|
eventChannel.setStreamHandler(null)
|
||||||
|
eventSink = null
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
var eventSink: EventChannel.EventSink? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
import com.traccar.traccar_client.BridgeModule
|
||||||
import com.traccar.traccar_client.MainActivity
|
import com.traccar.traccar_client.MainActivity
|
||||||
import com.traccar.traccar_client.R
|
import com.traccar.traccar_client.R
|
||||||
import com.traccar.traccar_client.location.DistanceFilterProcessor
|
import com.traccar.traccar_client.location.DistanceFilterProcessor
|
||||||
|
|
@ -165,6 +166,16 @@ class LocationTrackingService : Service() {
|
||||||
|
|
||||||
updateNotification(location)
|
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) {
|
if (isNetworkAvailable) {
|
||||||
sendLocationToServer(traccarLocation)
|
sendLocationToServer(traccarLocation)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue