问题
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
}
回答1:
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 think buildInMic
stands for buildIn microphone inside the device and not the bluetooth device :P If you take a look into docs you can see it :P
You 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:
- Create instance of
EAAccessoryManager
- Use that instance to get the connected devices
- Find airpods via some ID
- Figure out how to check if the accessory is connected, but this should be piece of cake.
Also very important step is to add UISupportedExternalAccessoryProtocols
to your info.plist file
I 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
let session = AVAudioSession.sharedInstance()
try! session.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth)
guard let availableInputs = session.availableInputs else { return }
for input in availableInputs {
if input.portType == .bluetoothHFP {
// Do your stuff...
}
}
prove:
2019-01-04 02:32:13.462093+0100 Accessory games[24578:5411208] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Butcher’s AirPods (type: BluetoothHFP)
(lldb) po availableInputs
▿ 2 elements
- 0 : <AVAudioSessionPortDescription: 0x283b401b0, type = MicrophoneBuiltIn; name = iPhone Mikrofon; UID = Built-In Microphone; selectedDataSource = Vpředu>
- 1 : <AVAudioSessionPortDescription: 0x283b40250, type = BluetoothHFP; name = Butcher’s AirPods; UID = 10:94:BB:5D:5F:F7-tsco; selectedDataSource = (null)>
(lldb) po availableInputs[1].portName
"Butcher’s AirPods"
(lldb) po availableInputs[1].portType
▿ AVAudioSessionPort
- _rawValue : BluetoothHFP
(lldb)
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
来源:https://stackoverflow.com/questions/54014593/how-to-check-if-airpods-are-connected-to-iphone