摄像头固件更新
摄像头 OTA 用于更新受支持设备摄像头模块的固件,与设备主固件更新相互独立。
宿主 App 提供兼容的本地摄像头固件包。SDK 随后协调完整升级流程:配置设备热点、进入 Camera OTA 模式、启动本地 HTTP 服务器、向设备发送固件 URL、传输固件包并刷写摄像头固件。界面可通过阶段回调展示当前操作,最终升级结果以完成回调为准。
摄像头 OTA 升级流程
宿主 App 验证本地固件包后,由 SDK 协调传输和刷写,并通过完成回调返回最终结果。
前提条件
- 设备已连接,并遵循
DeviceCameraOtaAPI。 - 本地固件路径可读,且固件包与摄像头硬件匹配。
- App 可以加入设备热点、访问本地网络,并保持本地 HTTP 服务器可用。
- 避免媒体导入、摄像头 OTA 或其他热点流程同时运行。
使用 AI 辅助实现
让 AI 帮助实现此工作流
使用官方“更新 AIBuds 相机固件”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/update-aibuds-camera-firmware,使用该技能在当前 iOS 项目中完成“更新 AIBuds 相机固件”,并验证结果。API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds
import AIBudsFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
DeviceCameraOtaAPI 协议继承自 DeviceAPI,当前提供一个执行完整摄像头 OTA 流程的实例方法。
实例方法
- Swift
- Objective-C
/// Start camera OTA upgrade.
///
/// - Parameters:
/// - filePath: The readable local path of the camera firmware file.
/// - configureHotspotStartingHandler: Called when hotspot configuration starts.
/// - hotspotConfigureCompletionHandler: Called when hotspot configuration completes.
/// - success: `true` if configuration succeeded; otherwise `false`.
/// - error: The failure information, or `nil` if configuration succeeded.
/// - enterCameraOtaModeStartingHandler: Called when entry into camera OTA mode starts.
/// - enterCameraOtaModeCompletedHandler: Called when entry into camera OTA mode completes.
/// - success: `true` if the device entered OTA mode; otherwise `false`.
/// - error: The failure information, or `nil` if entry succeeded.
/// - waitingForHotspotOpenHandler: Called while waiting for the device hotspot to open.
/// - connectDeviceHotspotStartingHandler: Called when connecting to the device hotspot starts.
/// - ssid: The SSID of the device hotspot.
/// - deviceHotspotConnectCompletionHandler: Called when the hotspot connection completes.
/// - success: `true` if the connection succeeded; otherwise `false`.
/// - error: The failure information, or `nil` if the connection succeeded.
/// - httpServerStartingHandler: Called when the local HTTP server is starting.
/// - httpServerStartCompletionHandler: Called when the HTTP server start attempt completes.
/// - success: `true` if the server started; otherwise `false`.
/// - error: The failure information, or `nil` if the server started.
/// - sendingFirmwareUrlHandler: Called before sending the firmware URL to the device.
/// - firmwareUrl: The local HTTP URL that will be sent.
/// - firmwareUrlSendCompletionHandler: Called when firmware URL delivery completes.
/// - success: `true` if the URL was sent; otherwise `false`.
/// - error: The failure information, or `nil` if delivery succeeded.
/// - startCompletionHandler: Called when the Camera OTA start command completes.
/// - success: `true` if the update started; otherwise `false`.
/// - error: The failure information, or `nil` if the update started.
/// - fileTransferStartedHandler: Called when firmware transfer starts.
/// - fileTransferCompletedHandler: Called when firmware transfer completes.
/// - waitingForFlashingHandler: Called while the device is preparing to flash.
/// - flashingStartedHandler: Called when firmware flashing starts.
/// - flashingCompletedHandler: Called when firmware flashing completes.
/// - phaseProgressHandler: Reports progress for transfer and flashing.
/// - phase: `.transferring` or `.flashing`.
/// - progress: The phase progress in the range `0...100`.
/// - completionHandler: Called when the complete Camera OTA workflow finishes.
/// - success: `true` if the upgrade succeeded; otherwise `false`.
/// - error: The failure information, or `nil` if the upgrade succeeded.
func startCameraOta(
withFilePath filePath: String,
configureHotspotStartingHandler: AIBudsCameraOtaConfigureHotspotStartingHandler?,
hotspotConfigureCompletionHandler: AIBudsCameraOtaHotspotConfigureCompletionHandler?,
enterCameraOtaModeStartingHandler: AIBudsEnterCameraOtaModeStartingHandler?,
enterCameraOtaModeCompletedHandler: AIBudsEnterCameraOtaModeCompletionHandler?,
waitingForHotspotOpenHandler: AIBudsCameraOtaStartingToWaitForHotspotOpenHandler?,
connectDeviceHotspotStartingHandler: AIBudsCameraOtaConnectDeviceHotspotStartingHandler?,
deviceHotspotConnectCompletionHandler: AIBudsCameraOtaDeviceHotspotConnectCompletionHandler?,
httpServerStartingHandler: AIBudsCameraOtaHttpServerStartingHandler?,
httpServerStartCompletionHandler: AIBudsCameraOtaHttpServerStartCompletionHandler?,
sendingFirmwareUrlHandler: AIBudsCameraOtaSendingFirmwareUrlHandler?,
firmwareUrlSendCompletionHandler: AIBudsCameraOtaFirmwareUrlSendCompletionHandler?,
startCompletionHandler: AIBudsCameraOtaStartCompletionHandler?,
fileTransferStartedHandler: AIBudsCameraOtaFileTransferStartedHandler?,
fileTransferCompletedHandler: AIBudsCameraOtaFileTransferCompletedHandler?,
waitingForFlashingHandler: AIBudsCameraOtaWaitingForFlashingHandler?,
flashingStartedHandler: AIBudsCameraOtaFlashingStartedHandler?,
flashingCompletedHandler: AIBudsCameraOtaFlashingCompletedHandler?,
phaseProgressHandler: AIBudsCameraOtaPhaseProgressHandler?,
completionHandler: AIBudsCameraOtaCompletionHandler?
)/// Start camera OTA upgrade from a local firmware path.
///
/// - Parameters:
/// - filePath: The readable local path of the camera firmware file.
/// - configureHotspotStartingHandler: Called when hotspot configuration starts.
/// - hotspotConfigureCompletionHandler: Returns configuration success and error.
/// - enterCameraOtaModeStartingHandler: Called when entry into camera OTA mode starts.
/// - enterCameraOtaModeCompletedHandler: Returns OTA-mode entry success and error.
/// - waitingForHotspotOpenHandler: Called while waiting for the device hotspot to open.
/// - connectDeviceHotspotStartingHandler: Returns the device hotspot SSID.
/// - deviceHotspotConnectCompletionHandler: Returns hotspot connection success and error.
/// - httpServerStartingHandler: Called when the local HTTP server is starting.
/// - httpServerStartCompletionHandler: Returns HTTP server start success and error.
/// - sendingFirmwareUrlHandler: Returns the local firmware URL before it is sent.
/// - firmwareUrlSendCompletionHandler: Returns URL delivery success and error.
/// - startCompletionHandler: Returns Camera OTA start success and error.
/// - fileTransferStartedHandler: Called when firmware transfer starts.
/// - fileTransferCompletedHandler: Called when firmware transfer completes.
/// - waitingForFlashingHandler: Called while the device prepares to flash.
/// - flashingStartedHandler: Called when firmware flashing starts.
/// - flashingCompletedHandler: Called when firmware flashing completes.
/// - phaseProgressHandler: Returns `Transferring` or `Flashing` and progress from `0` to `100`.
/// - completionHandler: Returns final upgrade success and error.
- (void)startCameraOtaWithFilePath:(NSString *_Nonnull)filePath
configureHotspotStartingHandler:(AIBudsCameraOtaConfigureHotspotStartingHandler _Nullable)
configureHotspotStartingHandler
hotspotConfigureCompletionHandler:
(AIBudsCameraOtaHotspotConfigureCompletionHandler _Nullable)
hotspotConfigureCompletionHandler
enterCameraOtaModeStartingHandler:
(AIBudsEnterCameraOtaModeStartingHandler _Nullable)enterCameraOtaModeStartingHandler
enterCameraOtaModeCompletedHandler:
(AIBudsEnterCameraOtaModeCompletionHandler _Nullable)enterCameraOtaModeCompletedHandler
waitingForHotspotOpenHandler:
(AIBudsCameraOtaStartingToWaitForHotspotOpenHandler _Nullable)
waitingForHotspotOpenHandler
connectDeviceHotspotStartingHandler:
(AIBudsCameraOtaConnectDeviceHotspotStartingHandler _Nullable)
connectDeviceHotspotStartingHandler
deviceHotspotConnectCompletionHandler:
(AIBudsCameraOtaDeviceHotspotConnectCompletionHandler _Nullable)
deviceHotspotConnectCompletionHandler
httpServerStartingHandler:
(AIBudsCameraOtaHttpServerStartingHandler _Nullable)httpServerStartingHandler
httpServerStartCompletionHandler:
(AIBudsCameraOtaHttpServerStartCompletionHandler _Nullable)
httpServerStartCompletionHandler
sendingFirmwareUrlHandler:
(AIBudsCameraOtaSendingFirmwareUrlHandler _Nullable)sendingFirmwareUrlHandler
firmwareUrlSendCompletionHandler:
(AIBudsCameraOtaFirmwareUrlSendCompletionHandler _Nullable)
firmwareUrlSendCompletionHandler
startCompletionHandler:
(AIBudsCameraOtaStartCompletionHandler _Nullable)startCompletionHandler
fileTransferStartedHandler:
(AIBudsCameraOtaFileTransferStartedHandler _Nullable)fileTransferStartedHandler
fileTransferCompletedHandler:
(AIBudsCameraOtaFileTransferCompletedHandler _Nullable)fileTransferCompletedHandler
waitingForFlashingHandler:
(AIBudsCameraOtaWaitingForFlashingHandler _Nullable)waitingForFlashingHandler
flashingStartedHandler:
(AIBudsCameraOtaFlashingStartedHandler _Nullable)flashingStartedHandler
flashingCompletedHandler:
(AIBudsCameraOtaFlashingCompletedHandler _Nullable)flashingCompletedHandler
phaseProgressHandler:
(AIBudsCameraOtaPhaseProgressHandler _Nullable)phaseProgressHandler
completionHandler:
(AIBudsCameraOtaCompletionHandler _Nullable)completionHandler;请参阅 API Reference 中的 startCameraOta。
返回值
此方法不直接返回值。启动、阶段切换、进度和最终结果均通过提供的处理器传递。
流程阶段
以下是摄像头 OTA 流程的回调阶段,并不是 CameraOtaProgressPhase 的取值。
| 阶段 | 相关处理器 |
|---|---|
| 配置热点 | configureHotspotStartingHandler, hotspotConfigureCompletionHandler |
| 进入摄像头 OTA 模式 | enterCameraOtaModeStartingHandler, enterCameraOtaModeCompletedHandler |
| 连接设备热点 | waitingForHotspotOpenHandler, connectDeviceHotspotStartingHandler, deviceHotspotConnectCompletionHandler |
| 启动本地 HTTP 服务器 | httpServerStartingHandler, httpServerStartCompletionHandler |
| 发送固件 URL | sendingFirmwareUrlHandler, firmwareUrlSendCompletionHandler |
| 启动更新 | startCompletionHandler |
| 传输固件 | fileTransferStartedHandler, fileTransferCompletedHandler |
| 准备并刷写 | waitingForFlashingHandler, flashingStartedHandler, flashingCompletedHandler |
| 完成 | completionHandler |
进度阶段
CameraOtaProgressPhase 仅包含两个公开值。progress 参数为当前阶段报告的 0 至 100 整数。
| Swift | Objective-C | 原始值 | 含义 |
|---|---|---|---|
.transferring | AIBudsCameraOtaProgressPhaseTransferring | 1 | 固件文件传输进度。 |
.flashing | AIBudsCameraOtaProgressPhaseFlashing | 2 | 摄像头固件刷写进度。 |
使用示例
示例保留 SDK Demo 提供的全部回调。生产代码应在主队列更新 UI。
- Swift
- Objective-C
guard let cameraOtaDevice = device as? DeviceCameraOtaAPI else {
print("Camera OTA is not supported")
return
}
cameraOtaDevice.startCameraOta(
withFilePath: filePath,
configureHotspotStartingHandler: {
print("Configuring device hotspot")
},
hotspotConfigureCompletionHandler: { success, error in
print(
success
? "Hotspot configured"
: "Hotspot configuration failed: \(error?.localizedDescription ?? "Unknown error")")
},
enterCameraOtaModeStartingHandler: {
print("Entering Camera OTA mode")
},
enterCameraOtaModeCompletedHandler: { success, error in
print(
success
? "Camera OTA mode ready"
: "Could not enter Camera OTA mode: \(error?.localizedDescription ?? "Unknown error")"
)
},
waitingForHotspotOpenHandler: {
print("Waiting for device hotspot")
},
connectDeviceHotspotStartingHandler: { ssid in
print("Connecting to \(ssid)")
},
deviceHotspotConnectCompletionHandler: { success, error in
print(
success
? "Device hotspot connected"
: "Hotspot connection failed: \(error?.localizedDescription ?? "Unknown error")")
},
httpServerStartingHandler: {
print("Starting local HTTP server")
},
httpServerStartCompletionHandler: { success, error in
print(
success
? "HTTP server ready"
: "HTTP server failed: \(error?.localizedDescription ?? "Unknown error")")
},
sendingFirmwareUrlHandler: { firmwareURL in
print("Sending firmware URL: \(firmwareURL)")
},
firmwareUrlSendCompletionHandler: { success, error in
print(
success
? "Firmware URL sent"
: "URL delivery failed: \(error?.localizedDescription ?? "Unknown error")")
},
startCompletionHandler: { success, error in
print(
success
? "Camera OTA started"
: "Camera OTA could not start: \(error?.localizedDescription ?? "Unknown error")")
},
fileTransferStartedHandler: {
print("Firmware transfer started")
},
fileTransferCompletedHandler: {
print("Firmware transfer completed")
},
waitingForFlashingHandler: {
print("Waiting for flashing")
},
flashingStartedHandler: {
print("Firmware flashing started")
},
flashingCompletedHandler: {
print("Firmware flashing completed")
},
phaseProgressHandler: { phase, progress in
switch phase {
case .transferring:
print("Transfer: \(progress)%")
case .flashing:
print("Flashing: \(progress)%")
}
},
completionHandler: { success, error in
if success {
print("Camera OTA completed successfully")
} else {
print("Camera OTA failed: \(error?.localizedDescription ?? "Unknown error")")
}
}
)id<AIBudsDeviceCameraOtaAPI> cameraOtaDevice = (id<AIBudsDeviceCameraOtaAPI>)self.device;
if (![cameraOtaDevice conformsToProtocol:@protocol(AIBudsDeviceCameraOtaAPI)]) {
NSLog(@"Camera OTA is not supported");
return;
}
[cameraOtaDevice startCameraOtaWithFilePath:filePath
configureHotspotStartingHandler:^{
NSLog(@"Configuring device hotspot");
}
hotspotConfigureCompletionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Hotspot configured" : error.localizedDescription);
}
enterCameraOtaModeStartingHandler:^{
NSLog(@"Entering Camera OTA mode");
}
enterCameraOtaModeCompletedHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Camera OTA mode ready" : error.localizedDescription);
}
waitingForHotspotOpenHandler:^{
NSLog(@"Waiting for device hotspot");
}
connectDeviceHotspotStartingHandler:^(NSString *_Nonnull ssid) {
NSLog(@"Connecting to %@", ssid);
}
deviceHotspotConnectCompletionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Device hotspot connected" : error.localizedDescription);
}
httpServerStartingHandler:^{
NSLog(@"Starting local HTTP server");
}
httpServerStartCompletionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"HTTP server ready" : error.localizedDescription);
}
sendingFirmwareUrlHandler:^(NSString *_Nonnull firmwareUrl) {
NSLog(@"Sending firmware URL: %@", firmwareUrl);
}
firmwareUrlSendCompletionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Firmware URL sent" : error.localizedDescription);
}
startCompletionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Camera OTA started" : error.localizedDescription);
}
fileTransferStartedHandler:^{
NSLog(@"Firmware transfer started");
}
fileTransferCompletedHandler:^{
NSLog(@"Firmware transfer completed");
}
waitingForFlashingHandler:^{
NSLog(@"Waiting for flashing");
}
flashingStartedHandler:^{
NSLog(@"Firmware flashing started");
}
flashingCompletedHandler:^{
NSLog(@"Firmware flashing completed");
}
phaseProgressHandler:^(AIBudsCameraOtaProgressPhase phase, NSInteger progress) {
if (phase == AIBudsCameraOtaProgressPhaseTransferring) {
NSLog(@"Transfer: %ld%%", (long)progress);
} else if (phase == AIBudsCameraOtaProgressPhaseFlashing) {
NSLog(@"Flashing: %ld%%", (long)progress);
}
}
completionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(@"%@", success ? @"Camera OTA completed successfully" : error.localizedDescription);
}];错误处理
摄像头 OTA 错误使用 AIBudsSDK.CameraOtaErrorDomain 和 SdkCameraOtaErrorCode。公开枚举涵盖本地文件验证、网络或热点故障、任务生命周期故障、进入 OTA 模式失败和固件 URL 发送失败。
| 代码 | 常见情况 |
|---|---|
unknown, networkRequestFailed, networkTimeout | SDK 无法进一步细分错误,或网络活动失败。 |
cameraOtaFileNotFound, invalidCameraOtaFile, cameraOtaTaskCreateFailedDueToFileNotFound | 所选本地固件路径不存在或固件包无效。 |
hotspotDisconnectedDuringTransfer, invalidHotspotConfig | 设备热点无法可靠使用。 |
stageTwoTimeout, interruptedByShutdownOrLowBattery | 刷写接近完成时停滞,或设备中断更新。 |
cameraOtaTaskAlreadyRunning | 已有另一个摄像头 OTA 任务正在运行。 |
4003...4009 | 任务在设备连接、超时、热点设置、进入 OTA 模式、HTTP 服务或 URL 发送期间失败。 |
5001...5010 | 设备因命令、存储、权限、通道或状态问题拒绝进入 OTA 模式或接收固件 URL。 |
应展示具体失败阶段,而不是笼统的更新错误。刷写开始后,不要自动重试、断开连接、关机或启动其他热点流程。SDK 当前没有规定可安全重试的时机;重新提供固件包前,应先确认设备恢复状态和摄像头固件版本。
最佳实践
- 调用 API 前验证本地文件存在;固件包真实性和摄像头型号兼容性仍由宿主产品负责。
- 避免媒体文件导入、摄像头 OTA 或其他热点流程同时运行。
- 保持 App 活跃和本地 HTTP 服务器可访问,直至最终完成。
- 回调可能来自其他队列,因此 UI 更新应派发到主队列。
- 将
startCompletionHandler视为 OTA 操作的启动结果,而不是最终升级成功。 - 以
completionHandler作为最终结果,并在重新连接后验证摄像头固件状态。
注意事项
- 摄像头 OTA 使用本地固件路径;SDK 不查询远程更新服务,也不下载固件包。
phaseProgressHandler仅报告传输和刷写进度,其他流程阶段使用各自的专用回调。- 传输进度达到
100并不表示摄像头 OTA 已完成,随后仍需刷写固件并等待最终回调。 - 公开 SDK 契约当前未定义安全的自动重试行为。