AVAudioSession does not automatically connect to Bluetooth A2DP audio? - ios

Description:
When I change my AudioSession to .playAndRecord via the following call:
AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothA2DP, .mixWithOthers])
From these category options set, I expect that, once I pair bluetooth headphones with my phone, my running app should automatically route audio to my bluetooth A2DP headphones. Instead, my app still chooses to use the phone speaker. There is no indication at all, whether from querying the audio session instance or in the form of a route change notification, that I have bluetooth headphones connected..
Question: How do I ensure the audiosession connects automatically to the bluetooth headphones?
This behavior clearly has nothing to do with an incorrect line of code. As if I had bluetooth headphones paired before the I invoke audio-session category change, my audio will correctly continue to use the bluetooth headphones.
Moreover, if I had instead used the .allowBluetooth category option (for HFP instead of A2DP) during the code invocation, and then paired bluetooth headphones, the headphones connect automatically just fine. Behavior reproduced on: iPhone XR (iOS 13.6.1) with Apple AirPods Pro
iPhone XR (iOS 13.6) with Senso wireless 44.1kHz bluetooth headphones.
Reproduction steps:
For the sake of simplicity and reproducibility for Apple’s engineers, I have reproduced the issue on a lightly modified version of AVEchoTouch Demo Code from WWDC. As this was a demo project published by Apple, it is all correct code. My modified project, called BluetoothIssue, can be found here.
Step 1: Make sure your bluetooth headphones are currently not paired with the iPhone.
Step 2: Launch the “BluetoothIssue App”. When you toggle the “FX Out” or the “Speech Out” toggles, the app should be playing sounds (out of iPhone speaker)
Step 3: Now connect your bluetooth headphones to the phone. Even though the headphones are now connected and paired with phone, the app does not register the connection, and continues play out of the phones built-in speakers.
// Steps 4-5 (Optional): to see that nothing is wrong with the app’s category itself
Step 4: Now force kill the “BluetoothIssue” app, while your headphones are still connected.
Step 5: Now relaunch the “BluetoothIssue” app. The app plays sound correctly out to the bluetooth headphones.

I learned that if i set the mode to VideoRecording, iOS will change the route if I turn the bluetooth on (and I get a route change notification). Without this mode I had to allow both bluetooth, after detect one, change the category again without AllowBluetooth, just A2DP. Weird and slow solution, but worked. Now im just using videorecording mode.

Related

iOS Simulator makes headphones enter call mode

Every time I open iOS Simulator my headphones enter call mode. Why MacOS thinks an open Simulator is like a running call?
Headphones are connected wirelessly (Beoplay H9i).
Under Hardware, Audio Input select a different input device (such as built-in microphone).

Monitor volume level when using headphones

I'm looking to create an iphone app to help people monitor whether they are experiencing hearing loss from turning up the sound too high on their earbuds for a research project.
To do this, I want to periodically poll the volume level when music is playing on the iphone and the headphones are plugged in.
I did find the following answers that indicate it is possible however I am not familiar with iphone development.
checking when the audio is playing on the phone
Can my app be notified when another application starts/stops playing audio?
check if headphones are plugged in
Are headphones plugged in? iOS7
checking the volume level on the phone
https://stackoverflow.com/a/29170927/1361960
In Apple-way you cannot make app be notified when another application starts/stops playing audio.
Since the background execution of your app was limited according to the document.
Your code will only be executed when your application was running.

Connecting multiple wireless devices to one main device using bluetooth

What I want to do is connect to bluetooth enabled devices to one main device. For example two bluetooth speakers to connect to my iPhone and play music.
Do speaker A needs to connect to Speaker B through bluetooth and then connect to the iPhone? IS this even possible?
iPhones music app can only output to one bluetooth speaker at a time.
Either the software would need to support multiple outputs or the speakers would need to have a feature to pair with each other.
So it is technically possible, but not supported by iOS' music app, nor do I personally know of a speaker that does that.

