Skip to content

Commit d2b100a

Browse files
committed
ios: add WiredHeadset event
1 parent 9fb0520 commit d2b100a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

ios/RNInCallManager/RNInCallManager.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
170170
}
171171
}
172172

173-
let isCurrentRouteToSpeaker: Bool = self.checkAudioRoute([AVAudioSessionPortBuiltInSpeaker])
173+
let isCurrentRouteToSpeaker: Bool = self.checkAudioRoute([AVAudioSessionPortBuiltInSpeaker], "output")
174174
if (overrideAudioPort == .Speaker && !isCurrentRouteToSpeaker) || (overrideAudioPort == .None && isCurrentRouteToSpeaker) {
175175
do {
176176
try self.audioSession.overrideOutputAudioPort(overrideAudioPort)
@@ -191,9 +191,10 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
191191
//self.debugAudioSession()
192192
}
193193

194-
func checkAudioRoute(targetPortTypeArray: [String]) -> Bool {
194+
func checkAudioRoute(targetPortTypeArray: [String], _ routeType: String) -> Bool {
195195
if let currentRoute: AVAudioSessionRouteDescription = self.audioSession.currentRoute {
196-
for _portDescription in currentRoute.outputs {
196+
let routes: [AVAudioSessionPortDescription] = (routeType == "input" ? currentRoute.inputs : currentRoute.outputs)
197+
for _portDescription in routes {
197198
let portDescription: AVAudioSessionPortDescription = _portDescription as AVAudioSessionPortDescription
198199
if targetPortTypeArray.contains(portDescription.portType) {
199200
return true
@@ -203,8 +204,9 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
203204
return false
204205
}
205206

206-
func isHeadsetPluggedIn() -> Bool {
207-
return self.checkAudioRoute([AVAudioSessionPortHeadphones, AVAudioSessionPortHeadsetMic])
207+
func isWiredHeadsetPluggedIn() -> Bool {
208+
// --- only check for a audio device plugged into headset port instead bluetooth/usb/hdmi
209+
return self.checkAudioRoute([AVAudioSessionPortHeadphones], "output") || self.checkAudioRoute([AVAudioSessionPortHeadsetMic], "input")
208210
}
209211

210212
func audioSessionSetCategory(audioCategory: String, _ options: AVAudioSessionCategoryOptions?, _ callerMemo: String) -> Void {
@@ -376,8 +378,16 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
376378
NSLog("RNInCallManager.AudioRouteChange.Reason: Unknown")
377379
case .NewDeviceAvailable:
378380
NSLog("RNInCallManager.AudioRouteChange.Reason: NewDeviceAvailable")
381+
if self.checkAudioRoute([AVAudioSessionPortHeadsetMic], "input") {
382+
self.bridge.eventDispatcher.sendDeviceEventWithName("WiredHeadset", body: ["isPlugged": true, "hasMic": true, "deviceName": AVAudioSessionPortHeadsetMic])
383+
} else if self.checkAudioRoute([AVAudioSessionPortHeadphones], "output") {
384+
self.bridge.eventDispatcher.sendDeviceEventWithName("WiredHeadset", body: ["isPlugged": true, "hasMic": false, "deviceName": AVAudioSessionPortHeadphones])
385+
}
379386
case .OldDeviceUnavailable:
380387
NSLog("RNInCallManager.AudioRouteChange.Reason: OldDeviceUnavailable")
388+
if !self.isWiredHeadsetPluggedIn() {
389+
self.bridge.eventDispatcher.sendDeviceEventWithName("WiredHeadset", body: ["isPlugged": false, "hasMic": false, "deviceName": ""])
390+
}
381391
case .CategoryChange:
382392
NSLog("RNInCallManager.AudioRouteChange.Reason: CategoryChange. category=\(self.audioSession.category) mode=\(self.audioSession.mode)")
383393
self.updateAudioRoute()

0 commit comments

Comments
 (0)