SDKJava (Android) SDK

Device discovery & connection

Android Java SDK: Device discovery & connection

Current version · 2.0.0

Device discovery & connection

The USB transport supports single-port CDC ACM. Integrate other serial chipsets or multi-port devices through an app implementation of com.qnbot.sdk.android.SerialTransport.

Select and authorize

The app obtains system authorization through its own USB flow, then hands the authorized SerialTransport to the SDK; public examples start at that point and do not include the permission dialog. DiscoverGloves.run(context, transport) lists glove candidates over the authorized transport, printing provenance, port, hand, serial number and any per-device error.

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);
      }
    }
  }
}

Discovery closes temporary endpoints and leaves the transport available for the Sdk constructor. The app displays candidates and keeps the selected port; do not open every inventoried device.

Declare and select

Set the actual Side, optional device name and SerialConnection.port(selectedPort) in GloveConfig.builder(), then add it to SdkConfig. Construct new Sdk(config, context, transport) and select with sdk.glove().device(name, side). A name or side matching several devices raises AmbiguousSelectionException; DeviceSelection.run(context) demonstrates selection by name and side with external connections and checks the expected ambiguities. Give multiple configured devices unique names and keep the declaration for a single hand.

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");
  }
}

If selection ends before the SDK accepts the transport, the app must close it. Once accepted, end the session through the SDK lifecycle.

Read resolved device information

After selecting a verified direct-serial or Receiver member device, call getDeviceInfo() before or after start(). The returned snapshot identifies the resolved device and connection used by that GloveDevice. ExternalConnection and any other source without verified identity information throw 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));
    }
  }
}

Read canonicalSn, deviceType, model, firmwareVersion, hand, handSource and transportType from the snapshot. A direct glove reports SERIAL; a resolved Receiver member reports RECEIVER_PEER and an empty handSource because the Receiver does not report how the hand assignment was determined. RECEIVER_USB belongs to DiscoveredGlove.transportType for the Receiver USB candidate and is not a resolved member's GloveDeviceInfo value.

Receiver and two-hand configuration

Select and authorize the Receiver USB port in the app. Configure each wireless glove with an ordinary GloveConfig, its name and explicit side, and the same SerialConnection.port(selectedPort). For direct gloves, select each glove's own port. DeviceLifecycle.run(...) runs two named devices and stops and restarts one hand: the left pose keeps advancing while the stopped right pose does not.

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");
    }
  }
}

Construct the SDK using this configuration, app Context and authorized transport. Select gloves with sdk.glove().device("left") and device("right"), then follow the existing start/subscription/shutdown lifecycle. For one hand, include only its declaration. Two enabled gloves sharing one Receiver need distinct explicit sides.

CompositeExoGloveConfig.builder().name(...) names an Exo+Glove combination independently of its member names; it does not replace selection through device(name, side).

Shared serial port for Exo and gloves

Install both components from the matched offline delivery. Pass the authorized shared port once, on the outer configuration. Create one Sdk from this configuration and call sdk.start() to start the three members. Use sdk.glove() and sdk.exo() to select their data channels. The composite examples QuickStart, DiscoverExoGlove, GloveRetargeting and GloveHaptics live in Glove + Exo composite solution; this page does not repeat their configuration code.

Typed discovery provenance

DiscoveredGlove.transportType from Sdk.discoverGloves(context, transport) is GloveTransportType.SERIAL, RECEIVER_USB or RECEIVER_PEER, identifying wired gloves, Receiver USB or wireless-member provenance. Partial and error-bearing candidates retain their provenance. Do not infer it from port strings or product names.

On Android, call Sdk.discoverGloves(context, transport) on a worker thread with an authorized host transport. Discovery closes temporary endpoints and leaves the transport available for SDK construction.