Skip to content

Core Physical Operations Assign Functions

Mr.P edited this page Aug 29, 2026 · 2 revisions

Assign Functions

Assign a DeviceFunction to a supported DeviceOperation.

Prerequisites

API Reference

Framework

AIBuds.xcframework

Import

Swift

import AIBuds

Objective-C

#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>

Protocol

Swift

/// 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?
    )
}

Objective-C

/// 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

Instance Method

Assigns a function to a physical operation.

Swift

/// 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?
)

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.
- (void)assignFunction:(enum AIBudsDeviceFunction)function
    forPhysicalOperation:(enum AIBudsDeviceOperation)operation
              completion:(AIBudsCompletionHandler _Nullable)completion;

Parameters

Parameter Type Description
function DeviceFunction The function to assign.
operation DeviceOperation The physical operation that triggers the function.
completion AIBudsCompletionHandler? Optional completion handler.

Callback Parameters:

Name Type Description
success Bool / BOOL Whether the assignment succeeded.
error NSError? Failure details, or nil on success.

Return Value

This method returns no value directly.

Usage Examples

Swift

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")
}

Objective-C

#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");
                  }];
}

Error Handling

Check success and use error for failure details. Do not update a local mapping until the completion reports success.

Best Practices

  1. Start from the Published Mapping: Offer operations reported by physicalOperationsMapping.
  2. Use SDK Enums: Pass DeviceOperation and DeviceFunction, not arbitrary integers.
  3. Refresh After Success: Re-read the mapping when presenting the applied configuration.

Notes

  • The API assigns one function to one operation per call.
  • The SDK does not define a KeyMapping model or customizeKeyMapping method.

AIBuds SDK iOS Wiki

Clone this wiki locally