Get and Set On-Device Mode
Read the active on-device voice-assistant mode and request a mode supported by the device capability.
Prerequisites
- The device is connected and conforms to
OnDeviceVoiceAssistantAPI. onDeviceVoiceAssistantCapabilityis not.none.- The target mode is compatible with the reported capability and is not
.unknown.
API Reference
Framework
AIBuds.xcframework
Declaration
- Swift
- Objective-C
/// The protocol for the on-device voice assistant API.
protocol OnDeviceVoiceAssistantAPI: DeviceAPI {
/// The current mode of the on-device voice assistant.
var onDeviceVoiceAssistantMode: OnDeviceVoiceAssistantMode { get }
/// Sets the mode of the on-device voice assistant.
/// - Parameters:
/// - mode: The desired mode to set.
/// - completion: Called when the operation completes.
/// - success: `true` when the operation succeeds.
/// - error: Error details on failure; otherwise `nil`.
func setOnDeviceVoiceAssistantMode(_ mode: OnDeviceVoiceAssistantMode,
completion: AIBudsCompletionHandler?)
}/// The protocol for the on-device voice assistant API.
@protocol AIBudsOnDeviceVoiceAssistantAPI <AIBudsDeviceAPI>
/// The current mode of the on-device voice assistant.
@property(nonatomic, readonly) AIBudsOnDeviceVoiceAssistantMode onDeviceVoiceAssistantMode;
/// Sets the mode of the on-device voice assistant.
/// - Parameters:
/// - mode: The desired mode to set.
/// - completion: Called when the operation completes.
/// - success: `YES` when the operation succeeds.
/// - error: Error details on failure; otherwise `nil`.
- (void)setOnDeviceVoiceAssistantMode:(enum AIBudsOnDeviceVoiceAssistantMode)mode
completion:(AIBudsCompletionHandler _Nullable)completion;
@endSee onDeviceVoiceAssistantMode, setOnDeviceVoiceAssistantMode, and OnDeviceVoiceAssistantMode.
Compatible Modes
| Capability | Modes to present |
|---|---|
.none | No mode controls |
.keywordWakeup | .disabled, .basic |
.common | .disabled, .basic, .advanced |
Usage Examples
- Swift
- Objective-C
guard let voiceAssistant = device as? OnDeviceVoiceAssistantAPI else {
print("On-device voice assistant is not supported")
return
}
print("Current mode: \(voiceAssistant.onDeviceVoiceAssistantMode)")
guard voiceAssistant.onDeviceVoiceAssistantCapability == .common else {
print("Advanced mode is not supported")
return
}
voiceAssistant.setOnDeviceVoiceAssistantMode(.advanced) { success, error in
guard success else {
print(error?.localizedDescription ?? "Unable to change mode")
return
}
print("Mode changed to advanced")
}id<AIBudsOnDeviceVoiceAssistantAPI> voiceAssistant =
(id<AIBudsOnDeviceVoiceAssistantAPI>)self.device;
if (![voiceAssistant conformsToProtocol:@protocol(AIBudsOnDeviceVoiceAssistantAPI)]) {
NSLog(@"On-device voice assistant is not supported");
return;
}
NSLog(@"Current mode: %ld", (long)voiceAssistant.onDeviceVoiceAssistantMode);
if (voiceAssistant.onDeviceVoiceAssistantCapability !=
AIBudsOnDeviceVoiceAssistantCapabilityCommon) {
NSLog(@"Advanced mode is not supported");
return;
}
[voiceAssistant
setOnDeviceVoiceAssistantMode:AIBudsOnDeviceVoiceAssistantModeAdvanced
completion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"%@", error.localizedDescription ?: @"Unable to change mode");
return;
}
NSLog(@"Mode changed to advanced");
}];Notes
.unknownrepresents unavailable state and must not be sent as a target mode.- A successful callback confirms the request. Refresh UI from the device property or mode-change delegate callback.
.advancedincludes call-state voice control and should only be offered for.commoncapability.