feat: Reconnect button (#6)
This commit is contained in:
parent
eddcf47205
commit
dbfb08412d
3 changed files with 58 additions and 20 deletions
|
@ -48,6 +48,14 @@ class Garmin(
|
|||
locations.start()
|
||||
}
|
||||
|
||||
fun reconnect() {
|
||||
Log.d(TAG, "reconnect pressed")
|
||||
if (!connection.isConnecting.get()) {
|
||||
Log.d(TAG, "reconnection enqueued")
|
||||
context.runAsync { connection.tryConnect() }
|
||||
}
|
||||
}
|
||||
|
||||
fun close() {
|
||||
connection.close()
|
||||
|
||||
|
@ -185,6 +193,7 @@ class Garmin(
|
|||
|
||||
private inner class ConnectThread : Thread() {
|
||||
|
||||
val isConnecting = AtomicBoolean(false)
|
||||
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
||||
private val state = AtomicBoolean(false)
|
||||
private var current: IntArray = intArrayOf()
|
||||
|
@ -195,24 +204,30 @@ class Garmin(
|
|||
}
|
||||
|
||||
fun tryConnect() {
|
||||
context.checkBt()
|
||||
// Cancel discovery because it otherwise slows down the connection.
|
||||
adapter.cancelDiscovery()
|
||||
try {
|
||||
socket?.connect()
|
||||
context.toastConnectionStatus(true)
|
||||
sleep(3000)
|
||||
readAll()
|
||||
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
||||
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
||||
state.set(true)
|
||||
} catch (e: IOException) {
|
||||
Log.d(TAG, "Not connected", e)
|
||||
context.toastConnectionStatus(false)
|
||||
state.set(false)
|
||||
Log.d(TAG, "Connection requested")
|
||||
if (!isConnecting.get()) {
|
||||
Log.d(TAG, "Trying to connect...")
|
||||
isConnecting.set(true)
|
||||
context.checkBt()
|
||||
// Cancel discovery because it otherwise slows down the connection.
|
||||
adapter.cancelDiscovery()
|
||||
try {
|
||||
socket?.connect()
|
||||
context.toastConnectionStatus(true)
|
||||
sleep(3000)
|
||||
readAll()
|
||||
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
||||
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
||||
state.set(true)
|
||||
} catch (e: IOException) {
|
||||
Log.d(TAG, "Not connected", e)
|
||||
context.toastConnectionStatus(false)
|
||||
state.set(false)
|
||||
}
|
||||
queue.clear()
|
||||
current = intArrayOf()
|
||||
isConnecting.set(false)
|
||||
}
|
||||
queue.clear()
|
||||
current = intArrayOf()
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
|
@ -262,9 +277,13 @@ class Garmin(
|
|||
}
|
||||
|
||||
private fun sendRaw(buff: IntArray) {
|
||||
buff.forEach { socket!!.outputStream.write(it) }
|
||||
socket!!.outputStream.flush()
|
||||
readAll()
|
||||
try {
|
||||
buff.forEach { socket!!.outputStream.write(it) }
|
||||
socket!!.outputStream.flush()
|
||||
readAll()
|
||||
} catch (e: Exception) {
|
||||
Log.d(TAG, "sendRaw: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import androidx.activity.result.contract.ActivityResultContracts.StartActivityFo
|
|||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.mapbox.navigation.base.options.NavigationOptions
|
||||
import com.mapbox.navigation.core.MapboxNavigation
|
||||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
||||
|
@ -25,6 +26,9 @@ import com.mapbox.navigation.core.lifecycle.requireMapboxNavigation
|
|||
import eu.ztsh.garmin.databinding.ActivityMainBinding
|
||||
import eu.ztsh.garmin.mapbox.MapControl
|
||||
import eu.ztsh.garmin.util.PermissionsHelper
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
|
||||
|
@ -124,6 +128,14 @@ class MainActivity : AppCompatActivity() {
|
|||
return true
|
||||
}
|
||||
|
||||
fun runAsync(action: () -> Unit) {
|
||||
lifecycleScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
action()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun toastConnectionStatus(success: Boolean) {
|
||||
this.runOnUiThread {
|
||||
if (success) {
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.mapbox.navigation.ui.maps.camera.data.MapboxNavigationViewportDataSou
|
|||
import com.mapbox.navigation.ui.maps.camera.lifecycle.NavigationBasicGesturesHandler
|
||||
import com.mapbox.navigation.ui.maps.camera.state.NavigationCameraState
|
||||
import com.mapbox.navigation.ui.maps.location.NavigationLocationProvider
|
||||
import eu.ztsh.garmin.Garmin
|
||||
import eu.ztsh.garmin.MainActivity
|
||||
import eu.ztsh.garmin.UI
|
||||
import eu.ztsh.garmin.mock.ReplayResources
|
||||
|
@ -105,6 +106,12 @@ class MapControl(
|
|||
locationObserver = LocationObserver(this)
|
||||
routeProgressObserver = routeControl.routeProgressObserver
|
||||
voiceInstructionsObserver = voiceControl.voiceInstructionsObserver
|
||||
|
||||
|
||||
ui.reconnect.setOnClickListener {
|
||||
Garmin.instance.reconnect()
|
||||
ui.reconnect.showTextAndExtend(UI.BUTTON_ANIMATION_DURATION)
|
||||
}
|
||||
}
|
||||
|
||||
fun routeToPoint(point: Point) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue