震动反馈
Android Java SDK:震动反馈
当前版本 · 2.0.0
震动反馈
设备连接并运行后,通过 device.haptics() 提交每根手指的震动强度。是否产生反馈取决于当前设备与交付支持。
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(...) 启动手套后把拇指设为 80、食指设为 40,保持指定秒数,最后在 finally 中清除所有通道。示例类名与 SDK 的 com.qnbot.sdk.glove.Haptics 同名,因此源码用全限定名创建请求。
GloveFinger 选择手指,强度为 0–127 的整数。未包含的手指保留当前强度;把单根手指设为 0 可停止该手指,clear() 清空当前震动状态。示例只在保持时间结束后清除,不自动为每根手指定时停止;由 app 在业务计时结束时调用 clear() 或设置新强度。
需要读取状态时,StateChannel<Haptics> 同时提供 latest()、next()、subscribe() 与配置视图;示例在设置后打印最新保留样本。先在低强度下验证产品支持和停止行为,再集成业务反馈。
Glove 使用原生 0–127 范围,不按百分比缩放;Exo 的独立强度范围仍为 0–100。