Skip to content

Commit 2a4b0c0

Browse files
authored
Merge pull request #481 from mathias5r/selected-audio-device
add selected property
2 parents 8fef467 + 3a08e04 commit 2a4b0c0

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,16 @@ public void getAudioRoutes(Promise promise){
681681
WritableArray devices = Arguments.createArray();
682682
ArrayList<String> typeChecker = new ArrayList<>();
683683
AudioDeviceInfo[] audioDeviceInfo = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS + AudioManager.GET_DEVICES_OUTPUTS);
684+
String selectedAudioRoute = getSelectedAudioRoute(audioManager);
684685
for (AudioDeviceInfo device : audioDeviceInfo){
685686
String type = getAudioRouteType(device.getType());
686687
if(type != null && !typeChecker.contains(type)) {
687688
WritableMap deviceInfo = Arguments.createMap();
688689
deviceInfo.putString("name", type);
689690
deviceInfo.putString("type", type);
691+
if(type.equals(selectedAudioRoute)) {
692+
deviceInfo.putBoolean("selected", true);
693+
}
690694
typeChecker.add(type);
691695
devices.pushMap(deviceInfo);
692696
}
@@ -714,6 +718,19 @@ private String getAudioRouteType(int type){
714718
}
715719
}
716720

721+
private String getSelectedAudioRoute(AudioManager audioManager){
722+
if(audioManager.isBluetoothScoOn()){
723+
return "Bluetooth";
724+
}
725+
if(audioManager.isSpeakerphoneOn()){
726+
return "Speaker";
727+
}
728+
if(audioManager.isWiredHeadsetOn()){
729+
return "Headset";
730+
}
731+
return "Phone";
732+
}
733+
717734
@ReactMethod
718735
public void sendDTMF(String uuid, String key) {
719736
Log.d(TAG, "[RNCallKeepModule] sendDTMF, uuid: " + uuid + ", key: " + key);

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ declare module 'react-native-callkeep' {
2121

2222
export type AudioRoute = {
2323
name: string,
24-
type: string
24+
type: string,
25+
selected?: boolean
2526
}
2627

2728
interface IOptions {

ios/RNCallKeep/RNCallKeep.m

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,14 @@ + (void)setup:(NSDictionary *)options {
542542
+ (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
543543
{
544544
NSMutableArray *newInputs = [NSMutableArray new];
545-
545+
NSString * selected = [RNCallKeep getSelectedAudioRoute];
546+
546547
NSMutableDictionary *speakerDict = [[NSMutableDictionary alloc]init];
547548
[speakerDict setObject:@"Speaker" forKey:@"name"];
548549
[speakerDict setObject:AVAudioSessionPortBuiltInSpeaker forKey:@"type"];
550+
if(selected && [selected isEqualToString:AVAudioSessionPortBuiltInSpeaker]){
551+
[speakerDict setObject:@YES forKey:@"selected"];
552+
}
549553
[newInputs addObject:speakerDict];
550554

551555
for (AVAudioSessionPortDescription* input in inputs)
@@ -556,6 +560,9 @@ + (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
556560
NSString * type = [RNCallKeep getAudioInputType: input.portType];
557561
if(type)
558562
{
563+
if([selected isEqualToString:type]){
564+
[dict setObject:@YES forKey:@"selected"];
565+
}
559566
[dict setObject:type forKey:@"type"];
560567
[newInputs addObject:dict];
561568
}
@@ -569,12 +576,18 @@ + (NSArray *) getAudioInputs
569576
NSString *str = nil;
570577

571578
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
579+
NSString *category = [myAudioSession category];
580+
NSUInteger options = [myAudioSession categoryOptions];
572581

573-
BOOL isCategorySetted = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&err];
574-
if (!isCategorySetted)
582+
583+
if(![category isEqualToString:AVAudioSessionCategoryPlayAndRecord] && (options != AVAudioSessionCategoryOptionAllowBluetooth) && (options !=AVAudioSessionCategoryOptionAllowBluetoothA2DP))
575584
{
576-
NSLog(@"[RNCallKeep][getAudioInputs] setCategory failed");
577-
[NSException raise:@"setCategory failed" format:@"error: %@", err];
585+
BOOL isCategorySetted = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&err];
586+
if (!isCategorySetted)
587+
{
588+
NSLog(@"setCategory failed");
589+
[NSException raise:@"setCategory failed" format:@"error: %@", err];
590+
}
578591
}
579592

580593
BOOL isCategoryActivated = [myAudioSession setActive:YES error:&err];
@@ -613,6 +626,21 @@ + (NSString *) getAudioInputType: (NSString *) type
613626
}
614627
}
615628

629+
+ (NSString *) getSelectedAudioRoute
630+
{
631+
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
632+
AVAudioSessionRouteDescription *currentRoute = [myAudioSession currentRoute];
633+
NSArray *selectedOutputs = currentRoute.outputs;
634+
635+
AVAudioSessionPortDescription *selectedOutput = selectedOutputs[0];
636+
637+
if(selectedOutput && [selectedOutput.portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
638+
return @"Phone";
639+
}
640+
641+
return [RNCallKeep getAudioInputType: selectedOutput.portType];
642+
}
643+
616644
- (void)requestTransaction:(CXTransaction *)transaction
617645
{
618646
#ifdef DEBUG

0 commit comments

Comments
 (0)