I am trying to check if AirPods are connected to the iPhone. How can I check it programmatically?
For airpods port.portType value is .builtInMic which is not sufficient to check if airpods are connected to iphone
class func isMicAvailbale() -> Bool{
let availableInputs:[AVAudioSessionPortDescription] = AVAudioSession.sharedInstance().availableInputs ?? []
var micPresent = false;
for port in availableInputs
{
if port.portType == .builtInMic{
micPresent = true
}
}
return micPresent
}
One way that comes to my mind is that you could use
Core Bluetooth
API to access the airpods via Bluetooth. But this might be overkill when you can use AVSession. I don't know why exactly you want to detect just airpods and no other bluetooth headphones. But I thinkbuildInMic
stands for buildIn microphone inside the device and not the bluetooth device :P If you take a look into docs you can see it :PYou didn't ask for other bluetooth headset, but as part of the answer I will provide you this code, this should work for non MFI headsets connected to iPhone via Bluetooth.
Now to the Airpod part.You probably want to use
ExternalAccessory.framework
to communicate with the MFI bluetooth devices such as Airpods.~~I haven't been working with
EAAccessory
yet but I believe you have to do something like this:EAAccessoryManager
Also very important step is to add
UISupportedExternalAccessoryProtocols
to your info.plist fileI am bit tired so if you have any questions ask, tommorow I will write the implementation in here if no-one will be faster.
Okay so obviously my answer was totally wrong on the first place.
I have learned today that Airpods aren't listed in apple's MFI devices so the ExternalAccessorymanager won't obviously work. As stated in the answer mentioned in the footer, all you need to do is to add category to the AVSession.
So the whole code is basically in here :D
prove:
Sorry for misunderstanding and writing totally offtopic answer. But hey, at least you know something about external accessories :)
Also you might want to take a look in here