audioaccessorykit
dpearson2699/swift-ios-skills
Enable automatic audio routing for paired accessories with placement and source detection.
What is audioaccessorykit?
AudioAccessoryKit provides automatic audio switching and intelligent routing for third-party audio accessories on iOS 26.4+. Use it to register audio accessory configuration from your container app, report placement and connected audio sources from an app extension, and let the system intelligently route audio output based on device state.
- Register audio accessories for automatic switching after pairing via AccessorySetupKit
- Report device placement (inEar, onHead, overTheEar, offHead) to guide audio routing decisions
- Track primary and secondary connected Bluetooth audio sources via device identifiers
- Enable or disable audio switching and placement capabilities per accessory
- Update configuration from app extensions to reflect real-time accessory state changes
- Query device capabilities to determine supported features
How to install audioaccessorykit
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill audioaccessorykit- Accessory must be paired via AccessorySetupKit first
- iOS 26.4 or later, or iPadOS 26.4 or later
- Import AudioAccessoryKit and AccessorySetupKit frameworks
- ASAccessory object from AccessorySetupKit pairing flow
How to use audioaccessorykit
- 1.Import AudioAccessoryKit and AccessorySetupKit in your container app and app extension
- 2.Pair the accessory using AccessorySetupKit to obtain an ASAccessory object
- 3.Create an AccessoryControlDevice.Configuration with desired capabilities (audioSwitching, placement) and initial state
- 4.Call AccessoryControlDevice.register(accessory, configuration) from the container app
- 5.In the app extension, retrieve the device with AccessoryControlDevice.current(for: accessory)
- 6.Update placement or audio source identifiers via device.update(config) whenever state changes
- 7.Query device.configuration.deviceCapabilities to check supported features at runtime
Use cases
- Implement automatic audio output switching when users put on or remove wireless earbuds or headphones
- Route audio to the correct paired device when an accessory connects to multiple Bluetooth sources simultaneously
- Report earbud placement detection (in-ear vs. off-head) to optimize system audio routing
- Update connected audio source identifiers when Bluetooth devices connect or disconnect
- Build companion apps for third-party audio accessories that integrate with iOS audio routing
- iOS app developers building companion apps for third-party audio accessories
- Audio accessory manufacturers integrating with Apple's audio routing system
- App extension developers managing real-time accessory state updates
- Teams implementing automatic audio switching for wireless headphones or earbuds
audioaccessorykit FAQ
Use it when building a companion app for a third-party audio accessory that needs to participate in iOS automatic audio routing, report its physical placement, or manage connections to multiple Bluetooth sources.
Yes. The accessory must be paired via AccessorySetupKit first to obtain an ASAccessory object, which is then registered with AudioAccessoryKit.
Call register() from the container app after pairing. Call update() from the app extension to report real-time state changes like placement or connected sources.
Report .inEar, .onHead, .overTheEar when worn, or .offHead when not being worn. Update promptly on every detected change for responsive audio routing.
Set primaryAudioSourceDeviceIdentifier and secondaryAudioSourceDeviceIdentifier in the configuration as Data objects containing the 6-byte Bluetooth addresses of connected devices.
Full instructions (SKILL.md)
Source of truth, from dpearson2699/swift-ios-skills.
name: audioaccessorykit description: "Support audio accessory features like automatic switching using AudioAccessoryKit. Use when implementing automatic audio routing for paired accessories, registering audio accessory configuration from the container app, updating placement or connected audio source identifiers from an app extension, or handling AccessoryControlDevice capabilities and errors."
AudioAccessoryKit
Automatic audio switching support and intelligent audio routing inputs for third-party audio accessories. Enables companion apps to register audio accessory configuration with the system, and app extensions to report placement and connected source changes that help the system switch audio output. Available iOS 26.4+ / iPadOS 26.4+.
Beta-sensitive. AudioAccessoryKit is new in iOS 26.4. Re-check current Apple documentation before relying on specific API details.
AudioAccessoryKit builds on top of AccessorySetupKit. The accessory must first
be paired via AccessorySetupKit before it can be registered for audio features.
The central type is AccessoryControlDevice, which registers a
Configuration from the container app and applies ongoing configuration updates
from the app extension.
Contents
- Setup
- Session Management
- Audio Switching
- Device Placement
- Connected Audio Sources
- Feature Discovery
- Error Handling
- Common Mistakes
- Review Checklist
- References
Setup
Prerequisites
- Pair the accessory over Bluetooth using AccessorySetupKit. This yields an
ASAccessoryobject. - Import the frameworks where needed in the container app and extension:
import AccessorySetupKit
import AudioAccessoryKit
Framework Availability
| Platform | Minimum Version |
|---|---|
| iOS | 26.4+ |
| iPadOS | 26.4+ |
Session Management
Registering an Accessory
After pairing via AccessorySetupKit, register the accessory from the container
app by passing an AccessoryControlDevice.Configuration that describes the
capabilities and any initial state the accessory supports:
let accessory: ASAccessory // Obtained from AccessorySetupKit pairing
let configuration = AccessoryControlDevice.Configuration(
devicePlacement: .offHead,
deviceCapabilities: [.audioSwitching, .placement]
)
try await AccessoryControlDevice.register(accessory, configuration)
Registration activates the specified capabilities and gives the system the configuration it needs to participate in audio routing decisions.
Retrieving the Current Configuration
In the app extension, access the device's current configuration using the
static current(for:) method:
let device = try AccessoryControlDevice.current(for: accessory)
let currentConfig = device.configuration
This returns the AccessoryControlDevice instance associated with the paired
ASAccessory. The device exposes both the accessory reference and the
current configuration. Apple marks current(for:) as app-extension-only.
Updating Configuration
In the app extension, push configuration changes to the system with
update(_:). Only update fields for capabilities that were declared during
registration:
let device = try AccessoryControlDevice.current(for: accessory)
var config = device.configuration
config.devicePlacement = .onHead
try await device.update(config)
The update call is async and can throw AccessoryControlDevice.Error on
failure. Apple marks update(_:) as app-extension-only.
Audio Switching
Automatic audio switching lets the system intelligently route audio output to the correct device based on placement and connected sources.
Enabling Audio Switching
Declare the .audioSwitching capability in the registration configuration:
let configuration = AccessoryControlDevice.Configuration(
deviceCapabilities: [.audioSwitching]
)
try await AccessoryControlDevice.register(accessory, configuration)
For Apple's automatic switching workflow, include both .audioSwitching and
.placement when the accessory can report placement:
let configuration = AccessoryControlDevice.Configuration(
devicePlacement: .offHead,
deviceCapabilities: [.audioSwitching, .placement]
)
try await AccessoryControlDevice.register(accessory, configuration)
Capabilities
AccessoryControlDevice.Capabilities is an option set with two members:
| Capability | Purpose |
|---|---|
.audioSwitching | Device supports automatic audio switching |
.placement | Device can report its physical placement |
Both capabilities can be combined. Do not declare .placement unless the
accessory can keep the system updated with real placement state.
Device Placement
Report the physical position of the accessory from the app extension to help the system make routing decisions. Update placement whenever the accessory detects a position change.
Placement Values
AccessoryControlDevice.Placement defines four cases:
| Placement | Meaning |
|---|---|
.inEar | Accessory is seated in the ear (e.g., earbuds) |
.onHead | Accessory is on the head (e.g., headband headphones) |
.overTheEar | Accessory is over the ear (e.g., over-ear headphones) |
.offHead | Accessory is not being worn |
Updating Placement
let device = try AccessoryControlDevice.current(for: accessory)
var config = device.configuration
config.devicePlacement = .inEar
try await device.update(config)
Common transitions:
.offHeadto.onHeador.inEarwhen the user puts on the accessory.onHeador.inEarto.offHeadwhen removed- Update promptly on every detected change for responsive audio routing
Connected Audio Sources
For accessories that connect to multiple Bluetooth devices simultaneously, inform the system from the app extension which devices are connected. This lets the system route audio from the appropriate source.
Setting Audio Source Identifiers
Provide the Bluetooth address of connected devices as Data:
let device = try AccessoryControlDevice.current(for: accessory)
var config = device.configuration
let primaryBTAddress = Data([0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC])
config.primaryAudioSourceDeviceIdentifier = primaryBTAddress
let secondaryBTAddress = Data([0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45])
config.secondaryAudioSourceDeviceIdentifier = secondaryBTAddress
try await device.update(config)
Update these identifiers when the Bluetooth connection state changes (new device connects, existing device disconnects).
Configuration Properties
AccessoryControlDevice.Configuration contains all configurable state:
| Property | Type | Purpose |
|---|---|---|
deviceCapabilities | Capabilities | Declared device capabilities |
devicePlacement | Placement? | Current physical placement |
primaryAudioSourceDeviceIdentifier | Data? | Primary connected Bluetooth device address |
secondaryAudioSourceDeviceIdentifier | Data? | Secondary connected Bluetooth device address |
Feature Discovery
Querying Capabilities
In the app extension, inspect the device's declared capabilities through its configuration:
let device = try AccessoryControlDevice.current(for: accessory)
let caps = device.configuration.deviceCapabilities
if caps.contains(.audioSwitching) {
// Device supports automatic audio switching
}
if caps.contains(.placement) {
// Device reports physical placement
}
Checking Placement
Read the current placement to determine if the accessory is being worn:
let device = try AccessoryControlDevice.current(for: accessory)
if let placement = device.configuration.devicePlacement {
switch placement {
case .inEar, .onHead, .overTheEar:
// Accessory is being worn
break
case .offHead:
// Accessory is not being worn
break
@unknown default:
break
}
}
Error Handling
AccessoryControlDevice.Error covers failure cases during registration and
updates:
| Error | Cause |
|---|---|
.accessoryNotCapable | Accessory does not support the requested capability |
.invalidRequest | Request parameters are invalid |
.invalidated | Device registration has been invalidated |
.unknown | An unspecified error occurred |
Handle errors from registration and update calls:
let configuration = AccessoryControlDevice.Configuration(
devicePlacement: .offHead,
deviceCapabilities: [.audioSwitching, .placement]
)
do {
try await AccessoryControlDevice.register(accessory, configuration)
} catch let error as AccessoryControlDevice.Error {
switch error {
case .accessoryNotCapable:
// Accessory hardware does not support requested capabilities
break
case .invalidRequest:
// Check registration parameters
break
case .invalidated:
// Coordinate container-app registration again
break
case .unknown:
// Log and retry
break
@unknown default:
break
}
}
Common Mistakes
DON'T: Register before pairing with AccessorySetupKit
// WRONG -- no ASAccessory from a completed AccessorySetupKit pairing
try await AccessoryControlDevice.register(unknownAccessory, configuration)
// CORRECT -- use the ASAccessory from a completed pairing session
session.activate(on: .main) { event in
if event.eventType == .accessoryAdded, let accessory = event.accessory {
Task {
let configuration = AccessoryControlDevice.Configuration(
deviceCapabilities: [.audioSwitching]
)
try await AccessoryControlDevice.register(accessory, configuration)
}
}
}
DON'T: Declare placement capability without updating placement
// WRONG -- registers placement but never updates it
let registration = AccessoryControlDevice.Configuration(
deviceCapabilities: [.audioSwitching, .placement]
)
try await AccessoryControlDevice.register(accessory, registration)
// System never receives placement data, reducing switching accuracy
// CORRECT -- extension updates placement when state changes
let device = try AccessoryControlDevice.current(for: accessory)
var config = device.configuration
config.devicePlacement = .offHead
try await device.update(config)
DON'T: Ignore connection state changes for multi-device accessories
// WRONG -- set audio source identifiers once and never update
config.primaryAudioSourceDeviceIdentifier = someAddress
try await device.update(config)
// Device disconnects, but system still thinks it's the primary source
// CORRECT -- update identifiers when connections change
func onDeviceDisconnected() {
var config = device.configuration
config.primaryAudioSourceDeviceIdentifier = nil
Task { try await device.update(config) }
}
DON'T: Forget to handle the invalidated error
// WRONG -- ignores invalidation, keeps using stale device reference
try await device.update(config) // Throws .invalidated, unhandled
// CORRECT -- catch invalidation and ask the container app to re-register
do {
try await device.update(config)
} catch AccessoryControlDevice.Error.invalidated {
await notifyContainerAppToRegisterAgain(accessory)
}
Review Checklist
- Accessory paired via AccessorySetupKit before AudioAccessoryKit registration
- Both
AccessorySetupKitandAudioAccessoryKitimported - Container app calls
register(_: _:)withAccessoryControlDevice.Configuration - App extension calls
current(for:)andupdate(_:) - Capabilities in the registration configuration match actual hardware support
- Updates only touch fields for capabilities declared during registration
-
.placementcapability accompanied by ongoing placement updates - Placement transitions (on/off head) reported promptly
- Audio source device identifiers updated on Bluetooth connection changes
- All
AccessoryControlDevice.Errorcases handled, including@unknown default -
update(_:)calls usetry awaitand handle errors - Invalidated device references trigger container-app registration recovery
- Deployment target set to iOS 26.4+ or iPadOS 26.4+
References
- Extended patterns (registration flow, placement monitoring, multi-device coordination): references/audioaccessorykit-patterns.md
- AudioAccessoryKit framework
- Supporting automatic audio switching
- AccessoryControlDevice
- AccessoryControlDevice.register(::)
- AccessoryControlDevice.current(for:)
- AccessoryControlDevice.update(_:)
- AccessoryControlDevice.Configuration
- AccessoryControlDevice.Capabilities
- AccessoryControlDevice.Placement
- AccessorySetupKit framework (prerequisite for pairing)
Related skills
More from dpearson2699/swift-ios-skills and the wider catalog.

authentication
Implement iOS authentication with Sign in with Apple, passkeys, OAuth, and biometric flows.

avkit
Create system-standard video players with AVKit for iOS—supports Picture-in-Picture, AirPlay, subtitles, and SwiftUI integration.

background-processing
Schedule and execute background work on iOS using BGTaskScheduler, background URLSession, and push notifications.

browserenginekit
Build alternative browser engines for iOS/iPadOS with process isolation, XPC communication, and system integration.

callkit
Implement native iOS VoIP calling with CallKit and PushKit integration.

carplay
Build CarPlay-enabled apps for vehicle displays using Apple's template-based UI framework.