Skip to content

Commit 1ce1e46

Browse files
committed
Bug fixed
close reason message cannot be more than 123 bytes. Server was sending a very long message causing error at client side
1 parent a238f78 commit 1ce1e46

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
9999
clientWebsocket.setAttachment(TouchSensors())
100100
notifyConnectionsChanged()
101101
}
102-
103-
// TODO : handleGPSRequest(websocket) never gets called when app has no location permission
104102
CONNECTION_PATH_GPS -> handleGPSRequest(clientWebsocket)
105103
else -> clientWebsocket.close(CLOSE_CODE_UNSUPPORTED_REQUEST, "unsupported request")
106104

@@ -254,31 +252,31 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
254252

255253

256254

257-
@SuppressLint("MissingPermission")
258255
private fun handleGPSRequest(clientWebsocket: WebSocket)
259256
{
260-
if (!hasLocationPermission())
257+
258+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
259+
context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
261260
{
261+
// Reason message must be 123 bytes or less
262262
clientWebsocket.close(
263-
CLOSE_CODE_PERMISSION_DENIED,
264-
"App has No permission to access location. Go to your device's installed apps settings and allow location permission to Sensor Server app"
263+
CLOSE_CODE_PERMISSION_DENIED,
264+
"Location permission required. Please enable it in your device's App Settings."
265265
)
266266
return
267267
}
268268

269-
269+
// In Android 5.0 permissions are granted at installation time
270270
locationManager.requestLocationUpdates(
271-
LocationManager.GPS_PROVIDER,
272-
0,
273-
0f,
274-
this,
275-
handlerThread.looper
271+
LocationManager.GPS_PROVIDER,
272+
0,
273+
0f,
274+
this,
275+
handlerThread.looper
276276
)
277277

278278
clientWebsocket.setAttachment( GPS() )
279279

280-
281-
282280
notifyConnectionsChanged()
283281
}
284282

@@ -321,18 +319,6 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
321319
{
322320
// super.onStatusChanged(provider, status, extras)
323321
}
324-
325-
private fun hasLocationPermission(): Boolean
326-
{
327-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
328-
{
329-
context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
330-
}
331-
else true
332-
333-
//prior to android marshmallow dangerous permission are prompt at install time
334-
}
335-
336322
override fun onClose(clientWebsocket: WebSocket, code: Int, reason: String, remote: Boolean)
337323
{
338324
Log.i(TAG,"Connection closed ${clientWebsocket.remoteSocketAddress} with exit code $code additional info: $reason")

0 commit comments

Comments
 (0)