fix: Throw no error on connection error

This commit is contained in:
Piotr Dec 2024-07-14 18:47:47 +02:00
parent bb9a497a73
commit b5e9601065
Signed by: stawros
GPG key ID: F89F27AD8F881A91
2 changed files with 42 additions and 11 deletions

View file

@ -10,7 +10,6 @@ import java.io.IOException
import java.util.*
import java.util.concurrent.SynchronousQueue
@SuppressLint("MissingPermission")
class Garmin(
val context: MainActivity,
@ -100,14 +99,26 @@ class Garmin(
// Cancel discovery because it otherwise slows down the connection.
context.checkBt()
adapter.cancelDiscovery()
socket?.connect()
sleep(3000)
readAll()
while (true) {
val newCurrent = Optional.ofNullable(queue.poll()).orElse(current)
current = newCurrent
send(current)
sleep(900)
try {
socket?.connect()
context.setConnectionStatus(true)
sleep(3000)
readAll()
while (true) {
val newCurrent = Optional.ofNullable(queue.poll()).orElse(current)
current = newCurrent
send(current)
sleep(900)
}
} catch (e: IOException) {
Log.d(TAG, "Not connected", e)
context.setConnectionStatus(false)
while (true) {
// Just dequeue
// TODO: Add option to reconnect
queue.poll()
sleep(900)
}
}
}

View file

@ -5,11 +5,13 @@ import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
@ -66,6 +68,7 @@ class MainActivity : AppCompatActivity() {
super.onStart()
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
}
override fun onStop() {
super.onStop()
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
@ -77,7 +80,6 @@ class MainActivity : AppCompatActivity() {
maneuverApi.cancel()
}
// Define distance formatter options
private val distanceFormatter: DistanceFormatterOptions by lazy {
DistanceFormatterOptions.Builder(this).build()
@ -88,7 +90,13 @@ class MainActivity : AppCompatActivity() {
}
private val routeProgressObserver =
RouteProgressObserver { routeProgress -> maneuverApi.getManeuvers(routeProgress).value?.apply { garmin.process(this[0]) } }
RouteProgressObserver { routeProgress ->
maneuverApi.getManeuvers(routeProgress).value?.apply {
garmin.process(
this[0]
)
}
}
private fun bluetoothInit() {
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
@ -136,7 +144,19 @@ class MainActivity : AppCompatActivity() {
return true
}
fun setConnectionStatus(success: Boolean) {
this.runOnUiThread {
if (success) {
Toast.makeText(this, "Garmin connected", Toast.LENGTH_LONG).show()
} else {
// TODO: Make snackbar with reconnect option
Toast.makeText(this, "Garmin not connected", Toast.LENGTH_LONG).show()
}
}
}
companion object {
private const val TAG = "bt"
}