OGeek|极客世界-中国程序员成长平台

标题: ios - 如何检测 iOS 中是否连接了 HFP 或 A2DP? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:52
标题: ios - 如何检测 iOS 中是否连接了 HFP 或 A2DP?

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

现在我正在使用 AVFoundation 框架来执行此操作。代码如下:

- (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连接效果的方法。我正在使用标志

static BOOL isHFPConnectedFlag

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

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(handleAudioSessionRouteChangeWithState name:AVAudioSessionRouteChangeNotification object:nil];

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

有谁知道实现这一点的原因或更好的方法(在不中断音乐播放的情况下检测 HFP 连接)?



Best Answer-推荐答案


你可以这样使用!

    -(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 - 如何检测 iOS 中是否连接了 HFP 或 A2DP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37157252/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4