SDKJava (Android) SDK

Runtime modes

Android Java SDK: Runtime modes

Current version · 1.4.0

Runtime modes

Exo runtime modes share the device session. Choose one after device start. Ordinary Android apps should use exo.runBackground() and request stop/join/close from a worker outside callbacks when finished.

ModeCallResponsibility
Backgroundexo.runBackground()requestStop and join on exit
Calling threadexo.runForever()Dedicated worker; control thread requests stop
Manualexo.update()Honor UpdateResult waits without polling

Independent device lifecycle

The public DeviceLifecycle example starts two configured Exo devices, leaves both running for seconds, stops the second device, then reads the first device's health and retained telemetry before stopping and closing both. It shows that stopping one device leaves the other running and its latest sample readable.

import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.config.SerialConnection;
import com.qnbot.sdk.exo.Exo;
import com.qnbot.sdk.exo.ExoConfig;
import com.qnbot.sdk.exo.ExoDevice;

/**
 * Runs two Exo devices independently: stop the second and keep reading the first.
 *
 * <p>Supply an Android {@link Context}, an authorized {@link SerialTransport} and two distinct
 * ports. The example waits {@code seconds}, stops the second device, reads the first device's
 * health and retained telemetry, then closes both.
 */
public final class DeviceLifecycle {
  private DeviceLifecycle() {}

  public static void run(Context context, SerialTransport transport, String firstPort,
      String secondPort, double seconds) throws Exception {
    if (!Double.isFinite(seconds) || seconds <= 0)
      throw new IllegalArgumentException("seconds must be positive and finite");
    if (firstPort.equals(secondPort))
      throw new IllegalArgumentException("Use two distinct Exo ports");
    SdkConfig config = SdkConfig.builder()
                           .device(ExoConfig.builder()
                                       .name("first")
                                       .connection(SerialConnection.port(firstPort))
                                       .build())
                           .device(ExoConfig.builder()
                                       .name("second")
                                       .connection(SerialConnection.port(secondPort))
                                       .build())
                           .build();
    try (Sdk sdk = new Sdk(config, context, transport); Exo exo = sdk.exo()) {
      ExoDevice first = exo.device("first");
      ExoDevice second = exo.device("second");
      first.telemetry().subscribe(
          sample -> System.out.println("first sequence=" + sample.sequence));
      second.telemetry().subscribe(
          sample -> System.out.println("second sequence=" + sample.sequence));
      first.start();
      second.start();
      System.out.println("first health=" + first.health() + " second health=" + second.health());
      Thread.sleep((long) (seconds * 1000));
      // Stopping one device leaves the other running and its retained sample readable.
      second.stop();
      System.out.println("second stopped; first continues");
      System.out.println("first health=" + first.health());
      first.telemetry().latest().ifPresent(
          sample -> System.out.println("first retained sequence=" + sample.sequence));
      first.stop();
      first.close();
      second.close();
    }
  }
}

Run it on a worker thread with two distinct authorized ports. The example closes both devices explicitly and then closes the SDK through try-with-resources. Use exo.device(name) whenever more than one device is configured.

Manual updates

SDK main has no public Exo manual-update example, so the helper below is documentation code rather than part of the example set. Call exo.update() on a worker after device start and call sleep() once after every update; do not also run a background loop.

// Documentation helper: no public Exo manual-update example ships in SDK main.
import com.qnbot.sdk.SleepResult;
import com.qnbot.sdk.UpdateResult;
import com.qnbot.sdk.exo.Exo;

public final class ExoManualLoop {
    // Invoke after device.start() on a worker; do not also run a background loop.
    public static void run(Exo exo) throws InterruptedException {
        while (true) {
            UpdateResult update = exo.update();
            SleepResult result = update.sleep();
            if (result == SleepResult.STOPPED) break;
            if (result == SleepResult.NO_TASKS) Thread.sleep(10);
        }
    }
}

hasNextTask reports periodic deadlines only; event-driven work can still need a wait when it is false. READY continues the loop, STOPPED ends it, and NO_TASKS means there is currently nothing to wait on. The helper pauses briefly only for NO_TASKS to avoid busy polling; do not replace the SDK wait with a fixed delay.

Call exo.requestStop() from a control thread to end the manual loop. Still stop devices and close the SDK after it exits. Join background cleanup before restarting devices; reconnection does not replay haptics. Exo does not provide the glove pushFrame() workflow.