如何检测iOS中是否连接了HFP或A2DP?
我正在做一个项目,可以通过HFP设备播放音乐。但这里有一个问题,我想检测播放音乐时是否连接了HFP或A2DP 现在我正在使用AVFoundation框架来完成这项工作。代码如下:如何检测iOS中是否连接了HFP或A2DP?,ios,bluetooth,a2dp,hfp,Ios,Bluetooth,A2dp,Hfp,我正在做一个项目,可以通过HFP设备播放音乐。但这里有一个问题,我想检测播放音乐时是否连接了HFP或A2DP 现在我正在使用AVFoundation框架来完成这项工作。代码如下: - (BOOL)isConnectedToBluetoothPeripheral { BOOL isMatch = NO; NSString* categoryString = [AVAudioSession sharedInstance].category; AVAudioSessionCat
- (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 selector:@selector(handleAudioSessionRouteChangeWithState:) name:AVAudioSessionRouteChangeNotification object:nil];
当路由更改原因为AVAudioSessionRouteChangeReasonNewDeviceAvailable
或AvaudioSessionRouteChangeReasonOldDeviceAvailable
时,我可以知道HFP已连接或断开。不幸的是,当我在iPhone中连接一些HFP时,系统不会发布此通知,因此在这种情况下我无法检测到连接
是否有人知道实现此功能的原因或更好的方法(在不中断音乐播放的情况下检测HFP连接)?您可以这样使用
-(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;
}
Swift 5版本:
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
}