BlueTooth headset not able to use as audio recording capture for iOS and OpenEars

We are building an iOS app that does basic speech recognition. Basically, the app counts the number of words you speak into the iOS device. The app works well when speaking into the standard microphone built into the iPhone. However, when connecting a wireless Bluetooth audio device, we are unable to use that Bluetooth device as a method for recording voice audio. We are using following software and devices:
built for iOS7.0/7.1 with the OpeanEars library for speech recognition
we’re using the ZOMM Wireless Leash (http://www.zomm.com/ | http://www.amazon.com/ZOMM-Wireless-Bluetooth-Speakerphone-Black/dp/B003N3J6BU/ref=sr_1_1?ie=UTF8&qid=1409515088&sr=8-1)
Tried other Bluetooth devices with the same behavior (Bluedio 66i and Bluedio DF200)
Unable to capture audio in default Voice Memo app
as far as we know this is simply using standard BlueTooth protocol, as we understand it, once the bluetooth device is paired it should automatically start accepting it as a device for recording/audio capture
According to OpenEars, the Bluetooth audio devices should be picked up automatically (http://www.politepix.com/forums/topic/enabling-bluetooth-support/). Are we right in assuming this?
We used the VoiceMemo app (the voice recording app that ships with iOS) to test out the bluetooth device as a “control” experiment:
Pair the ZOMM with the iOS device
Open VoiceMemo
Select ZOMM as input device from within the VoiceMemo app
Start recording
Stop recording – no audio was captured
Unfortunately this meant that neither our app nor the standard voice recording app is able to use the bluetooth device as a means for recording audio. Either way it’s hard to rule the device as simply the issue.
We’re curious to understand if this simply a hardware issue (and need a BT-enabled device that supports voice recording to iOS) or if there is something in the code we need to enable in order for the app to start accepting the device as recordable.
Also, more details about the ZOMM headset:
ZOMM specifications:
Bluetooth Wireless Compatibility:
This ZOMM device supports the following Bluetooth wireless protocols and profiles:
• Bluetooth core technology v2.1+EDR
• Hands-Free Profile (HFP) v1.5 headset role
• Headset Profile (HSP) v1.2 headset role
Bluetooth Wireless Interoperability:
This ZOMM device is designed to be interoperate with all Bluetooth wireless products that support compatible profiles and roles
including:
Bluetooth core technology v3.0, v2.1+EDR, v2.0 +EDR, v1.2
Bluetooth master and slave roles
Bluetooth Hands-Free Profile (HFP) v1.5 and prior headset (HS) role
Bluetooth Headset Profile (HS) v1.2 and prior headset (HS) role
Any idea on what we could do to resolve this issue and use Bluetooth together with the OpenEars library on iOS7.1?
Thanks! Philip
testing with Voice Memo is logical, however the app may not be allowing the route the change when BT is connected. A detailed explanation is here. iOS: Using Bluetooth audio output (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput) AudioSession
With OpenEars, I believe you can enable logging to track when the audio route changes. So, you can verify via logging that it is listening to where it should be or .... not listening. I would suggest upgrading as of this post http://www.politepix.com/forums/topic/small-bug-when-running-on-ios-8/ . If you scroll to the bottom you can see BT should work now.
Another test worth running is SaveThatWav within OE. I have not used it, but you should be able to verify what you are listening too.

iOS: can I pair more than one bluetooth audio device at once?

I would like to write an iOS app to simultaneously record audio from several bluetooth headset microphones. Is this even possible given the api of iOS?
The bluetooth specification allows up to seven devices to connect, but it's not straightforward, as it is dependent on the type of device and the service profile it provides. For example, you can have a bluetooth keyboard, mouse, and headset connected at the same time, and an A2DP (Advanced Audio Distribution Profile) device to stream music, but you can't have two devices that performing the same service profile connected at the same time. check the link below for more info.
http://en.wikipedia.org/wiki/Bluetooth

Resources