Hand model parameter configuration
Android Java SDK: Hand model parameter configuration
Hand model parameter configuration
The Android SDK controls hand model parameters through TargetAlgorithm's bone configuration mode and algorithm configuration file. Add the TargetAlgorithm to the matching TargetConfig before creating the SDK. Configuration objects do not change after construction.
Bone configuration mode
TargetAlgorithm supports BoneConfigMode.AUTO, ENABLED and DISABLED for loading personal bone profiles. The default constructor keeps AUTO and a null algorithmConfigPath. Algorithm parameter overrides use algorithmConfigPath independently.
| Need | Setting |
|---|---|
| Load a saved profile, using the algorithm defaults when the file is missing | BoneConfigMode.AUTO |
| Require a readable, valid and compatible saved profile | BoneConfigMode.ENABLED |
| Use the algorithm's default bone parameters | BoneConfigMode.DISABLED |
| Use parameter overrides supported by the algorithm | An app-readable algorithmConfigPath, with the desired bone mode |
An existing profile with read errors or invalid YAML causes an error in AUTO mode. Close and recreate the SDK after changing a profile or algorithm configuration file.
Measure and save personal bone lengths
Before constructing Sdk, explicitly start phone-based measurement with BoneConfig.create(...) on a worker thread. Prepare the A4 calibration board required by the webpage, scan the QR code or open the phone link, and complete measurement for the requested hand. Measurement requires an Android Context and works before a glove is connected.
The default AUTO_TERMINAL interaction presents the terminal flow, waits, saves the result and closes the session on success or failure. Omitting the operator uses "default". The result provides interaction, installed() and session() with mode-specific typed access. Calling the accessor for the other mode throws IllegalStateException.
import com.qnbot.sdk.config.Side;
import com.qnbot.sdk.glove.BoneConfig;
import com.qnbot.sdk.glove.BoneConfigInstallResult;
BoneConfigInstallResult installed = BoneConfig.create(context, Side.RIGHT).installed();A successful repeat measurement replaces the same operator and hand profile by default. To protect an existing profile during terminal creation, pass CaptureInteractionMode.AUTO_TERMINAL and BoneConfigOverwritePolicy.DENY explicitly. For an Android App interface, select CaptureInteractionMode.EXTERNAL, present the session's phone link or QR code in the app, poll until COMPLETED, then call session.install() and close. External installation also defaults to REPLACE_EXISTING. Call session.install(BoneConfigOverwritePolicy.DENY) to reject an existing profile. Cancel and close an abandoned measurement. The external app flow appears in prepareBoneConfig below.
For the run(...) example below, pass "default" and the same hand to prepareBoneConfig. Applications using a custom operator must set the matching CaptureConfig.operatorId in their SdkConfig and retain the same hand and data root when constructing the SDK. A failed measurement, validation or save preserves the previous profile.
Export a saved profile
BoneConfig.exportFile(context, destination, side, operatorId) exports the saved single-hand YAML and returns a Path. Omitting the operator selects "default". The destination policy defaults to DENY. Use the overload with REPLACE_EXISTING to replace it explicitly. A missing or invalid saved profile produces an error. Export preserves the saved profile, and a failed write preserves an existing destination. The app handles copying to a document provider or sharing. Historical files can be restored through the app-owned file flow before constructing the SDK.
Algorithm configuration file
The optional algorithmConfigPath points to a configuration file supported by the selected algorithm. The path must be a non-empty, valid UTF-8 string without NUL, and the file must be readable in the Android app runtime. Use only configuration files confirmed by the actual algorithm delivery.
Configuration example
AlgorithmConfiguration.run(...) configures a configured_hand target with new TargetAlgorithm(packageId, boneConfigMode, algorithmConfigPath), runs it for a fixed duration and prints target output. The same public class also provides preparation before SDK creation: importAlgorithmArchive installs an Android algorithm ZIP, exportBoneConfig exports a saved bone-length profile, and prepareBoneConfig presents the phone link and polls the submitted result.
import android.content.Context;
import android.net.Uri;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.algorithm.AlgorithmPackageManager;
import com.qnbot.sdk.algorithm.InstallBatchResult;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.config.BoneConfigMode;
import com.qnbot.sdk.config.DeviceSelector;
import com.qnbot.sdk.config.SerialConnection;
import com.qnbot.sdk.config.Side;
import com.qnbot.sdk.config.TargetAlgorithm;
import com.qnbot.sdk.config.TargetConfig;
import com.qnbot.sdk.glove.BoneConfig;
import com.qnbot.sdk.glove.BoneConfigInstallResult;
import com.qnbot.sdk.glove.BoneConfigOverwritePolicy;
import com.qnbot.sdk.glove.BoneConfigSession;
import com.qnbot.sdk.glove.BoneConfigSessionInfo;
import com.qnbot.sdk.glove.BoneConfigSessionStatus;
import com.qnbot.sdk.glove.capture.CaptureInteractionMode;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
import com.qnbot.sdk.glove.GloveDevice;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.CancellationException;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
/**
* Prepares an algorithm package or bone configuration, then runs one configured target for a
* fixed duration and prints retargeting output.
*
* <p>Supply the Android Context, authorized transport, explicit serial port, side and installed
* package ID. Pass null algorithmConfigPath when the package uses its defaults; try-with-resources
* closes the SDK after the timed run.
*/
public final class AlgorithmConfiguration {
private AlgorithmConfiguration() {}
/** Optional preparation before run(): import the Android algorithm ZIP selected by the host. */
public static InstallBatchResult importAlgorithmArchive(Context context, Uri selectedZip)
throws IOException {
return new AlgorithmPackageManager(context, "glove").installFromArchive(selectedZip);
}
/** Exports the selected operator's saved hand without replacing an existing backup. */
public static Path exportBoneConfig(
Context context, Path destination, Side side, String operatorId) {
return BoneConfig.exportFile(context, destination, side, operatorId);
}
/** Exports the selected operator's saved hand with an explicit destination policy. */
public static Path exportBoneConfig(Context context, Path destination, Side side,
String operatorId, BoneConfigOverwritePolicy overwrite) {
return BoneConfig.exportFile(context, destination, side, operatorId, overwrite);
}
/** Prepares the saved hand, replacing this operator's previous measurement. */
public static BoneConfigInstallResult prepareBoneConfig(Context context, Side side,
String operatorId, Consumer<String> presentLink, BooleanSupplier cancelled)
throws InterruptedException {
return prepareBoneConfig(context, side, operatorId, BoneConfigOverwritePolicy.REPLACE_EXISTING,
presentLink, cancelled);
}
/**
* Presents the phone link, polls the session, installs the submitted configuration and closes
* the session. presentLink must return promptly; cancelled stops the wait. Do not log the phone
* link.
*/
public static BoneConfigInstallResult prepareBoneConfig(Context context, Side side,
String operatorId, BoneConfigOverwritePolicy overwrite, Consumer<String> presentLink,
BooleanSupplier cancelled) throws InterruptedException {
try (BoneConfigSession session = BoneConfig.create(
context, side, operatorId, CaptureInteractionMode.EXTERNAL).session()) {
try {
BoneConfigSessionInfo info = session.info();
presentLink.accept(info.phoneUrl);
long deadline = System.nanoTime() + info.remainingSeconds * 1_000_000_000L;
while (!cancelled.getAsBoolean()) {
if (System.nanoTime() >= deadline) {
throw new IllegalStateException("Bone configuration session expired");
}
BoneConfigSessionStatus status = session.poll();
if (cancelled.getAsBoolean()) {
break;
}
if (status == BoneConfigSessionStatus.COMPLETED) {
return session.install(overwrite);
}
long nextPoll = System.nanoTime() + info.pollIntervalSeconds * 1_000_000_000L;
while (!cancelled.getAsBoolean() && System.nanoTime() < Math.min(nextPoll, deadline)) {
Thread.sleep(20);
}
}
throw new CancellationException("Bone preparation cancelled by caller");
} catch (InterruptedException | RuntimeException error) {
try {
session.cancel();
} catch (RuntimeException ignored) {
// Keep the original preparation failure.
}
throw error;
}
}
}
/**
* Runs the configured target for {@code seconds} and prints retargeting output. The SDK default
* Capture interaction applies; use the preparation methods above when the package or bone
* configuration is not ready.
*/
public static void run(Context context, SerialTransport transport, String port, Side side,
String packageId, BoneConfigMode boneConfigMode, String algorithmConfigPath, double seconds)
throws InterruptedException {
if (!Double.isFinite(seconds) || seconds <= 0) {
throw new IllegalArgumentException("seconds must be positive and finite");
}
SdkConfig config = SdkConfig.builder()
.device(GloveConfig.builder()
.name("primary")
.side(side)
.connection(SerialConnection.port(port))
.build())
.target(TargetConfig.builder()
.name("configured_hand")
.side(side)
.source(new DeviceSelector("glove", "primary", side))
.algorithm(new TargetAlgorithm(
packageId, boneConfigMode, algorithmConfigPath))
.build())
.build();
try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
GloveDevice device = glove.device();
device.output("configured_hand")
.subscribe(sample
-> System.out.println("retargeting sequence=" + sample.sequence
+ " target=" + sample.value.target + " joints=" + sample.value.joints));
glove.start();
glove.runBackground();
try {
Thread.sleep((long) (seconds * 1_000));
} finally {
glove.requestStop();
glove.join();
}
}
}
}packageId is the algorithm identifier from the actual delivery, and algorithmConfigPath is a path prepared by the app. Pass null when no file is needed; do not pass an empty string. prepareBoneConfig's link callback must return promptly; session expiry, failure and cancellation are reported through exceptions.
The built-in skeleton does not need an algorithm package and can still be selected with device.skeleton() before start; see Hand skeleton visualization. To install an algorithm package or read other target outputs, see Reading retargeting data.