Skip to main content

Assign Functions

Assign a DeviceFunction to a supported DeviceOperation.

Prerequisites

API Reference

Framework

AIBuds.xcframework

Import

Swift
import AIBuds

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

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

Parameters

ParameterTypeDescription
functionDeviceFunctionThe function to assign.
operationDeviceOperationThe physical operation that triggers the function.
completionAIBudsCompletionHandler?Optional completion handler.

Callback Parameters:

NameTypeDescription
successBool / BOOLWhether the assignment succeeded.
errorNSError?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")
}

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.