SDKJava (Android) SDK

Timestamps & external time sync

Android Java SDK: Timestamps & external time sync

Current version · 2.0.0

Timestamps & external time sync

External time synchronization

Call Glove.syncTime(double referenceTimeMs) to synchronize the whole Glove domain with absolute Unix milliseconds from an external time source. The value supports fractional milliseconds and must be finite; convert seconds or nanoseconds before passing it.

TimeSync.run(context, transport) synchronizes before start and, while the background runtime runs, reads the time source again every second and re-synchronizes. The no-argument overload uses System::currentTimeMillis; pass a DoubleSupplier for a custom source, for example glove.syncTime(externalTimeMs.getAsDouble()) when the source variable is named externalTimeMs. For the local system clock, () -> (double) System.currentTimeMillis() also works.

import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
import com.qnbot.sdk.glove.GloveDevice;
import java.util.function.DoubleSupplier;

/**
 * Runs in the background while resynchronizing Glove time every second.
 *
 * <p>Pass the host {@link Context}, an authorized transport and an external Unix-millisecond time
 * source. The SDK auto-discovers the only glove; pose and status subscriptions show the
 * synchronized timestamps. Interrupt the worker to stop and join the runner.
 */
public final class TimeSync {
  private TimeSync() {}

  /** Resynchronizes from the host clock. */
  public static void run(Context context, SerialTransport transport) throws InterruptedException {
    run(context, transport, System::currentTimeMillis);
  }

  public static void run(Context context, SerialTransport transport,
      DoubleSupplier referenceTimeMillis) throws InterruptedException {
    SdkConfig config = SdkConfig.builder().device(GloveConfig.builder().build()).build();
    try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
      GloveDevice device = glove.device();
      double reference = referenceTimeMillis.getAsDouble();
      glove.syncTime(reference);
      System.out.println("reference_time_ms=" + reference);
      device.pose().subscribe(sample
          -> System.out.println("pose sequence=" + sample.sequence
              + " source_time_ms=" + sample.sourceTime + " observed_at_ms=" + sample.observedAt));
      device.status().subscribe(sample
          -> System.out.println("status source_time_ms=" + sample.sourceTime
              + " observed_at_ms=" + sample.observedAt + " connected=" + sample.value.connected));
      glove.start();
      glove.runBackground();
      try {
        while (true) {
          Thread.sleep(1_000);
          reference = referenceTimeMillis.getAsDouble();
          glove.syncTime(reference);
          System.out.println("reference_time_ms=" + reference);
        }
      } finally {
        glove.requestStop();
        glove.join();
      }
    }
  }
}

While the Glove domain remains open, synchronize before start or periodically during execution. Re-synchronization updates subsequent source times across all Glove sources without restarting devices or subscriptions or rewriting delivered data.

Synchronization affects SDK-assigned pose, IMU and status producer times, plus newly created capture/calibration result times. An external frame's explicit timestampMs still takes precedence. Sample.sourceTime uses the producer timeline, while Sample.observedAt retains SDK-relative monotonic time. Both use milliseconds, but subtracting them does not measure latency. Exo time and runtime scheduling remain independent.

NaN or infinity raises InvalidArgumentException and preserves the previous reference. A closed Glove domain or SDK raises ClosedHandleException. This method synchronizes time used for SDK data; it sends no hardware time-setting command.

Timestamps

Use sourceTime to align sources on a shared reference clock. Use differences between observedAt values to measure sample intervals as observed by the SDK.