Skip to content

Instantly share code, notes, and snippets.

@savelichalex
Created July 18, 2017 11:46
Show Gist options
  • Save savelichalex/069a7585981412e5226f653c2cb9e222 to your computer and use it in GitHub Desktop.
Save savelichalex/069a7585981412e5226f653c2cb9e222 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface HeadsetDetector : RCTEventEmitter <RCTBridgeModule>
@end
#import "HeadsetDetector.h"
@implementation HeadsetDetector
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
return @[@"HeadsetStatusChange"];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioRoutingListenerCallback:)
name:AVAudioSessionRouteChangeNotification
object:nil];
- (void)audioRoutingListenerCallback:(NSNotification*)notification
{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
[self sendEventWithName:@"HeadsetStatusChange" body:@{@"isPlugged": true}];
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
[self sendEventWithName:@"HeadsetStatusChange" body:@{@"isPlugged": false}];
break;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment