查找设备概述
查找 API 支持两个方向:让已连接设备发出查找提示,或在设备请求应用查找 iPhone 时作出响应。
这两个方向相反,使用不同的 API:
| 流程 | 发起方 | 应用职责 | 完成信号 |
|---|---|---|---|
| 查找设备 | 宿主 App | 通过 DeviceFindAPI 启动或停止已连接设备由固件定义的查找提示。 | 命令回调报告设备是否接受命令。受支持的设备之后可能触发 deviceDidReportFound。 |
| 查找 iPhone | 已连接设备 | 响应代理回调,启动或停止 iPhone 侧的声音、振动或 UI。 | 用户找到 iPhone 后,通过 FindPhoneStateReportingAPI 报告已解决。 |
功能概览
查找设备
在已连接的 AIBuds 设备上启动查找操作
停止查找设备
停止已连接 AIBuds 设备上的查找操作
上报已找到 iPhone
上报由设备发起的查找 iPhone 请求已经解决
使用 AI 辅助实现
使用 AI 开发
让 AI 帮助实现此工作流
使用官方“实现 AIBuds 查找设备流程”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-find-workflow,使用该技能在当前 iOS 项目中完成“实现 AIBuds 查找设备流程”,并验证结果。开始使用
使用查找功能时,请按以下步骤操作:
- 连接设备:与目标 AIBuds 设备建立可用连接。
- 选择方向:确定应用是在查找设备,还是响应设备发起的查找 iPhone 请求。
- 检查协议和能力支持:查找设备命令使用
DeviceFindAPI,并要求supportsFindDevice == true;报告已找到 iPhone 使用FindPhoneStateReportingAPI。 - 处理正确的结束信号:不要将命令被接受误认为实体设备或手机已被找到。
关键概念
查找设备
- 查找设备:请求已连接设备启动查找提示。
- 停止查找设备:请求已连接设备停止查找提示。
- 设备已找到事件:设备支持时,
deviceDidReportFound(_:)是由设备发起的独立结束事件。
DeviceFindAPI 操作的是已连接设备。显示相关控件或发送命令前,请检查 supportsFindDevice。如果已连接设备没有上报该能力,此属性默认为 false。该 API 不执行蓝牙发现,也不扫描附近设备。
查找手机
deviceDidRequestStartFindingPhone(_:)请求应用启动自身的 iPhone 侧提醒。deviceDidRequestStopFindingPhone(_:)请求应用停止该提醒。- 用户找到 iPhone 后,调用
notifyPhoneFound(_:)向请求设备报告已解决。
协议参考
查找设备命令通过 DeviceFindAPI 调用:
- Swift
- Objective-C
guard let device = device as? DeviceFindAPI else {
print("Device does not support DeviceFindAPI")
return
}
guard device.supportsFindDevice else {
print("Connected device does not support find-device commands")
return
}id<AIBudsDeviceFindAPI> device = (id<AIBudsDeviceFindAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceFindAPI)]) {
NSLog(@"Device does not support AIBudsDeviceFindAPI");
return;
}
if (!device.supportsFindDevice) {
NSLog(@"Connected device does not support find-device commands");
return;
}相反方向的查找 iPhone 流程在设备发起请求后使用 FindPhoneStateReportingAPI:
- Swift
- Objective-C
guard let reporter = device as? FindPhoneStateReportingAPI else {
print("Device cannot receive an iPhone-found report")
return
}id<AIBudsFindPhoneStateReportingAPI> reporter =
(id<AIBudsFindPhoneStateReportingAPI>)self.device;
if (![reporter conformsToProtocol:@protocol(AIBudsFindPhoneStateReportingAPI)]) {
NSLog(@"Device cannot receive an iPhone-found report");
return;
}最佳实践
- 区分两个方向:不要使用
DeviceFindAPI实现 iPhone 侧提醒。 - 检查协议和能力支持:确认设备支持所选流程需要的协议,并在显示查找设备控件前检查
supportsFindDevice。 - 区分接受与解决:成功回调只确认命令已处理,不表示目标已被实际找到。
- 提供停止入口:允许用户停止当前设备或手机提示。
- 在主队列更新 UI:将回调中的 UIKit 修改派发到主队列。
注意事项
- 查找设备命令要求目标设备保持连接且可达。
- 设备侧声音、振动或其他提示行为由固件定义。
- 查找 iPhone 请求所用的 iPhone 侧声音、振动和 UI 由应用负责。
supportsFindDevice == false可能表示不支持该功能,也可能表示设备没有上报该能力。- 两个协议都没有提供可直接读取的活动状态或持续时间。