From d2f6156841c1b0389dc0fa0ae608d2f928b73c04 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Jan 2020 16:58:47 -0800 Subject: [PATCH] better --- .../java/com/geeksville/mesh/MainActivity.kt | 30 +++++++++++++------ .../geeksville/mesh/RadioInterfaceService.kt | 24 ++++++++++++--- .../java/com/geeksville/mesh/SafeBluetooth.kt | 4 +-- .../geeksville/mesh/SoftwareUpdateService.kt | 6 ++-- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 24c9a8980..a6ccec909 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -120,6 +120,27 @@ class MainActivity : AppCompatActivity(), Logging { ) } }) + + Button(text = "send packets", + onClick = { + // FIXME - don't do these operations until we are informed we have a connection, otherwise + // the radio interface service might not yet be connected to the mesh service + val m = meshService!! + + // Do some test operations + m.setOwner("+16508675309", "Kevin Xter", "kx") + val testPayload = "hello world".toByteArray() + m.sendData( + "+16508675310", + testPayload, + MeshProtos.Data.Type.SIGNAL_OPAQUE_VALUE + ) + m.sendData( + "+16508675310", + testPayload, + MeshProtos.Data.Type.CLEAR_TEXT_VALUE + ) + }) } } } @@ -157,15 +178,6 @@ class MainActivity : AppCompatActivity(), Logging { val m = IMeshService.Stub.asInterface(service) meshService = m - // FIXME - don't do these operations until we are informed we have a connection, otherwise - // the radio interface service might not yet be connected to the mesh service - - // Do some test operations - m.setOwner("+16508675309", "Kevin Xter", "kx") - val testPayload = "hello world".toByteArray() - m.sendData("+16508675310", testPayload, MeshProtos.Data.Type.SIGNAL_OPAQUE_VALUE) - m.sendData("+16508675310", testPayload, MeshProtos.Data.Type.CLEAR_TEXT_VALUE) - // FIXME this doesn't work because the model has already been copied into compose land? // runOnUiThread { // FIXME - this can be removed? meshServiceState.connected = m.isConnected diff --git a/app/src/main/java/com/geeksville/mesh/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/RadioInterfaceService.kt index b8fc3bdb4..4e84fc92d 100644 --- a/app/src/main/java/com/geeksville/mesh/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/RadioInterfaceService.kt @@ -4,6 +4,7 @@ import android.app.Service import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.BluetoothGattCharacteristic.PERMISSION_WRITE import android.bluetooth.BluetoothManager import android.content.Context import android.content.Intent @@ -153,6 +154,8 @@ class RadioInterfaceService : Service(), Logging { override fun onCreate() { super.onCreate() + info("Creating radio interface service") + // FIXME, let user GUI select which device we are talking to // Note: this call does no comms, it just creates the device object (even if the @@ -170,14 +173,21 @@ class RadioInterfaceService : Service(), Logging { // This callback is invoked after we are connected connRes.getOrThrow() // FIXME, instead just try to reconnect? + info("Connected to radio!") + // FIXME - no need to discover services, instead just hardwire the characteristics (like we do for toRadio) safe.asyncDiscoverServices { discRes -> discRes.getOrThrow() // FIXME, instead just try to reconnect? - fromRadio = service.getCharacteristic(BTM_FROMRADIO_CHARACTER) - fromNum = service.getCharacteristic(BTM_FROMNUM_CHARACTER) + // we begin by setting our MTU size as high as it can go + safe.asyncRequestMtu(512) { mtuRes -> + mtuRes.getOrThrow() + + fromRadio = service.getCharacteristic(BTM_FROMRADIO_CHARACTER) + fromNum = service.getCharacteristic(BTM_FROMNUM_CHARACTER) - doReadFromRadio() + doReadFromRadio() + } } } @@ -185,6 +195,7 @@ class RadioInterfaceService : Service(), Logging { } override fun onDestroy() { + info("Destroying radio interface service") sentPacketsLog.close() super.onDestroy() } @@ -199,7 +210,12 @@ class RadioInterfaceService : Service(), Logging { // Note: we generate a new characteristic each time, because we are about to // change the data and we want the data stored in the closure - val toRadio = service.getCharacteristic(BTM_FROMRADIO_CHARACTER) + val toRadio = BluetoothGattCharacteristic( + BTM_FROMRADIO_CHARACTER, + BluetoothGattCharacteristic.PROPERTY_WRITE, + PERMISSION_WRITE + ) + toRadio.value = a safe.asyncWriteCharacteristic(toRadio) { it.getOrThrow() // FIXME, handle the error better diff --git a/app/src/main/java/com/geeksville/mesh/SafeBluetooth.kt b/app/src/main/java/com/geeksville/mesh/SafeBluetooth.kt index 43d2f861b..fff232e55 100644 --- a/app/src/main/java/com/geeksville/mesh/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/SafeBluetooth.kt @@ -60,8 +60,8 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD completeWork(status, Unit) } BluetoothProfile.STATE_DISCONNECTED -> { - // cancel any ops - failAllWork(IOException("Lost connection")) + // cancel any queued ops? for now I think it is best to keep them around + // failAllWork(IOException("Lost connection")) } } } diff --git a/app/src/main/java/com/geeksville/mesh/SoftwareUpdateService.kt b/app/src/main/java/com/geeksville/mesh/SoftwareUpdateService.kt index 01d44cdb8..4841c065f 100644 --- a/app/src/main/java/com/geeksville/mesh/SoftwareUpdateService.kt +++ b/app/src/main/java/com/geeksville/mesh/SoftwareUpdateService.kt @@ -49,11 +49,11 @@ class SoftwareUpdateService : JobIntentService(), Logging { sync.connect() - // we begin by setting our MTU size as high as it can go - sync.requestMtu(512) - sync.discoverServices() // Get our services + // we begin by setting our MTU size as high as it can go + sync.requestMtu(512) + val service = sync.gatt.services.find { it.uuid == SW_UPDATE_UUID }!! val totalSizeDesc = service.getCharacteristic(SW_UPDATE_TOTALSIZE_CHARACTER)