We're trying to get realtime updates of the devices location, by using Traccar API with Retrofit2, until now, we've only been able to get the location when the application starts (Android application).
To get live updates you should be using WebSocket connection to Traccar server. Retrofit doesn't support anything like that, so you should use OkHttp directly:
Request request = new Request.Builder().url(retrofit.baseUrl().url().toString() + "api/socket").build();
webSocket = WebSocketCall.create(client, request);
webSocket.enqueue(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
@Override
public void onFailure(IOException e, Response response) {
reconnectWebSocket();
}
@Override
public void onMessage(ResponseBody message) throws IOException {
final String data = message.string();
handler.post(new Runnable() {
@Override
public void run() {
try {
handleMessage(data);
} catch (IOException e) {
Log.w(MainFragment.class.getSimpleName(), e);
}
}
});
}
@Override
public void onPong(Buffer payload) {
}
@Override
public void onClose(int code, String reason) {
reconnectWebSocket();
}
});
For more information you can check official Traccar Manager app (native version):
https://github.com/tananaev/traccar-manager-android/tree/native