fix: Throw no error on connection error
This commit is contained in:
parent
bb9a497a73
commit
b5e9601065
2 changed files with 42 additions and 11 deletions
|
@ -10,7 +10,6 @@ import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.SynchronousQueue
|
import java.util.concurrent.SynchronousQueue
|
||||||
|
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
class Garmin(
|
class Garmin(
|
||||||
val context: MainActivity,
|
val context: MainActivity,
|
||||||
|
@ -100,7 +99,9 @@ class Garmin(
|
||||||
// Cancel discovery because it otherwise slows down the connection.
|
// Cancel discovery because it otherwise slows down the connection.
|
||||||
context.checkBt()
|
context.checkBt()
|
||||||
adapter.cancelDiscovery()
|
adapter.cancelDiscovery()
|
||||||
|
try {
|
||||||
socket?.connect()
|
socket?.connect()
|
||||||
|
context.setConnectionStatus(true)
|
||||||
sleep(3000)
|
sleep(3000)
|
||||||
readAll()
|
readAll()
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -109,6 +110,16 @@ class Garmin(
|
||||||
send(current)
|
send(current)
|
||||||
sleep(900)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Closes the client socket and causes the thread to finish.
|
// Closes the client socket and causes the thread to finish.
|
||||||
|
|
|
@ -5,11 +5,13 @@ import android.annotation.SuppressLint
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.activity.result.ActivityResultCallback
|
import androidx.activity.result.ActivityResultCallback
|
||||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
@ -66,6 +68,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
super.onStop()
|
super.onStop()
|
||||||
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
||||||
|
@ -77,7 +80,6 @@ class MainActivity : AppCompatActivity() {
|
||||||
maneuverApi.cancel()
|
maneuverApi.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Define distance formatter options
|
// Define distance formatter options
|
||||||
private val distanceFormatter: DistanceFormatterOptions by lazy {
|
private val distanceFormatter: DistanceFormatterOptions by lazy {
|
||||||
DistanceFormatterOptions.Builder(this).build()
|
DistanceFormatterOptions.Builder(this).build()
|
||||||
|
@ -88,7 +90,13 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private val routeProgressObserver =
|
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() {
|
private fun bluetoothInit() {
|
||||||
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
|
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
|
||||||
|
@ -136,7 +144,19 @@ class MainActivity : AppCompatActivity() {
|
||||||
return true
|
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 {
|
companion object {
|
||||||
|
|
||||||
private const val TAG = "bt"
|
private const val TAG = "bt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue