首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测iOS中是否连接了HFP或A2DP?

如何检测iOS中是否连接了HFP或A2DP?
EN

Stack Overflow用户
提问于 2016-05-11 16:39:10
回答 2查看 1.7K关注 0票数 3

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

现在我使用AVFoundation框架来做这件事。代码如下:

代码语言:javascript
复制
- (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连接,会导致音乐播放中断两秒左右。

因此,我尝试了另一种方法,可以减少检测HFP连接的影响。我正在使用一面旗帜

代码语言:javascript
复制
static BOOL isHFPConnectedFlag

以指示是否已连接HFP或A2DP。我使用前面的方法只检测一次连接(当应用程序启动时),并将结果保存到isHFPConnectedFlag中。更重要的是,我观察AudioSessionRouteChange来同步连接状态:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionRouteChangeWithState:) name:AVAudioSessionRouteChangeNotification object:nil];

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

有没有人知道实现这一点的原因或更好的方法(检测HFP连接而不中断音乐播放)?

EN

回答 2

Stack Overflow用户

发布于 2016-10-11 11:54:37

你可以像这样使用!

代码语言:javascript
复制
    -(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;
}
票数 4
EN

Stack Overflow用户

发布于 2020-05-22 19:20:37

Swift 5版本:

代码语言:javascript
复制
func bluetoothDeviceHFPAvailable() -> Bool {
    let audioSession = AVAudioSession.sharedInstance()
    let currentRoute = audioSession.currentRoute

    for output in currentRoute.outputs {
        if output.portType == .bluetoothHFP || output.portType == .bluetoothA2DP {
            return true
        }
    }

    return false
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37157252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档