一.蓝牙音频协议常见的分为两种
1.单向高保真类型音频协议(即:输出),其常见类型为 BluetoothA2DPOutput,听歌看影音为主;
2.双向低保真类型音频协议(即:输出、输入),其常见类型为 BluetoothHFP (即:Hands-Free Profile) 和 BluetoothHSP (HeadSet Profile),听说均可;
二.获取状态
1.蓝牙开启未连接耳机 - Speaker
2.单向高保真音频协议(输出) - BluetoothA2DPOutput
3.双向低保真音频协议(输出、输入) - BluetoothHFP、BluetoothHSP
三.方法实现
1.获取连接状态
#pragma mark - ****************************** 蓝牙耳机是否链接 - 状态
+ (BOOL)isBluetoothHeadsetConnectedState {
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription *routeDescription = [session currentRoute];
if (routeDescription) {
NSArray *outputs = [routeDescription outputs];
if (outputs && 0 < [outputs count]) {
AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
NSString *portType = [portDescription portType];
if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"] && [portType isEqualToString:@"BluetoothHFP"] && [portType isEqualToString:@"BluetoothHSP"]) {
return YES;
}
}
}
return NO;
}
2.获取连接实体
#pragma mark - ****************************** 蓝牙耳机是否链接 - 实体
+ (AVAudioSessionPortDescription *)isBluetoothHeadsetConnectedEntity {
/*
蓝牙开启未链接耳机:
Speaker
单向保真音频协议(输出):
BluetoothA2DPOutput
...
双向保真音频协议(输入 & 输入):
BluetoothHFP - HFP(Hands-Free Profile)
BluetoothHSP - HSP(HeadSet Profile)
其它:
Receiver
*/
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription *routeDescription = [session currentRoute];
// NSLog(@"%@", routeDescription);
if (routeDescription) {
NSArray *outputs = [routeDescription outputs]; // 输入源
if (outputs && 0 < [outputs count]) {
AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
NSString *portType = [portDescription portType];
NSLog(@"PortType: %@", portType); // 蓝牙开启未链接: Speaker && 链接耳机: BluetoothA2DPOutput BluetoothHFP BluetoothHSP
if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"]) {
NSLog(@"[蓝牙检测] - 状态 - 输出 - %@", [portDescription portName]);
return portDescription;
} else if (portType && [portType isEqualToString:@"BluetoothHFP"] && [portType isEqualToString:@"BluetoothHSP"]) {
NSLog(@"[蓝牙检测] - 状态 - 输出 & 输入 - %@", [portDescription portName]);
return portDescription;
}
}
}
return nil;
}
以上便是此次分享的全部内容,希望能对大家有所帮助!
网友评论