如何检测iOS中是否连接了HFP或A2DP? [英] How can I detect whether an HFP or A2DP is connected in iOS?

查看:1185
本文介绍了如何检测iOS中是否连接了HFP或A2DP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个可以通过HFP设备播放音乐的项目.但是,这是一个我想在播放音乐时检测是否连接了HFP或A2DP的问题.

I am working on a project which can play music via HFP device. But here's a problem that I want to detect whether an HFP or A2DP is connected when music is playing.

现在,我正在使用AVFoundation框架来执行此操作.这是代码:

Now I am using the AVFoundation framework to do this. Here's the code:

- (BOOL)isConnectedToBluetoothPeripheral
{
    BOOL isMatch = NO;
    NSString* categoryString = [AVAudioSession sharedInstance].category;
    AVAudioSessionCategoryOptions categoryOptions = [AVAudioSession sharedInstance].categoryOptions;
    if ((![categoryString isEqualToString:AVAudioSessionCategoryPlayAndRecord] &&
         ![categoryString isEqualToString:AVAudioSessionCategoryRecord]) ||
        categoryOptions != AVAudioSessionCategoryOptionAllowBluetooth)
    {
        NSError * error = nil;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                         withOptions:AVAudioSessionCategoryOptionAllowBluetooth
                                               error:&error];
        if (error) {
            [[AVAudioSession sharedInstance] setCategory:categoryString
                                             withOptions:categoryOptions
                                                   error:&error];
            return isMatch;
        }
    }
    
        NSArray * availableInputs = [AVAudioSession sharedInstance].availableInputs;
        for (AVAudioSessionPortDescription *desc in availableInputs)
        {
            if ([[desc portType] isEqualToString:AVAudioSessionPortBluetoothA2DP] || [[desc portType] isEqualToString:AVAudioSessionPortBluetoothHFP])
            {
                    isMatch = YES;
                    break;
            }
        }
        if (!isMatch)
        {
            NSArray * outputs = [[[AVAudioSession sharedInstance] currentRoute] outputs];
            
            for (AVAudioSessionPortDescription * desc in outputs)
            {
                if ([[desc portType] isEqualToString:AVAudioSessionPortBluetoothA2DP] || [[desc portType] isEqualToString:AVAudioSessionPortBluetoothHFP])
                {
                        isMatch = YES;
                        break;
                }
            }
        }
        
        NSError * error = nil;
        [[AVAudioSession sharedInstance] setCategory:categoryString
                                         withOptions:categoryOptions
                                               error:&error];
    
        return isMatch;
}

它运作良好,但会引起另一个问题:在播放音乐时,使用此方法检测HFP连接将使音乐播放中断大约两秒钟.

It works well but cause another problem: when music is playing, using this method to detect HFP connection will make music playing interrupt for about two seconds.

因此,我尝试了另一种方法来降低检测HFP连接的影响.我正在使用一个标志

So I tried another way which can reduce the effect of detecting HFP connecting. I am using a flag

static BOOL isHFPConnectedFlag

指示是否连接了HFP或A2DP.我使用以前的方法仅检测一次连接(在应用程序启动时),并将结果保存到isHFPConnectedFlag中.此外,我观察到AudioSessionRouteChange可以同步连接状态:

To indicate whether HFP or A2DP is connected. I use previous method to detect the connection only once (when the app is launching) and save the result into isHFPConnectedFlag. What's more, I observe the AudioSessionRouteChange to sync the connection status:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionRouteChangeWithState:) name:AVAudioSessionRouteChangeNotification object:nil];

当路由更改原因是AVAudioSessionRouteChangeReasonNewDeviceAvailableAVAudioSessionRouteChangeReasonOldDeviceUnavailable时,我可以知道HFP已连接或断开.不幸的是,当我在iPhone中连接某些HFP时,系统将不会发布此通知,因此在这种情况下我无法检测到连接.

When the route change reason is AVAudioSessionRouteChangeReasonNewDeviceAvailable or AVAudioSessionRouteChangeReasonOldDeviceUnavailable I can know HFP is connected or disconnected. Unfortunately, when I connect some HFP in my iPhone, the system will not post this notification, so I cannot detect the connection in this situation.

有人知道实现此目的的原因或更好的方法(检测HFP连接而不会中断音乐播放)吗?

Does anyone know the reason or a better way to implements this (Detecting HFP connection without music playing interrupting)?

推荐答案

您可以像这样使用!

    -(BOOL) bluetoothDeviceA2DPAvailable {

    BOOL available = NO;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    AVAudioSessionRouteDescription *currentRoute = [audioSession currentRoute];

    for (AVAudioSessionPortDescription *output in currentRoute.outputs) {
        if (([[output portType] isEqualToString:AVAudioSessionPortBluetoothA2DP] ||
             [[output portType] isEqualToString:AVAudioSessionPortBluetoothHFP])) {
            available = YES;
            break;
        }
    }
    return available;
}

这篇关于如何检测iOS中是否连接了HFP或A2DP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