SuperAPI AI 工具集

sample-app-guide

编程与开发

查看项目

更新于 11-08 17:06

技能介绍

Sample App Guide(iOS)是一份面向 iOS 开发者的 Meta Wearables DAT 示例应用构建指南,主题是“构建一个包含摄像头视频流与拍照的完整 DAT 应用”。资料以代码为主,按以下方面展开。

一、用途与项目准备 该指南用于指引开发者从零搭建一个 DAT iOS 示例应用,并可与 CameraAccess 示例配合使用。项目准备步骤包括:新建 Xcode 项目(SwiftUI App);通过 SPM 添加 SDK,仓库地址为 https://github.com/facebook/meta-wearables-dat-ios;在目标中添加 MWDATCore、MWDATCamera 和 MWDATMockDevice;配置 Info.plist(具体内容指向 Getting Started)。

二、应用架构 典型的 DAT 应用由以下部分组成:应用入口 MyDATApp.swift(负责 SDK 初始化);ViewModels 目录下的 WearablesViewModel(注册与设备管理)和 StreamViewModel(视频流与拍照);Views 目录下的 MainAppView(导航)、RegistrationView(注册 UI)和 StreamView(视频预览与拍照按钮)。

三、SDK 初始化 在 @main 应用结构的 init 中调用 try Wearables.configure(),并对失败使用 assertionFailure。在 WindowGroup 中加载 MainAppView,并通过 onOpenURL 检查 URL 查询参数是否包含 metaWearablesAction,若包含则调用 Wearables.shared.handleUrl(url)。

四、Wearables ViewModel

  • devicesStream() 产出的是 [DeviceIdentifier](即 [String]),而非设备对象;需要名称、类型、连接状态或兼容性时,用 wearables.deviceForIdentifier(_:) 解析标识符,再访问 nameOrId()、deviceType()、linkState、compatibility()。
  • 示例类 WearablesViewModel 为 @MainActor 的 ObservableObject,发布 registrationState 与 devices;observeState() 分别监听 registrationStateStream() 与 devicesStream();register() 调用 wearables.startRegistration();unregister() 调用 wearables.startUnregistration()。

五、Stream ViewModel

  • 示例类 StreamViewModel 发布 currentFrame(UIImage?)、streamState(String)与 capturedPhoto(Data?)。
  • startStream() 使用 StreamConfiguration(videoCodec: .raw, resolution: .medium, frameRate: 24),通过 AutoDeviceSelector 选择设备,调用 wearables.createSession(deviceSelector:) 创建会话并启动,等待会话状态变为 .started,再通过 deviceSession.addCamera(config:) 添加摄像头并获得 camera.stream。随后监听 stream.statePublisher、stream.videoFramePublisher(用 frame.makeUIImage() 转成图像)和 stream.photoDataPublisher(读取 photoData.data),最后调用 stream.start()。
  • stopStream() 中,停止摄像头会级联停止其子视频流,随后停止 deviceSession 并清空引用。
  • capturePhoto() 调用 stream?.capturePhoto(format: .jpeg)。

六、CameraAccess 示例也覆盖的行为

  • 两步显式流程:“Start Session”创建并启动 DeviceSession;“Preview”添加 Camera 并启动视频流。停止预览会保留会话连接,用户可再次启动而无需重建会话。
  • UI 绑定 SDK 自身状态:镜像 DeviceSessionState 与 StreamState,而不是维护并行状态机;在 starting、waitingForDevice、stopping 期间显示忙碌指示。
  • 后台处理:收到 UIApplication.didEnterBackgroundNotification 时结束会话,并抑制该主动停止期间 SDK 抛出的拆除错误,避免用户返回时看到虚假失败。
  • 更新提示:通过 device.addCompatibilityListener 监听 device.compatibility();遇到 .deviceUpdateRequired 时提供 Wearables.shared.openFirmwareUpdate();会话启动报 DeviceSessionError.datAppOnTheGlassesUpdateRequired 时提供 Wearables.shared.openDATGlassesAppUpdate()。
  • 统一错误呈现:将 session.errorPublisher 与 stream.errorPublisher 的 error.localizedDescription 集中在一个弹窗展示,而非应用自维护错误映射。

七、使用 MockDeviceKit 测试 引入 MWDATMockDevice 后,可调用 MockDeviceKit.shared.enable(),通过 pairGlasses(model: .rayBanMeta) 配对模拟眼镜,并依次 powerOn()、unfold()、don();可从 Bundle 加载 test_video.mp4 并通过 device.services.camera.setCameraFeed(fileURL:) 提供摄像头画面。结束时调用 MockDeviceKit.shared.disable()。

八、允许的依赖 DAT 应用只应依赖:MWDATCore(始终必需)、MWDATCamera(摄像头视频流)、MWDATMockDevice(测试用,可作为仅测试依赖)、MWDATDisplay(用于 Display 体验)。

限制与未提供信息:资料未说明 SDK 的系统版本要求、审核或上架流程、真实设备支持范围、费用或授权条款,也未给出示例仓库的完整目录文件内容;上述内容暂未提供。