0

I have a project where I am trying to get my bluetooth headphones to work. In my view controller I add routeChangeNotification to see what is going on and it fires twice (both with case .categoryChange. The first time it shows my bluetooth headphones and then the second time it fires it reverts back to device microphone.

In AppDelegate I have:

        let session = AVAudioSession.sharedInstance()
    do {
        // https://stackoverflow.com/questions/51010390/avaudiosession-setcategory-swift-4-2-ios-12-play-sound-on-silent
        if #available(iOS 10.0, *) {
            try session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
        } else {
            session.perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playAndRecord, with: [
                AVAudioSession.CategoryOptions.allowBluetooth,
                AVAudioSession.CategoryOptions.defaultToSpeaker]
            )
            try session.setMode(.default)
        }
        try session.setActive(true)
    } catch {
        print(error)
    }

In my ViewController I have:

@objc private func didRouteChangeNotification(_ notification: Notification) {
    guard let userInfo = notification.userInfo,
    let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {
        return
    }
    switch reason {
    case .newDeviceAvailable:
        debugPrint("newDeviceAvailable")
    case .oldDeviceUnavailable:
        debugPrint("oldDeviceUnavailable")
    case .categoryChange:
        debugPrint("catgoryChange")
    default: ()
    }
}
Mike Simz
  • 3,976
  • 4
  • 26
  • 43
  • What is your question? The text of your question doesn't match your subject line. Do you believe there's a disconnect somewhere? What do you expect to happen? Is there any other code in your app that calls `setCategory`? Are you really supporting iOS 9 as this code suggests? (If so, what iOS version are you testing this on?) What kind of bluetooth headphone are you connecting? Does it support HFP? Does it have a microphone? When do you start observing the route change? Does the first notification come much earlier than your `setActive`, or do they come in rapid succession? – Rob Napier Oct 15 '21 at 17:17
  • @RobNapier testing on iOS 15 using iPhone X. The problem is that i am unable to get my bluetooth earphones connected as input device. It keeps going to built-in device mic – Mike Simz Oct 15 '21 at 17:22
  • Also, what do you expect `[.defaultToSpeaker, .allowBluetooth]` to do? Do you want to use the built-in microphone or the headset microphone? – Rob Napier Oct 15 '21 at 17:22
  • Do your bluetooth earphones support HFP (that's what you're asking for here)? What's the `.defaultToSpeaker` for? (Also, to the original question, do you actually mean to support iOS 9 as your code suggests?) – Rob Napier Oct 15 '21 at 17:24
  • @RobNapier i have removed the iOS 9 bit of code as that is not required (supporting ios 12 and later). I want the ability to be able to use built-in mic OR bluetooth earphones if they are connected. I am not sure if they support HFP? I can use them for FaceTime as microphone. This is the product here: https://www.amazon.ca/gp/product/B0721MP41Q/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 – Mike Simz Oct 15 '21 at 17:28
  • @RobNapier i start observing routeChange in viewWillAppear of a view controller deep into the app so these observer calls fire much later than AppDelegate setCategory call – Mike Simz Oct 15 '21 at 17:31
  • Those definitely are going to support HFP. I would put breakpoints on `didRouteChangeNotification` and explore what's leading to the changes. Unless you really mean `.defaultToSpeaker`, I'd get rid of that. Don't just throw random options into AVAudioSession. You absolutely must study the docs to know what each option means. They're not obvious. Never copy AVAudioSession code from SO without researching each line. But start with breakpoints and explore where you're getting called from and everything that is being passed to you. The information is in there. – Rob Napier Oct 15 '21 at 17:35
  • @RobNapier dumb question but when reading userInfo in `didRouteChangeNotification` am i looking for my bluetooth headphones to be input or output for them to be used as mic? – Mike Simz Oct 15 '21 at 18:14
  • The microphone will be the input. It'll be listed as a AVAudioSessionPortDescription. (And not dumb at all; audio interfaces are very confusing, and occasionally they're very non-intuitive.) – Rob Napier Oct 15 '21 at 18:22

0 Answers0