就像提到的那样,CoreBluetooth 仅适用于 LE 设备,
所以这就是我为 BluetoothHFP设备获取事件所做的:
1. 您需要打开 AVAudioSeesion:
链接AVFoundation.framework
到您的项目
2. 对于当前可用的输入:
NSArray *availInputs = [[AVAudioSession sharedInstance] availableInputs];
3. 路线变更通知
:设置新的AVAudioSession
b. 注册观察员AVAudioSessionRouteChangeNotification
- (BOOL)prepareAudioSession {
// deactivate session
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil];
if (!success) {
NSLog(@"deactivationError");
}
// set audio session category AVAudioSessionCategoryPlayAndRecord options AVAudioSessionCategoryOptionAllowBluetooth
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
if (!success) {
NSLog(@"setCategoryError");
}
// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) {
NSLog(@"activationError");
}
return success;
}
并在您想开始收听更改时调用它:
[self prepareAudioSession];
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(bluetoothAvailabilityDidChange:)
name:@"BluetoothConnectabilityChangedNotification"
object:nil];
- 如果您想在后台获取回调,则需要在目标功能上添加音频和 AirPlay:

!!得到解决方案时,这个答案对我有帮助