async bt now works great

This commit is contained in:
geeksville 2020-01-27 19:08:12 -08:00
parent 534bf7fca8
commit d03bfb556c

View file

@ -4,13 +4,13 @@ import android.app.Service
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGattCharacteristic import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattCharacteristic.PERMISSION_WRITE
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.IBinder import android.os.IBinder
import com.geeksville.android.DebugLogFile import com.geeksville.android.DebugLogFile
import com.geeksville.android.Logging import com.geeksville.android.Logging
import com.geeksville.concurrent.DeferredExecution
import com.google.protobuf.util.JsonFormat import com.google.protobuf.util.JsonFormat
import java.util.* import java.util.*
@ -113,6 +113,14 @@ class RadioInterfaceService : Service(), Logging {
// for debug logging only // for debug logging only
private val jsonPrinter = JsonFormat.printer() private val jsonPrinter = JsonFormat.printer()
// We have talked to our device and consumed all of the FromRadio packets it had initially
// waiting for us
private var initCompleted = false
/// Work that users of our service want done, which might get deferred until after
/// we have completed our initial connection
private val clientOperations = DeferredExecution()
fun broadcastConnectionChanged(isConnected: Boolean) { fun broadcastConnectionChanged(isConnected: Boolean) {
val intent = Intent("$prefix.CONNECTION_CHANGED") val intent = Intent("$prefix.CONNECTION_CHANGED")
intent.putExtra(EXTRA_CONNECTED, isConnected) intent.putExtra(EXTRA_CONNECTED, isConnected)
@ -146,11 +154,20 @@ class RadioInterfaceService : Service(), Logging {
// Queue up another read, until we run out of packets // Queue up another read, until we run out of packets
doReadFromRadio() doReadFromRadio()
} else } else {
debug("Done reading from radio, fromradio is empty") debug("Done reading from radio, fromradio is empty")
initCompleted = true
doClientOperations()
}
} }
} }
/// If we are inited send any client requests
private fun doClientOperations() {
if (initCompleted)
clientOperations.run()
}
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -211,21 +228,19 @@ class RadioInterfaceService : Service(), Logging {
override fun sendToRadio(a: ByteArray) { override fun sendToRadio(a: ByteArray) {
debug("queuing ${a.size} bytes to radio") debug("queuing ${a.size} bytes to radio")
// Note: we generate a new characteristic each time, because we are about to clientOperations.add {
// change the data and we want the data stored in the closure // Note: we generate a new characteristic each time, because we are about to
val toRadio = BluetoothGattCharacteristic( // change the data and we want the data stored in the closure
BTM_FROMRADIO_CHARACTER, val toRadio = service.getCharacteristic(BTM_TORADIO_CHARACTER)
BluetoothGattCharacteristic.PROPERTY_WRITE, toRadio.value = a
PERMISSION_WRITE
)
toRadio.value = a
if (true)
safe.asyncWriteCharacteristic(toRadio) { safe.asyncWriteCharacteristic(toRadio) {
it.getOrThrow() // FIXME, handle the error better it.getOrThrow() // FIXME, handle the error better
debug("ToRadio write of ${a.size} bytes completed")
} }
else }
error("FIXME ignoring writes for now - because they slide in before discovery - bad bad")
doClientOperations()
} }
} }
} }