diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index a6ccec909..82233b9b4 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -98,6 +98,24 @@ class MainActivity : AppCompatActivity(), Logging { composeView(meshServiceState) } + private fun sendTestPackets() { + 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 + ) + } + @Composable fun composeView(meshServiceState: MeshServiceState) { MaterialTheme { @@ -122,25 +140,7 @@ 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 - ) - }) + onClick = { sendTestPackets() }) } } } @@ -178,6 +178,11 @@ class MainActivity : AppCompatActivity(), Logging { val m = IMeshService.Stub.asInterface(service) meshService = m + // FIXME: this still can't work this early because the send to +6508675310 + // requires a DB lookup which isn't yet populated (until the sim test packets + // from the radio arrive) + // sendTestPackets() // send some traffic ASAP + // 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/MeshService.kt b/app/src/main/java/com/geeksville/mesh/MeshService.kt index 8c457064c..1aa5a6e18 100644 --- a/app/src/main/java/com/geeksville/mesh/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/MeshService.kt @@ -4,6 +4,7 @@ import android.app.Service import android.content.* import android.os.IBinder import com.geeksville.android.Logging +import com.geeksville.concurrent.DeferredExecution import com.geeksville.mesh.MeshProtos.MeshPacket import com.geeksville.mesh.MeshProtos.ToRadio import com.geeksville.util.toOneLineString @@ -73,13 +74,18 @@ class MeshService : Service(), Logging { explicitBroadcast(intent) } - /// Send a command/packet to our radio + private val toRadioDeferred = DeferredExecution() + + /// Send a command/packet to our radio. But cope with the possiblity that we might start up + /// before we are fully bound to the RadioInterfaceService private fun sendToRadio(p: ToRadio.Builder) { + val b = p.build().toByteArray() + val s = radioService if (s != null) - s.sendToRadio(p.build().toByteArray()) + s.sendToRadio(b) else - error("FIXME! dropped sent packet because radio interface not yet fully connected") + toRadioDeferred.add { radioService!!.sendToRadio(b) } } override fun onBind(intent: Intent?): IBinder? { @@ -103,6 +109,9 @@ class MeshService : Service(), Logging { sendToRadio(ToRadio.newBuilder().apply { wantNodes = ToRadio.WantNodes.newBuilder().build() }) + + // Now send any packets which had previously been queued for clients + toRadioDeferred.run() } override fun onServiceDisconnected(name: ComponentName?) {