设备发现与连接
Android Java SDK:设备发现与连接
设备发现与连接
当前 USB transport 支持单串口 CDC ACM。其他串口芯片或多串口设备由 app 实现 com.qnbot.sdk.android.SerialTransport 接入。
选择与授权
app 通过自己的 USB 流程取得系统授权,再把已授权的 SerialTransport 交给 SDK;公开示例从这一步开始,不包含权限对话框。DiscoverGloves.run(context, transport) 在已授权传输上发现手套候选,打印来源、端口、手侧、序列号以及逐设备错误。
import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.glove.DiscoveredGlove;
import java.io.IOException;
import java.util.List;
/**
* Lists glove candidates reachable through the caller's authorized Android serial transport.
*
* <p>Supply an Android {@link Context} and an authorized {@link SerialTransport}; each candidate
* prints its transport, port, hand, serial number and any per-port discovery error. The temporary
* probe connections close before this method returns.
*/
public final class DiscoverGloves {
private DiscoverGloves() {}
public static void run(Context context, SerialTransport transport) throws IOException {
List<DiscoveredGlove> found = Sdk.discoverGloves(context, transport);
for (DiscoveredGlove device : found) {
String information = "transport=" + device.transportType + " port=" + device.port
+ " hand=" + device.hand + " serial=" + device.sn;
if (device.error == null) {
System.out.println(information);
} else {
System.out.println(information + " error=" + device.error.code
+ " message=" + device.error.message);
}
}
}
}发现结束后 SDK 关闭临时端点,transport 仍可传给 Sdk 构造函数。app 负责显示候选并保留选定端口;不要直接打开清单里的所有设备。
声明与选择
在 GloveConfig.builder() 中设置实际 Side、可选设备名和 SerialConnection.port(selectedPort),加入 SdkConfig。使用 new Sdk(config, context, transport),再按设备名和左右手调用 sdk.glove().device(name, side)。名称或左右手匹配多个设备时抛出 AmbiguousSelectionException;DeviceSelection.run(context) 使用外部连接演示按名称和左右手选择,并检查预期歧义。为多个配置项使用不同名称;只使用一只手套时保留对应声明。
import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.config.Side;
import com.qnbot.sdk.error.AmbiguousSelectionException;
import com.qnbot.sdk.glove.ExternalConnection;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
/**
* Resolves gloves by name and side, then shows the ambiguity errors for unqualified selectors.
*
* <p>No Android transport is needed: the devices use external connections. Supply the Android
* Context; the SDK closes after the three selections and two expected ambiguity checks.
*/
public final class DeviceSelection {
private DeviceSelection() {}
public static void run(Context context) throws Exception {
SdkConfig config = SdkConfig.builder()
.device(GloveConfig.builder()
.name("primary")
.side(Side.LEFT)
.connection(ExternalConnection.INSTANCE)
.build())
.device(GloveConfig.builder()
.name("primary")
.side(Side.RIGHT)
.connection(ExternalConnection.INSTANCE)
.build())
.device(GloveConfig.builder()
.name("backup")
.side(Side.LEFT)
.connection(ExternalConnection.INSTANCE)
.build())
.build();
try (Sdk sdk = new Sdk(config, context); Glove glove = sdk.glove()) {
System.out.println("primary left source_id=" + glove.device("primary", Side.LEFT).sourceId());
System.out.println(
"primary right source_id=" + glove.device("primary", Side.RIGHT).sourceId());
System.out.println("backup left source_id=" + glove.device("backup").sourceId());
try {
glove.device("primary");
throw new IllegalStateException("name=primary should match two devices");
} catch (AmbiguousSelectionException expected) {
System.out.println("expected ambiguity: name=primary");
}
try {
glove.device(Side.LEFT);
throw new IllegalStateException("side=left should match two devices");
} catch (AmbiguousSelectionException expected) {
System.out.println("expected ambiguity: side=left");
}
}
System.out.println("device selection complete");
}
}如果尚未把 transport 交给 SDK 就退出设备选择,app 负责调用它的 close()。如果已由 SDK 接管,则按 SDK 生命周期结束会话。
读取已确定的设备信息
选择已验证身份的直连串口手套或 Receiver 成员后,在 start() 前后都可调用 getDeviceInfo()。返回的快照标识该 GloveDevice 实际解析到的设备和连接。ExternalConnection 及其他没有已验证身份信息的来源会抛出 UnavailableFeatureException。
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.config.Side;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
import com.qnbot.sdk.glove.GloveDevice;
import com.qnbot.sdk.glove.GloveDeviceInfo;
import java.util.Locale;
/**
* Reads one glove's identity, firmware and transport information before starting the runtime.
*
* <p>Supply the Android Context, an authorized transport and optional port/side; null selects
* automatic discovery or leaves the side unconstrained. The SDK closes when the read completes.
*/
public final class DeviceInfo {
private DeviceInfo() {}
public static void run(Context context, SerialTransport transport, String port, Side side)
throws Exception {
SdkConfig config = SdkConfig.builder()
.device(GloveConfig.builder()
.name("primary")
.side(side)
.connection(port == null ? SerialConnection.autoDiscover()
: SerialConnection.port(port))
.build())
.build();
try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
GloveDevice device = glove.device();
GloveDeviceInfo info = device.getDeviceInfo();
System.out.println("source=" + device.sourceId()
+ " canonical_sn=" + info.canonicalSn
+ " device_type=" + (info.deviceType == null ? "unknown" : info.deviceType)
+ " model=" + (info.model == null ? "unknown" : info.model)
+ " firmware_version=" + (info.firmwareVersion.isEmpty() ? "unknown" : info.firmwareVersion)
+ " hand=" + info.hand.wireValue()
+ " hand_source=" + (info.handSource == null ? "unknown" : info.handSource)
+ " transport_type=" + info.transportType.name().toLowerCase(Locale.ROOT));
}
}
}通过快照读取 canonicalSn、deviceType、model、firmwareVersion、hand、handSource 和 transportType。直连手套返回 SERIAL;已解析的 Receiver 成员返回 RECEIVER_PEER,且 handSource 为空,因为 Receiver 不报告左右手判定来源。RECEIVER_USB 只用于 DiscoveredGlove.transportType 标识 Receiver USB 候选,不是已解析成员的 GloveDeviceInfo 值。
Receiver 与双手配置
app 先选择并授权 Receiver 的 USB 端口。每只无线手套使用普通 GloveConfig,指定名称和明确左右手,并使用相同的 SerialConnection.port(selectedPort)。直连手套分别选择各自的端口。DeviceLifecycle.run(...) 用两个命名设备展示独立启停:左手的姿态继续输出,右手的姿态在停止后不再推进。
import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.SleepResult;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.channel.ReadChannel;
import com.qnbot.sdk.channel.Sample;
import com.qnbot.sdk.config.SerialConnection;
import com.qnbot.sdk.config.Side;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
import com.qnbot.sdk.glove.GloveDevice;
import com.qnbot.sdk.glove.GlovePose;
import java.util.Optional;
/**
* Stops and restarts the right glove while the left keeps streaming pose samples.
*
* <p>Supply the Android Context, authorized transport, optional serial ports and the number of
* fresh samples per phase. The example checks that a stopped device stops advancing.
*/
public final class DeviceLifecycle {
private DeviceLifecycle() {}
public static void run(Context context, SerialTransport transport, String leftPort,
String rightPort, int updates) throws InterruptedException {
if (updates <= 0) {
throw new IllegalArgumentException("updates must be positive");
}
SdkConfig config = SdkConfig.builder()
.device(GloveConfig.builder()
.name("left")
.side(Side.LEFT)
.connection(leftPort == null
? SerialConnection.autoDiscover()
: SerialConnection.port(leftPort))
.build())
.device(GloveConfig.builder()
.name("right")
.side(Side.RIGHT)
.connection(rightPort == null
? SerialConnection.autoDiscover()
: SerialConnection.port(rightPort))
.build())
.build();
try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
GloveDevice left = glove.device("left");
GloveDevice right = glove.device("right");
ReadChannel<GlovePose> leftPose = left.pose();
ReadChannel<GlovePose> rightPose = right.pose();
left.start();
right.start();
System.out.println("both devices active");
for (int phase = 0; phase < 3; phase++) {
boolean rightActive = phase != 1;
if (phase == 1) {
right.stop();
System.out.println("right device stopped; left remains active");
} else if (phase == 2) {
right.start();
System.out.println("right device restarted");
}
long previousLeft = leftPose.latest().map(sample -> sample.sequence).orElse(-1L);
long previousRight = rightPose.latest().map(sample -> sample.sequence).orElse(-1L);
int leftCount = 0;
int rightCount = 0;
long deadline = System.nanoTime() + 10_000_000_000L;
while (leftCount < updates || (rightActive && rightCount < updates)) {
if (System.nanoTime() >= deadline) {
throw new IllegalStateException("timed out waiting for pose samples");
}
var update = glove.update();
if (update.sleep() == SleepResult.NO_TASKS) {
Thread.sleep(10);
}
Optional<Sample<GlovePose>> leftSample = leftPose.latest();
Optional<Sample<GlovePose>> rightSample = rightPose.latest();
long rightSequence = rightSample.map(sample -> sample.sequence).orElse(-1L);
if (!rightActive && rightSequence != previousRight) {
throw new IllegalStateException("right pose advanced while stopped");
}
if (leftSample.isPresent() && leftSample.get().sequence != previousLeft) {
previousLeft = leftSample.get().sequence;
leftCount++;
System.out.println("left pose sequence=" + leftSample.get().sequence);
}
if (rightActive && rightSample.isPresent() && rightSequence != previousRight) {
previousRight = rightSequence;
rightCount++;
System.out.println("right pose sequence=" + rightSample.get().sequence);
}
}
}
System.out.println("lifecycle complete");
}
}
}使用上述配置、app Context 和已授权传输构造 SDK。通过 sdk.glove().device("left") 和 device("right") 选择手套,再按既有流程启动、订阅和关闭。只使用一只手套时保留对应声明;共用 Receiver 的两只启用手套必须指定不同的左右手。
CompositeExoGloveConfig.builder().name(...) 设置 Exo+Glove 组合的外层名称,独立于成员名称;它不替代 device(name, side) 的选择条件。
Exo 与手套共用串口
从配套离线交付安装两个组件。只在外层配置中传入一次已授权的共享串口。用此配置创建一个 Sdk,调用 sdk.start() 启动三个成员,再通过 sdk.glove() 和 sdk.exo() 选择数据通道。组合示例 QuickStart、DiscoverExoGlove、GloveRetargeting 和 GloveHaptics 见手套 + 外骨骼融合方案,本页不重复配置代码。
类型化发现来源
Sdk.discoverGloves(context, transport) 的 DiscoveredGlove.transportType 为 GloveTransportType.SERIAL、RECEIVER_USB 或 RECEIVER_PEER;通过它区分有线手套、Receiver USB 和无线成员来源。部分成功或带错误的候选同样保留来源。不要从端口字符串或产品名猜测连接来源。
在 Android 上取得 USB 权限后,在工作线程调用 Sdk.discoverGloves(context, transport)。发现结束后 SDK 关闭临时端点,宿主传输仍可传入 SDK 构造函数。