Skip to content

Core Wear Detection Set Enabled

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

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

Objective-C

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

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

Objective-C

/// The protocol for device wear detection API.
@protocol AIBudsDeviceWearDetectionAPI <AIBudsDeviceAPI>
    /// 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.
    - (void)setWearDetectionEnabled:(BOOL)enabled
        completion:(AIBudsCompletionHandler _Nullable)completion;
@end

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

Objective-C

/// 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.
- (void)setWearDetectionEnabled:(BOOL)enabled
        completion:(AIBudsCompletionHandler _Nullable)completion;

Parameters

Parameter Type Description
enabled Bool / BOOL true/YES to enable wear detection; otherwise disable it.
completion AIBudsCompletionHandler? Optional completion handler invoked when the operation finishes.

Callback Parameters:

Name Type Description
success Bool / BOOL Whether the operation 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? 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")
}

Objective-C

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

id<AIBudsDeviceWearDetectionAPI> device =
    (id<AIBudsDeviceWearDetectionAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceWearDetectionAPI)]) {
    return;
}

if (device.wearDetectionCapability !=
    AIBudsWearDetectionCapabilitySupportedAndConfigurable) {
    NSLog(@"Wear detection is not configurable");
    return;
}

[device setWearDetectionEnabled:YES
    completion:^(BOOL success, NSError * _Nullable error) {
        if (!success) {
            NSLog(@"Failed to enable wear detection: %@",
                  error.localizedDescription);
            return;
        }
        NSLog(@"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.

AIBuds SDK iOS Wiki

Clone this wiki locally