Spire Spire - 2 months ago 34
iOS Question

Bluetooth classic connection awareness

I have a Bluetooth application that communicates with a peripheral device over a low energy.

This peripheral device also has a classic (HFP and/or A2DP) connection with the iOS device. It happens that the classic connection gets interrupted sometimes.

What I need is to be able to notify the user from in application that the classic connection has been lost.

How can I make my application aware of the classic connection?

What way would you prefer to do this?

Answer

Whilst CoreBluetooth is used for accessing Bluetooth LE or 4.0 devices you can use the ExternalAccessory framework to communicate with other bluetooth devices.

Like so:

- (void)registerForNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
} 

- (void)accessoryDidConnect:(NSNotification *)notification
{
    // Weird thing with ExternalAccessory where this notification is called more than once per accessory...
    if ([[(EAAccessory *)[[(EAAccessoryManager *)notification.object connectedAccessories] lastObject] protocolStrings] count]) {
        // Valid call
        if ([[(EAAccessory *)[notification.userInfo valueForKey:EAAccessoryKey] protocolStrings] containsObject:/*Protocol string for the accessory*/]) {

        }
    }
}

- (void)accessoryDidDisconnect:(NSNotification *)notification
{
    if ([[(EAAccessory *)[notification.userInfo valueForKey:EAAccessoryKey] protocolStrings] containsObject:/*Protocol string for the accessory*/]) {
        // Disconnected
    }
}

For this to work you have to add the key 'Supported external accessory protocols' to your app's info.plist and list the protocols for the bluetooth devices in the array under that key.

Also note that for distribution on the App Store the bluetooth device has to be registered under the Apple MFi program and you have to be an accepted developer (by the device's manufacturers).