I have a VoIP call application. I m trying to add functionality that when the user connected bluetooth to device and hit the bluetooth button call might be answered. I added options that when incoming call received incoming ring should play in bluetooth also.
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options:[.defaultToSpeaker,.allowBluetooth,.allowBluetoothA2DP])
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
and I added MPCommandCenter to listen bluetooth events ;
let rcCenter = MPRemoteCommandCenter.shared()
rcCenter.nextTrackCommand.isEnabled = false
rcCenter.nextTrackCommand.addTarget { _ in return .success }
rcCenter.previousTrackCommand.isEnabled = false
rcCenter.previousTrackCommand.addTarget { _ in return .success }
rcCenter.togglePlayPauseCommand.isEnabled = true
rcCenter.playCommand.isEnabled = true
rcCenter.pauseCommand.isEnabled = true
rcCenter.stopCommand.isEnabled = true
rcCenter.togglePlayPauseCommand.addTarget{ [unowned self] event in
print("togglePlayPauseCommand")
return.commandFailed
}
rcCenter.playCommand.addTarget{ [unowned self] event in
print("playCommand")
return.commandFailed
}
rcCenter.pauseCommand.addTarget{ [unowned self] event in
print("pause")
return.commandFailed
}
rcCenter.stopCommand.addTarget{ [unowned self] event in
print("stop")
return.commandFailed
}
When I remove bluetooth related options from audioSession.setCategory I can listen to events. But when I put them again, events don't work. I also tried UIResponder but had no success;
UIApplication.shared.becomeFirstResponder()
UIApplication.shared.beginReceivingRemoteControlEvents()
and
override func remoteControlReceived(with event: UIEvent?) {
if let rc = event?.subtype{
print("OVER HERE")
}
}
any idea will be appreciated.