SDKJava (Android) SDK

Haptics

Android Java SDK: Haptics

Current version · 2.0.0

Haptics

With a connected, running device, submit per-finger strengths through device.haptics(). Physical feedback depends on support in the actual device and delivery.

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.GloveFinger;

/**
 * Sets thumb and index haptics, holds them, then clears every channel.
 *
 * <p>Pass the host {@link Context}, an authorized transport, the glove port and side, and the
 * nonnegative hold time. The latest retained haptics sample is printed after the set; cleanup
 * always clears the channels.
 */
public final class Haptics {
  private Haptics() {}

  public static void run(Context context, SerialTransport transport, String port, Side side,
      double holdSeconds) throws InterruptedException {
    if (!Double.isFinite(holdSeconds) || holdSeconds < 0)
      throw new IllegalArgumentException("holdSeconds must be finite and nonnegative");
    SdkConfig config = SdkConfig.builder()
        .device(GloveConfig.builder()
                    .name("primary")
                    .side(side)
                    .connection(SerialConnection.port(port))
                    .build())
        .build();
    try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
      GloveDevice device = glove.device();
      glove.start();
      try {
        device.haptics().set(com.qnbot.sdk.glove.Haptics.builder()
                                 .set(GloveFinger.THUMB, 80)
                                 .set(GloveFinger.INDEX, 40)
                                 .build());
        device.haptics().latest().ifPresent(sample
            -> System.out.println("latest haptics sequence=" + sample.sequence
                + " value=" + sample.value.strengths));
        Thread.sleep((long) (holdSeconds * 1_000));
      } finally {
        device.haptics().clear();
      }
    }
  }
}

Haptics.run(...) starts the glove, sets thumb 80 and index 40, holds them for the requested seconds, then clears every channel in finally. The example class shares its name with com.qnbot.sdk.glove.Haptics, so the source builds the request with the fully qualified SDK type.

Choose a GloveFinger and an integer strength from 0–127. Omitted fingers retain their current strengths. Set one finger to 0 to stop it; clear() clears the haptics state. The example clears only after the hold time and does not schedule a per-finger stop; the app calls clear() or sets a new strength when its business timer ends.

StateChannel<Haptics> also supports latest(), next(), subscribe() and configured views; the example prints the latest retained sample after setting. Verify device support and stopping behavior at a low strength before integrating business feedback.

Glove uses the native 0–127 range without percentage scaling. Exo retains its separate 0–100 range.