Skip to content

Commit a238f78

Browse files
committed
New Feature #60
Allow websocket client to query last know location by sending "getLastKnownLocation" command to server
1 parent 9bfbe13 commit a238f78

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

app/src/main/java/github/umer0586/sensorserver/websocketserver/SensorWebSocketServer.kt

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.net.Uri
1515
import android.os.*
1616
import android.util.Log
1717
import android.view.MotionEvent
18+
import androidx.annotation.RequiresApi
1819
import github.umer0586.sensorserver.util.JsonUtil
1920
import org.java_websocket.WebSocket
2021
import org.java_websocket.handshake.ClientHandshake
@@ -290,41 +291,15 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
290291

291292
override fun onLocationChanged(location: Location)
292293
{
293-
if (getGPSConnectionCount() > 0)
294+
if (getGPSConnectionCount() == 0)
294295
{
295296
Log.w( TAG, "onLocationChanged() : " + "Location update received when no client with connected with GPS" )
296297
}
297298
for (websocket in connections)
298299
{
299300
if (websocket.getAttachment<Any>() is GPS)
300301
{
301-
message.clear()
302-
message["longitude"] = location.longitude
303-
message["latitude"] = location.latitude
304-
message["altitude"] = location.altitude
305-
message["bearing"] = location.bearing
306-
message["accuracy"] = location.accuracy
307-
message["speed"] = location.speed
308-
message["time"] = location.time
309-
310-
311-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
312-
{
313-
message["speedAccuracyMetersPerSecond"] = location.speedAccuracyMetersPerSecond
314-
message["bearingAccuracyDegrees"] = location.bearingAccuracyDegrees
315-
message["elapsedRealtimeNanos"] = location.elapsedRealtimeNanos
316-
message["verticalAccuracyMeters"] = location.verticalAccuracyMeters
317-
}
318-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
319-
{
320-
message["elapsedRealtimeAgeMillis"] = location.elapsedRealtimeAgeMillis
321-
}
322-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
323-
{
324-
message["elapsedRealtimeUncertaintyNanos"] = location.elapsedRealtimeUncertaintyNanos
325-
}
326-
327-
websocket.send( JsonUtil.toJSON(message) )
302+
websocket.send(location.toJson())
328303
}
329304
}
330305
}
@@ -410,8 +385,39 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
410385
notifyConnectionsChanged()
411386
}
412387

413-
override fun onMessage(conn: WebSocket, message: String)
388+
389+
override fun onMessage(websocket: WebSocket, message: String)
414390
{
391+
//Log.d(TAG, "onMessage: $message")
392+
//Log.d(TAG, "onMessage: ${Thread.currentThread().name}")
393+
394+
if(message.equals("getLastKnownLocation",ignoreCase = true) && websocket.getAttachment<Any>() is GPS)
395+
{
396+
// For Android 6.0 or above check if user has allowed location permission
397+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
398+
{
399+
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
400+
{
401+
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.apply {
402+
websocket.send(this.toJson(lastKnownLocation = true))
403+
}
404+
}
405+
else
406+
{
407+
websocket.close(
408+
CLOSE_CODE_PERMISSION_DENIED,
409+
"App has No permission to access location. Go to your device's installed apps settings and allow location permission to Sensor Server app"
410+
)
411+
}
412+
}
413+
// For Android 5.0 permissions are granted at install time
414+
else {
415+
416+
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.apply {
417+
websocket.send(this.toJson(lastKnownLocation = true))
418+
}
419+
}
420+
}
415421
}
416422

417423
override fun onMessage(conn: WebSocket, message: ByteBuffer)
@@ -671,4 +677,36 @@ fun SensorManager.getSensorFromStringType(sensorStringType: String) : Sensor?
671677
.filter { it.stringType.equals(sensorStringType, ignoreCase = true) }
672678
.firstOrNull()
673679

680+
}
681+
682+
fun Location.toJson(lastKnownLocation : Boolean = false) : String
683+
{
684+
val message = mutableMapOf<String,Any>()
685+
message["longitude"] = longitude
686+
message["latitude"] = latitude
687+
message["altitude"] = altitude
688+
message["bearing"] = bearing
689+
message["accuracy"] = accuracy
690+
message["speed"] = speed
691+
message["time"] = time
692+
message["lastKnowLocation"] = lastKnownLocation
693+
694+
695+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
696+
{
697+
message["speedAccuracyMetersPerSecond"] = speedAccuracyMetersPerSecond
698+
message["bearingAccuracyDegrees"] = bearingAccuracyDegrees
699+
message["elapsedRealtimeNanos"] = elapsedRealtimeNanos
700+
message["verticalAccuracyMeters"] = verticalAccuracyMeters
701+
}
702+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
703+
{
704+
message["elapsedRealtimeAgeMillis"] = elapsedRealtimeAgeMillis
705+
}
706+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
707+
{
708+
message["elapsedRealtimeUncertaintyNanos"] = elapsedRealtimeUncertaintyNanos
709+
}
710+
711+
return JsonUtil.toJSON(message)
674712
}

0 commit comments

Comments
 (0)