3434static NSString *const RNCallKeepDidToggleHoldAction = @" RNCallKeepDidToggleHoldAction" ;
3535static NSString *const RNCallKeepProviderReset = @" RNCallKeepProviderReset" ;
3636static NSString *const RNCallKeepCheckReachability = @" RNCallKeepCheckReachability" ;
37+ static NSString *const RNCallKeepDidChangeAudioRoute = @" RNCallKeepDidChangeAudioRoute" ;
3738static NSString *const RNCallKeepDidLoadWithEvents = @" RNCallKeepDidLoadWithEvents" ;
3839
3940@implementation RNCallKeep
@@ -42,7 +43,7 @@ @implementation RNCallKeep
4243 BOOL _isStartCallActionEventListenerAdded;
4344 bool _hasListeners;
4445 NSMutableArray *_delayedEvents;
45- }
46+ }
4647
4748static bool isSetupNatively;
4849static CXProvider * sharedProvider;
@@ -58,6 +59,11 @@ - (instancetype)init
5859 if (self = [super init ]) {
5960 _isStartCallActionEventListenerAdded = NO ;
6061 if (_delayedEvents == nil ) _delayedEvents = [NSMutableArray array ];
62+
63+ [[NSNotificationCenter defaultCenter ] addObserver: self
64+ selector: @selector (onAudioRouteChange: )
65+ name: AVAudioSessionRouteChangeNotification
66+ object: nil ];
6167 }
6268 return self;
6369}
@@ -116,6 +122,18 @@ - (void)stopObserving
116122 _hasListeners = FALSE ;
117123}
118124
125+ - (void )onAudioRouteChange : (NSNotification *)notification
126+ {
127+ NSDictionary *info = notification.userInfo ;
128+ NSInteger reason = [[info valueForKey: AVAudioSessionRouteChangeReasonKey] integerValue ];
129+ NSString *output = [AVAudioSession sharedInstance ].currentRoute .outputs .count > 0 ? [AVAudioSession sharedInstance ].currentRoute .outputs [0 ].portType : nil ;
130+
131+ [self sendEventWithName: RNCallKeepDidChangeAudioRoute body: @{
132+ @" output" : output,
133+ @" reason" : @(reason),
134+ }];
135+ }
136+
119137- (void )sendEventWithNameWrapper : (NSString *)name body : (id )body {
120138 if (_hasListeners) {
121139 [self sendEventWithName: name body: body];
@@ -398,8 +416,8 @@ + (void)setup:(NSDictionary *)options {
398416 resolve ([RNCallKeep getCalls ]);
399417}
400418
401- RCT_EXPORT_METHOD (setAudioRoute: (NSString *)uuid
402- inputName:(NSString *)inputName
419+ RCT_EXPORT_METHOD (setAudioRoute: (NSString *)uuid
420+ inputName:(NSString *)inputName
403421 resolver:(RCTPromiseResolveBlock)resolve
404422 rejecter:(RCTPromiseRejectBlock)reject)
405423{
@@ -417,7 +435,7 @@ + (void)setup:(NSDictionary *)options {
417435 resolve (@" Speaker" );
418436 return ;
419437 }
420-
438+
421439 NSArray *ports = [RNCallKeep getAudioInputs ];
422440 for (AVAudioSessionPortDescription *port in ports) {
423441 if ([port.portName isEqualToString: inputName]) {
@@ -456,12 +474,12 @@ + (void)setup:(NSDictionary *)options {
456474+ (NSMutableArray *) formatAudioInputs : (NSMutableArray *)inputs
457475{
458476 NSMutableArray *newInputs = [NSMutableArray new ];
459-
477+
460478 NSMutableDictionary *speakerDict = [[NSMutableDictionary alloc ]init];
461479 [speakerDict setObject: @" Speaker" forKey: @" name" ];
462480 [speakerDict setObject: AVAudioSessionPortBuiltInSpeaker forKey: @" type" ];
463481 [newInputs addObject: speakerDict];
464-
482+
465483 for (AVAudioSessionPortDescription* input in inputs)
466484 {
467485 NSString *str = [NSString stringWithFormat: @" PORTS :\" %@ \" : UID:%@ " , input.portName, input.UID ];
0 commit comments