我正在尝试发现“蓝牙耳机”并获取其事件。我读了“核心蓝牙”文档和实现的示例代码如下。它不会触发委托方法'didDiscoverPeripheral
'.
有什么解决办法吗?
Code:
CBCentralManager *myCentralManager;
[myCentralManager scanForPeripheralsWithServices:nil options:nil];
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
//following line prints CBCentralManagerStatePoweredOn
NSLog(@"state:%@", [self getCentralManagerState:central.state]);
}
//following method does not fire
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
}
如前所述,CoreBluetooth 仅适用于 LE 设备,
这就是我所做的蓝牙HFP 的事件设备:
1.需要打开AVAudioSession:
link AVFoundation.framework
到你的项目
2. 对于当前可用的输入:
NSArray *availInputs = [[AVAudioSession sharedInstance] availableInputs];
3. 路线变更通知:
A。设置新的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];
- if you want to get the callbacks while on background, you need to add Audio and AirPlay on target's capabilities:
!! This https://stackoverflow.com/a/20898939/1787109得到解决方案时,答案对我有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)