Implementing Vehicle Telematics in Mobile IoT Applications
Vehicle telematics collects data from moving objects: GPS coordinates, speed, mileage, driver behavior (hard acceleration, braking), fuel consumption, refrigerated body temperature. Data flows from onboard tracker via GPRS/LTE to server; mobile app is a dispatcher or fleet manager tool. Development breaks into three layers: tracker protocol, server platform, mobile client.
GPS Tracker Protocols
Trackers communicate via several common protocols:
| Protocol | Trackers | Transport |
|---|---|---|
| Teltonika codec 8/8E | Teltonika FMB920, FMC003 | TCP |
| Concox Protocol | Concox GT06, JT701 | TCP |
| GT06N (Gotop) | 90% cheap Chinese trackers | TCP |
| NMEA 0183 | Most GPS modules | RS-232/TCP |
| MQTT JSON | Modern IoT trackers | MQTT/TLS |
For parsing tracker protocols, use Traccar — open-source server platform supporting 200+ protocols. Traccar runs on your server, accepts tracker data and provides REST API + WebSocket for mobile clients. This is the most sensible starting path.
Traccar API: Android Integration
// Retrofit interface to Traccar API
interface TraccarApi {
@GET("devices")
suspend fun getDevices(
@Query("all") all: Boolean = false,
@Query("groupId") groupId: Long? = null,
): List<Device>
@GET("positions")
suspend fun getLatestPositions(
@Query("deviceId") deviceId: Long? = null,
): List<Position>
@GET("reports/trips")
suspend fun getTrips(
@Query("deviceId") deviceId: Long,
@Query("from") from: String, // ISO 8601
@Query("to") to: String,
): List<Trip>
}
data class Position(
val id: Long,
val deviceId: Long,
val latitude: Double,
val longitude: Double,
val speed: Double, // knots, convert to km/h * 1.852
val course: Double,
val altitude: Double,
val accuracy: Double,
val fixTime: String,
val valid: Boolean,
val attributes: Map<String, Any>, // battery, ignition, mileage, etc.
)
Real-time updates via Traccar WebSocket:
class TraccarWebSocketClient(private val baseUrl: String, private val token: String) {
private val okHttpClient = OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS) // infinite timeout for WS
.build()
fun connect(): Flow<TraccarEvent> = callbackFlow {
val request = Request.Builder()
.url("wss://${baseUrl}/api/socket")
.header("Cookie", "JSESSIONID=$token")
.build()
val ws = okHttpClient.newWebSocket(request, object : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
val event = json.decodeFromString<TraccarSocketMessage>(text)
event.positions?.forEach { trySend(TraccarEvent.Position(it)) }
event.devices?.forEach { trySend(TraccarEvent.DeviceUpdate(it)) }
event.events?.forEach { trySend(TraccarEvent.Alert(it)) }
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
close(t)
}
})
awaitClose { ws.close(1000, "Closed") }
}
}
Map with Live Markers
Google Maps SDK on Android with custom vehicle markers:
class FleetMapFragment : Fragment() {
private lateinit var map: GoogleMap
private val vehicleMarkers = HashMap<Long, Marker>()
private fun updateVehiclePosition(position: Position) {
val latLng = LatLng(position.latitude, position.longitude)
val marker = vehicleMarkers[position.deviceId]
if (marker == null) {
val newMarker = map.addMarker(
MarkerOptions()
.position(latLng)
.icon(getBitmapDescriptor(R.drawable.ic_truck, position.course))
.title(getVehicleName(position.deviceId))
)
vehicleMarkers[position.deviceId] = newMarker!!
} else {
// Animate marker movement
animateMarker(marker, latLng, position.course)
}
}
private fun animateMarker(marker: Marker, to: LatLng, bearing: Float) {
val animator = ValueAnimator.ofFloat(0f, 1f).apply {
duration = 1000
interpolator = LinearInterpolator()
}
val from = marker.position
animator.addUpdateListener { anim ->
val fraction = anim.animatedValue as Float
marker.position = LatLng(
from.latitude + (to.latitude - from.latitude) * fraction,
from.longitude + (to.longitude - from.longitude) * fraction,
)
marker.rotation = bearing
}
animator.start()
}
}
Marker animation between positions is a detail often missed. Without it, the icon jumps across the map.
Driver Behavior Analytics
Hard acceleration (> 0.3g), braking (> 0.4g), sharp turns — events from tracker accelerometer, arrive in position attributes. Driver scoring calculated on backend, sent to app as daily/weekly aggregate: percentage of time with speeding, count of harsh events, rating 0-100.
Geofences — zones on map; entry/exit generates event. Add geofence from mobile app: draw polygon on map, send coordinates to Traccar Geofences API.
Offline and Cache
Dispatcher watches the app constantly; if server is down 5 minutes, can't show empty map. Cache latest positions of all vehicles in Room. On startup show cache, update via WebSocket. Last update timestamp visible in header.
Developing dispatcher app with map, real-time positions via Traccar and trip reports: 5-8 weeks. Full telematics platform with driver analytics, geofences and CAN data integration: 3-4 months. Cost calculated individually after analyzing fleet size and platform requirements.







