0

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
{

}
Tulon
  • 4,011
  • 6
  • 36
  • 56
Coder_A_D
  • 340
  • 5
  • 20

2 Answers2

2

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.

Larme
  • 24,190
  • 6
  • 51
  • 81
  • I am trying to discover and trigger event of Samsung HM1100 bluetooth headset on iPhone 4S, iPad mini – Coder_A_D May 21 '14 at 12:55
  • On http://www.samsung.com/hk_en/consumer/mobile/mobile-phones/accessories/BHM1100UBECWDT?subsubtype=bluetooth-headset, its clearly indicated: `Bluetooth Version: 2.1+EDR`. So NO Bluetooth Low-Energy. I gave you the answer, and where to look (ExternalAccessory.framework, if maybe it has a custom protocol, but I doubt it, and RemoteControlEvents to get the events). – Larme May 21 '14 at 13:05
  • How come the devices are being displayed and can be connected to in the "Settings"page??? – iosMentalist Aug 01 '14 at 11:08
  • @Enkidu Refer http://stackoverflow.com/a/27049490/751026] BLE devices are never visible in the Settings app until after a custom app has scanned and connected to them. Whereas 'legacy' BT devices are visible from the Settings app. With the exception of BLE devices that solicit for e.g. ANCS service connection. – geekay Nov 10 '16 at 08:33
2

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];
  1. if you want to get the callbacks while on background, you need to add Audio and AirPlay on target's capabilities:
    enter image description here

!! This answer was helpful to me when got the solution

Community
  • 1
  • 1
Ben
  • 3,832
  • 1
  • 29
  • 30