报告自有 AI 服务鉴权
reportSelfAiServiceAuthResult 是用于衔接设备流程的状态通知,并不负责执行鉴权。使用自有 AI 服务时,宿主 App 需要自行完成服务鉴权,再将结果报告给已连接的设备。
设备收到自有 AI 服务鉴权成功的结果后,才能继续符合条件的 AI 流程。如果没有上报,即使宿主 App 中的 AI 服务已经可用,设备仍可能保持 AI 流程阻塞,例如不会开启当前会话所需的 Opus 语音流。
何时需要调用
该 API 适用于由宿主 App 自行接入和鉴权的 AI 服务。宿主 App 完成鉴权后:
- 只有自有 AI 服务已经可用时才报告
true。 - 鉴权失败或服务不可用时报告
false。 - completion 成功后,才能认为结果已经送达设备。
SDK 提供的 AI 服务有各自的鉴权流程,不应使用该 API 替代服务商鉴权。
API 参考
- Swift
- Objective-C
/// Reports the authentication result for the self-AI service.
/// - Parameters:
/// - isAuthenticated: Whether the self-AI service is authenticated.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func reportSelfAiServiceAuthResult(
_ isAuthenticated: Bool,
completion: AIBudsCompletionHandler?
)/// Reports the authentication result for the self-AI service.
- (void)reportSelfAiServiceAuthResult:(BOOL)isAuthenticated
completion:(AIBudsCompletionHandler _Nullable)completion;请参阅 reportSelfAiServiceAuthResult。
使用示例
- Swift
- Objective-C
func selfAIAuthenticationDidFinish(isAuthenticated: Bool) {
guard let device = device as? DeviceServiceAuthAPI else { return }
// Report the result only after the host app's own AI authorization finishes.
device.reportSelfAiServiceAuthResult(isAuthenticated) { reportSucceeded, error in
guard reportSucceeded else {
print(error?.localizedDescription ?? "Failed to report authorization")
return
}
if isAuthenticated {
// The device can now continue eligible AI flows, including an Opus input stream.
print("Self-AI authorization delivered to the device")
}
}
}- (void)selfAIAuthenticationDidFinish:(BOOL)isAuthenticated {
id<AIBudsDeviceServiceAuthAPI> device = (id<AIBudsDeviceServiceAuthAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceServiceAuthAPI)])
return;
// Report the result only after the host app's own AI authorization finishes.
[device reportSelfAiServiceAuthResult:isAuthenticated
completion:^(BOOL reportSucceeded, NSError *error) {
if (!reportSucceeded) {
NSLog(@"%@", error.localizedDescription);
return;
}
if (isAuthenticated) {
// The device can now continue eligible AI flows, including
// an Opus input stream.
NSLog(@"Self-AI authorization delivered to the device");
}
}];
}completion 表示鉴权结果是否成功送达设备,并不表示该 API 完成了 AI 服务鉴权。不要通过该 API 发送访问 token 或凭据;它只接收布尔结果。