未完成内容,不代表最终质量
JS 接口参考

Device

功能简介

负责与小米穿戴设备的发现、状态同步及控制。

权限声明

使用此接口需要向 manifest 的 permissions 中加入 device 权限。

相关类型

类型字段描述
MiWearDeviceInfoname: stringaddr: string设备蓝牙名称及 MAC 地址
MiWearBleCharaUuidrecv: stringsent: stringBLE 特征值 UUID(接收/发送)(仅 macOS / iOS 可用)
SecurityKeysenc_key: number[]dec_key: number[]enc_nonce: number[]dec_nonce: number[]一次性 Session 密钥(对插件来说没什么卵用)
MiWearDeviceStatename: stringaddr: stringauthkey: stringbleservice: MiWearBleCharaUuidmax_frame_size: numbersec_keys?: SecurityKeys | nullnetwork_mtu: numbercodename: string设备运行状态及会话上下文

接口

getDeviceList

  • 签名getDeviceList(): MiWearDeviceInfo[]
  • 说明:返回当前已配对设备列表。

getDeviceState

  • 签名getDeviceState(addr: string): MiWearDeviceState
  • 参数
    • addr: string – 目标设备 MAC 地址
  • 返回:该设备的完整状态对象。

modifyDeviceState

  • 签名modifyDeviceState(addr: string, state: MiWearDeviceState): void
  • 功能:整体更新设备状态(会覆写旧的 state,高危操作)。

disconnectDevice

  • 签名disconnectDevice(): Promise<void>
  • 功能:主动断开当前设备的连接。

示例

import AstroBox from "astrobox-plugin-sdk";

// 获取设备列表
const devices = AstroBox.device.getDeviceList();
devices.forEach(d => console.log(`${d.name} @ ${d.addr}`));

// 读取状态并修改
const st = AstroBox.device.getDeviceState(devices[0].addr);
st.max_frame_size = 1024;
AstroBox.device.modifyDeviceState(st.addr, st);

// 断开连接
await AstroBox.device.disconnectDevice();

大纲