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
CoreBluetooth.framework is for Bluetooth Low-Energy.
Bluetooth Low-Energy is not designed for passing sound (exit headset, speakers, etc.)
So my question is: Are your sure your headset is using Bluetooth Low-Energy?
I don't think so.
So, that's why the delegate method centralManager:didDiscoverPeripheral:
isn't triggered.
If you want to get some events of the headset, like "user pressed next song", you can use the Remote Control Events. As I suspect you may want to listen to "others events", I guess your headset is under MFI. So it may have its own protocol. For example, I worked on the iOS app for a Bluetooth HeadSet that had others functionalities, like calling a favorite number, etc. But, then, you'll need to use ExternalAccessory.framework, and may have to reverse engineer the protocol.
来源:https://stackoverflow.com/questions/23776340/ios-how-to-discover-bluetooth-headset-and-its-events