运行方式
Android Java SDK:运行方式
当前版本 · 1.4.0
运行方式
Exo 的三种运行方式共享同一设备会话;启动设备后只选一种。普通 Android app 优先使用 exo.runBackground(),业务结束时从回调外工作线程请求停止、join 并关闭。
| 方式 | 调用 | 责任 |
|---|---|---|
| 后台循环 | exo.runBackground() | 结束时 requestStop 与 join |
| 当前线程 | exo.runForever() | 专用工作线程运行,控制线程请求停止 |
| 手动更新 | exo.update() | 按 UpdateResult 等待,不忙等 |
独立设备生命周期
公开示例 DeviceLifecycle 启动两台已配置的 Exo 设备,等待 seconds 秒后停止第二台,再读取第一台的健康状态和保留的最新遥测,最后停止并关闭两台。它展示停止一台设备不会影响另一台,后者的最新样本仍可读取。
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();
}
}
}在工作线程使用两个不同的已授权端口运行。示例显式关闭两台设备,再由 try-with-resources 关闭 SDK。配置了多台设备时始终用 exo.device(name) 选择。
手动更新
SDK main 分支中没有 Exo 手动更新循环的公开示例,下面的辅助类属于文档代码,不属于公开示例集。设备启动后在工作线程调用 exo.update(),每次 update() 后调用一次 sleep();不要同时运行后台循环。
// 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 只表示是否有定时任务截止时间;它为 false 时,事件驱动任务仍可能需要等待。READY 继续循环,STOPPED 结束循环,NO_TASKS 表示当前没有可等待的工作。辅助类只在 NO_TASKS 时短暂休眠以避免忙等;不要用固定延时替代 SDK 等待。
控制线程调用 exo.requestStop() 结束手动循环。循环退出后仍需停止设备并关闭 SDK。后台停止后先 join() 等待清理,再重新启动设备;重连不重放旧触觉。Exo 不支持手套的 pushFrame() 工作流。