3

I've been trying to get this to work for a while now. I've done everything they say in the documentation and still got nothing.

This is the code in my app delegate that registers for local notifications:

- (void) registerForLocalNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(_accessoryConnected:)
                                             name:EAAccessoryDidConnectNotification
                                           object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(_accessoryDisconnected:)
                                             name:EAAccessoryDidDisconnectNotification
                                           object:nil];

[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; }

The above is called from applicationDidFinishLaunching.

Here is the code of the connect/disconnect methods:

- (void) _accessoryConnected:(NSNotification *)notification {
          NSLog(@"_accessoryConnected"); }

- (void) _accessoryDisconnected:(NSNotification*)notification {
NSLog(@"_accessoryDisconnected"); }


-(void) accessoryDidDisconnect:(EAAccessory *) accessory {
NSLog(@"accessoryDidDisconnect"); }

Tried connecting the headphones that come with the iPhone and got nothing, same for my external accessory I want to integrate with the app.

Please help, Thanks, Shaul.

Shaul Amran
  • 45
  • 1
  • 5
  • Well, after doing some more reading I see that the headphones jack isn't supported in the external accessory framework. Is there any other framework I can use? Please anyone? – Shaul Amran Aug 03 '11 at 07:27

1 Answers1

4

You should use AudioSessionPropertyListener for this. EAAccessory notifications are for hardware that connects to the 30 pin port. Add this listener in viewDidLoad and remove it in ViewDidUnLoad

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil);

Add the following methods in the view controller.

BOOL isHeadsetPluggedIn() {
    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                              &routeSize,
                                              &route
                                              );    
    NSLog(@"%@", route);
    return (!error && (route != NULL) && ([(NSString*)route rangeOfString:@"Head"].location != NSNotFound));
}

void audioSessionPropertyListener(void* inClientData, AudioSessionPropertyID inID,
                                  UInt32 inDataSize, const void* inData) {
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

    // Determines the reason for the route change, to ensure that it is not
    //      because of a category change.
    CFDictionaryRef routeChangeDictionary = inData;    
    CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

    SInt32 routeChangeReason;    
    CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

    // "Old device unavailable" indicates that a headset was unplugged, or that the
    //  device was removed from a dock connector that supports audio output. 
    if (routeChangeReason != kAudioSessionRouteChangeReason_OldDeviceUnavailable)
        return;

    if (!isHeadsetPluggedIn()) 
    {
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    }
    else 
    {
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
    }    
}

Note, I got this code long ago from somewhere and it worked for me. Cannot attribute the source now as I don't know where I got it from.

Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • Tried to manage with this C code but it's been years since I touched C. Saw there's an alternative called AVAudioSession in Objective-C. Do you think I can do the same with this? http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008240 – Shaul Amran Aug 03 '11 at 07:57
  • Ok I managed to get this code you posted to work, but it works only when the headset isn't connected to the device before the app starts. Any ideas why is that? – Shaul Amran Aug 03 '11 at 10:33
  • Call isHeadsetPluggedIn on viewDidLoad as well. The above code calls it only when audio route change notification is received. – Mugunth Aug 04 '11 at 04:00
  • I simply missed initialization of the audio session. That was the real problem. It needs to come before anything else related to audio sessions. After I added that, everything started working. – Shaul Amran Aug 08 '11 at 05:58