功能分配
将 DeviceFunction 分配给受支持的 DeviceOperation。
前置条件
- 设备已连接并就绪
- 设备支持
DevicePhysicalOperationsAPI - 目标操作存在于
physicalOperationsMapping中
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
- Swift
- Objective-C
/// The protocol for device physical operations API.
protocol DevicePhysicalOperationsAPI: DeviceAPI {
/// Assign a device function to a specific operation.
///
/// - Parameters:
/// - function: The ``DeviceFunction`` to be assigned.
/// - operation: The ``DeviceOperation`` that will trigger the function.
/// - completion: An optional callback invoked when the assignment completes.
/// - success: `true` if the assignment succeeded; `false` otherwise.
/// - error: An error object explaining the failure, or `nil` on success.
func assignFunction(
_ function: DeviceFunction,
forPhysicalOperation operation: DeviceOperation,
completion: AIBudsCompletionHandler?
)
}/// The protocol for device physical operations API.
@protocol AIBudsDevicePhysicalOperationsAPI <AIBudsDeviceAPI>
/// Assign a device function to a specific operation.
///
/// - Parameters:
/// - function: The ``DeviceFunction`` to be assigned.
/// - operation: The ``DeviceOperation`` that will trigger the function.
/// - completion: An optional callback invoked when the assignment completes.
/// - success: `true` if the assignment succeeded; `false` otherwise.
/// - error: An error object explaining the failure, or `nil` on success.
- (void)assignFunction:(enum AIBudsDeviceFunction)function
forPhysicalOperation:(enum AIBudsDeviceOperation)operation
completion:(AIBudsCompletionHandler _Nullable)completion;
@end实例方法
将一个功能分配给指定的按键或触控操作。
- Swift
- Objective-C
/// Assign a device function to a specific operation.
///
/// - Parameters:
/// - function: The ``DeviceFunction`` to be assigned.
/// - operation: The ``DeviceOperation`` that will trigger the function.
/// - completion: An optional callback invoked when the assignment completes.
/// - success: `true` if the assignment succeeded; `false` otherwise.
/// - error: An error object explaining the failure, or `nil` on success.
func assignFunction(
_ function: DeviceFunction,
forPhysicalOperation operation: DeviceOperation,
completion: AIBudsCompletionHandler?
)/// Assign a device function to a specific operation.
///
/// - Parameters:
/// - function: The ``DeviceFunction`` to be assigned.
/// - operation: The ``DeviceOperation`` that will trigger the function.
/// - completion: An optional callback invoked when the assignment completes.
/// - success: `true` if the assignment succeeded; `false` otherwise.
/// - error: An error object explaining the failure, or `nil` on success.
- (void)assignFunction:(enum AIBudsDeviceFunction)function
forPhysicalOperation:(enum AIBudsDeviceOperation)operation
completion:(AIBudsCompletionHandler _Nullable)completion;参数
| 参数 | 类型 | 说明 |
|---|---|---|
function | DeviceFunction | 要分配的功能。 |
operation | DeviceOperation | 触发该功能的按键或触控操作。 |
completion | AIBudsCompletionHandler? | 可选的完成回调。 |
回调参数:
| 名称 | 类型 | 说明 |
|---|---|---|
success | Bool / BOOL | 分配是否成功。 |
error | NSError? | 失败详情;成功时为 nil。 |
返回值
该方法不直接返回值。
使用示例
- Swift
- Objective-C
import AIBuds
guard let device = device as? DevicePhysicalOperationsAPI else {
print("Device does not support physical-operation mapping")
return
}
device.assignFunction(
.playPause,
forPhysicalOperation: .rightDoubleClick
) { success, error in
guard success else {
print("Assignment failed: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Function assigned")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDevicePhysicalOperationsAPI> device = (id<AIBudsDevicePhysicalOperationsAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDevicePhysicalOperationsAPI)]) {
[device assignFunction:AIBudsDeviceFunctionPlayPause
forPhysicalOperation:AIBudsDeviceOperationRightDoubleClick
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Assignment failed: %@", error.localizedDescription);
return;
}
NSLog(@"Function assigned");
}];
}错误处理
检查 success,并通过 error 获取失败详情。完成回调报告成功前,不要更新本地映射。
最佳实践
- 以设备发布的映射为准:只提供
physicalOperationsMapping报告的操作。 - 使用 SDK 枚举:传入
DeviceOperation和DeviceFunction,不要使用任意整数。 - 成功后刷新:展示已应用配置时重新读取映射。
注意事项
- 每次调用只能为一个操作分配一个功能
- SDK 没有定义
KeyMapping模型或customizeKeyMapping方法