我正在做一个可以通过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 selector:@selector(handleAudioSessionRouteChangeWithState:) name:AVAudioSessionRouteChangeNotification object:nil];当路由变更原因为AVAudioSessionRouteChangeReasonNewDeviceAvailable或AVAudioSessionRouteChangeReasonOldDeviceUnavailable时,我可以知道HFP是连接还是断开。不幸的是,当我在我的iPhone中连接一些HFP时,系统不会发布此通知,因此在这种情况下我无法检测到连接。
有没有人知道实现这一点的原因或更好的方法(检测HFP连接而不中断音乐播放)?
发布于 2016-10-11 11:54:37
你可以像这样使用!
-(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;
}发布于 2020-05-22 19:20:37
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
}https://stackoverflow.com/questions/37157252
复制相似问题