Skip to main content

Set Wear Detection

Enable or disable wear detection when the device reports a configurable capability.

Prerequisites​

  • The device is connected and ready.
  • The device conforms to DeviceWearDetectionAPI.
  • wearDetectionCapability is .supportedAndConfigurable.

API Reference​

Framework​

AIBuds.xcframework

Import​

Swift
import AIBuds

Protocol​

The method is defined by DeviceWearDetectionAPI.

Swift
/// The protocol for device wear detection API.
protocol DeviceWearDetectionAPI: DeviceAPI {
    /// Enable or disable wear-detection functionality
    /// - Parameters:
    ///   - enabled: Whether to enable wear detection
    ///   - 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 setWearDetection(
        enabled: Bool,
        completion: AIBudsCompletionHandler?
    )
}

Instance Method​

Enables or disables wear detection.

Open setWearDetection in the API Reference.
Swift
/// Enable or disable wear-detection functionality
/// - Parameters:
///   - enabled: Whether to enable wear detection
///   - 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 setWearDetection(
    enabled: Bool,
    completion: AIBudsCompletionHandler?
)

Parameters​

ParameterTypeDescription
enabledBool / BOOLtrue/YES to enable wear detection; otherwise disable it.
completionAIBudsCompletionHandler?Optional completion handler invoked when the operation finishes.

Callback Parameters:

NameTypeDescription
successBool / BOOLWhether the operation 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? DeviceWearDetectionAPI else {
    print("Device does not support wear detection")
    return
}

guard device.wearDetectionCapability == .supportedAndConfigurable else {
    print("Wear detection is not configurable")
    return
}

device.setWearDetection(enabled: true) { success, error in
    guard success else {
        print("Failed to enable wear detection: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Wear detection enabled")
}

Error Handling​

Check success before updating local UI and use error for failure details. Do not call the method when the capability is .none or .supportedNotConfigurable.

Best Practices​

  1. Validate Capability: Require .supportedAndConfigurable before sending the command.
  2. Avoid Redundant Commands: Compare the requested value with isWearDetectionEnabled first.
  3. Refresh After Success: Read the current property or wait for didWearDetectionEnabledChanged before presenting the applied state.

Notes​

  • The SDK Demo performs the same capability check before enabling or disabling wear detection.
  • The method does not define automatic music pause or resume behavior.