How to determine if a Bluetooth headset is connected or not iOS 8?

In my project, I use AVAudioSession to detect that any headphones are plugged in or unplugged. But in this case, I cannot detect when the bluetooth device is connected. Here is my headphone status code.

  - (void)audioRouteChangeListenerCallback:(NSNotification*)notification { NSDictionary *interuptionDict = notification.userInfo; NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; switch (routeChangeReason) { case AVAudioSessionRouteChangeReasonNewDeviceAvailable: //NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable"); NSLog(@"Headphone/Line plugged in"); [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-on.png"] forState:UIControlStateNormal]; _headSetState=YES; break; case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable"); NSLog(@"Headphone/Line was pulled. Stopping player...."); [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-off.png"] forState:UIControlStateNormal]; if(_isPlaying==YES) { [self.player pause]; [_audioButtonOutlet setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; _isPlaying=NO; } _headSetState=NO; break; case AVAudioSessionRouteChangeReasonCategoryChange: // called at start - also when other audio wants to play NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange"); break; } - (BOOL)isHeadsetPluggedIn { AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute]; for (AVAudioSessionPortDescription* desc in [route outputs]) { if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones]) { [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-on.png"] forState:UIControlStateNormal]; _headSetState=YES; return YES; } else { [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-off.png"] forState:UIControlStateNormal]; _headSetState=NO; return NO; } } return NO; } } - viewWillAppear { [AVAudioSession sharedInstance]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:) name:AVAudioSessionRouteChangeNotification object:nil]; [self isHeadsetPluggedIn]; } 

So, how can I determine if a Bluetooth headset is connected or not iOS 8?

+3
ios8 bluetooth headset audiosession
Mar 08 '15 at 16:27
source share
3 answers

I was able to determine if a Bluetooth headset (HFP) device is connected using the following:

 NSArray *arrayInputs = [[AVAudioSession sharedInstance] availableInputs]; for (AVAudioSessionPortDescription *port in arrayInputs) { if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) { bHas = YES; break; } } 

However, for this to work, your AVAudioSession category must be set to AVAudioSessionCategoryPlayAndRecord. If this is not the case, the port will not appear in the list, even if an HFP device is connected.

+2
Mar 28 '15 at 19:25
source share

You can detect the currently active Bluetooth output devices (instead of input devices)

Swift Code:

 import AVFoundation func bluetoothAudioConnected() -> Bool{ let outputs = AVAudioSession.sharedInstance().currentRoute.outputs for output in outputs{ if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP || output.portType == AVAudioSessionPortBluetoothLE{ return true } } return false } 

Bluetooth devices are based on the following question: What is the difference between AVAudioSessionPortBluetoothHFP, A2DP and LE?

I hope this helps someone

+12
Feb 17 '16 at 11:49
source share

I am solving a problem with this answer.

stack overflow

0
May 17 '17 at 13:30
source share



All Articles