機能の割り当て
DeviceFunction を、対応する DeviceOperation に割り当てます。
前提条件
- デバイスが接続済みで、使用可能な状態であること。
- デバイスが
DevicePhysicalOperationsAPIに対応していること。 - 対象操作が
physicalOperationsMappingに存在すること。
API Reference
フレームワーク
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を渡します。 - 成功後に再取得:適用済み設定を表示するときは割り当てを再取得します。
注意事項
- 1 回の呼び出しで、1 つの操作に 1 つの機能を割り当てます。
- SDK には
KeyMappingモデルやcustomizeKeyMappingメソッドは定義されていません。