在iOS中启用蓝牙以进行PJSIP语音通话

时间:2018-07-18 07:40:35

标签: ios objective-c pjsip ios-bluetooth

我正在制作VOIP应用程序,现在我们需要使用蓝牙连接语音电话。 我们尝试将通话与蓝牙连接,但听不到任何声音。

enter image description here

   /* Initialize audio session category and mode */
{
    AVAudioSession *sess = [AVAudioSession sharedInstance];
    pj_bool_t err;



if ([sess respondsToSelector:@selector(setCategory:withOptions:error:)])
{
    err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord
            withOptions:AVAudioSessionCategoryOptionAllowBluetooth
            error:nil] != YES;
    } else {
        err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord
            error:nil] != YES;
    }
if (err) {
        PJ_LOG(3, (THIS_FILE,
               "Warning: failed settting audio session category"));
}

if ([sess respondsToSelector:@selector(setMode:error:)] &&
    [sess setMode:AVAudioSessionModeVoiceChat error:nil] != YES)
{
    PJ_LOG(3, (THIS_FILE, "Warning: failed settting audio mode"));
}
}

pjmedia-audiodev-> coreaudio_dev.m 文件中添加了以上代码。

还尝试了以下代码:-

+(void)EnableBluethooth
  {
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
   [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord 
  error: 
   nil];
  [audioSession setActive: YES error: nil];
  UInt32 allowBluetoothInput = 1;
  OSStatus ostatus = AudioSessionSetProperty (

   kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,


   sizeof(allowBluetoothInput), &allowBluetoothInput);

      pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH;
  pj_status_t status = 
   pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, &route, 
   PJ_TRUE);
   NSLog(@"statuys is--->%d",status);


     NSLog(@"status = %x", ostatus);



    }
   +(void)DisableBluethooth
  {
      AVAudioSession* audioSession = [AVAudioSession sharedInstance];
      [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord 
       error: nil];
       [audioSession setActive: NO error: nil];
     pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH;
       pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, &route, 
     PJ_FALSE);


   }

代码已执行,但无法使用蓝牙听到声音。 如果有人想为 pjsip 启用蓝牙,请给我。

谢谢。

1 个答案:

答案 0 :(得分:0)

设置 AVAudioSessionCategory 端口后,将preferredInput设置为蓝牙端口,以便激活蓝牙。请遵循以下代码并调用switchBluetooth方法。

- (AVAudioSessionPortDescription*)builtinAudioDevice
{
    NSArray* builtinRoutes = @[AVAudioSessionPortBuiltInMic];
    return [self audioDeviceFromTypes:builtinRoutes];
}        
- (AVAudioSessionPortDescription*)bluetoothAudioDevice
    {
        NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP];
        return [self audioDeviceFromTypes:bluetoothRoutes];
    }

    - (AVAudioSessionPortDescription*)audioDeviceFromTypes:(NSArray*)types
    {
        NSArray* routes = [[AVAudioSession sharedInstance] availableInputs];
        for (AVAudioSessionPortDescription* route in routes)
        {
            if ([types containsObject:route.portType])
            {
                return route;
            }
        }
        return nil;
    }

    - (BOOL)switchBluetooth:(BOOL)onOrOff
    {
        NSError* audioError = nil;
        BOOL changeResult = NO;
        if (onOrOff == YES)
        {
            AVAudioSessionPortDescription *bluetoothPort = [self bluetoothAudioDevice];
            if (bluetoothPort) {
                changeResult = [[AVAudioSession sharedInstance] setPreferredInput:bluetoothPort
                                                                            error:&audioError];
            }
        }
        else
        {
            AVAudioSessionPortDescription* builtinPort = [self builtinAudioDevice];
            if (builtinPort) {
                changeResult = [[AVAudioSession sharedInstance] setPreferredInput:builtinPort
                                                                            error:&audioError];
            }
        }
        if (audioError) {
            NSLog(@"BluetoothActiveError: %@ suggestion : %@",audioError.localizedDescription,audioError.localizedRecoverySuggestion);
        }
        return changeResult;
    }