问题描述
我试图发现蓝牙耳机,并得到其事件。我读CoreBluetooth文件和实施样本$ C $为C以下。它不火的委托方法 didDiscoverPeripheral
。
请问有什么解决此?
code:
CBCentralManager * myCentralManager;
[myCentralManager scanForPeripheralsWithServices:无选项:无];
- (无效)centralManagerDidUpdateState:(CBCentralManager *){中心 //以下行打印CBCentralManagerStatePoweredOn 的NSLog(@状态:%@,[自主getCentralManagerState:central.state]);
}
//下面的方法不火 - (无效)centralManager:(CBCentralManager *)中央didDiscoverPeripheral:(CBPeripheral *)周边advertisementData:(NSDictionary的*)advertisementData RSSI(NSNumber的*)RSSI
{}
就像提到CoreBluetooth是LE设备只,结果
所以这是我做过什么来获取BluetoothHFP 设备在事件:结果
1.你需要打开AVAudioSeesion:结果
链接 AVFoundation.framework
到项目结果
2.当前可用的输入:
的NSArray * availInputs = [[AVAudioSession sharedInstance] availableInputs]
3。关于路线变更通知:结果
一个。设置新的 AVAudioSession
结果
湾注册观察者 AVAudioSessionRouteChangeNotification
- (BOOL)$ P $ {ppareAudioSession //关闭会话
BOOL成功= [[AVAudioSession sharedInstance] SETACTIVE:没有错误:无];
如果(!成功){
的NSLog(@deactivationError);
} //设置音频会话类别AVAudioSessionCategoryPlayAndRecord选项AVAudioSessionCategoryOptionAllowBluetooth
成功= [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth错误:无];
如果(!成功){
的NSLog(@setCategoryError);
} //激活音频会议
成功= [[AVAudioSession sharedInstance] SETACTIVE:YES错误:无];
如果(!成功){
的NSLog(@activationError);
} 返回成功;
}
和调用这个时候你要开始听的变化:
[自prepareAudioSession]。NSNotificationCenter * defaultCenter = [NSNotificationCenter defaultCenter]
[defaultCenter的addObserver:自我
选择:@选择(bluetoothAvailabilityDidChange :)
名:@BluetoothConnectabilityChangedNotification
对象:无];
!! 的答案是有帮助的我,当得到了解决。
I am trying to discover "Bluetooth Headset" and get its events. I read the "CoreBluetooth" documentation and implemented sample code as below. It does not fire the delegate method 'didDiscoverPeripheral
'.
Is there any solution for this?
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
{
}
Like mentioned CoreBluetooth is for LE devices only,
so this is what I did to get the events for BluetoothHFP devices:
1. you need to open AVAudioSeesion:
link AVFoundation.framework
to your project
2. for current available inputs:
NSArray *availInputs = [[AVAudioSession sharedInstance] availableInputs];
3. for notification on route change:
a. setup new AVAudioSession
b. register observer to 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;
}
and call this when you want to start listen to changes:
[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 answer was helpful to me when got the solution
这篇关于的iOS如何发现蓝牙耳机及其活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!