Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does EAAccessoryDidConnectNotification occur twice?

I have a class that manages messages coming from and going to an external accessory to an iPad. In the init I have the following code:

- (id) init
{
    self = [super init];
    if (!self) return;

    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];    //we want to hear about accessories connecting and disconnecting
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(accessoryDidConnect:)
                                                 name:EAAccessoryDidConnectNotification
                                               object:nil];

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

in dealloc I have

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];    
}

For some reason, when I connect the external accessory to the iPad the accessoryDidConnect: fires followed by an accessoryDidDisconnect: followed by accessoryDidConnect:

I can't figure out why I would get an extra connect and disconnect. Any ideas?

like image 422
Sam Avatar asked Jul 29 '11 14:07

Sam


3 Answers

the eaaccessory framework will always fire 2 connect and 2 disconnect notifications from some reason. The first connect disconnect pair will have no protocol strings, you can ignore these.

like image 192
user2055089 Avatar answered Sep 20 '22 06:09

user2055089


Change to this sequence. First notification register then for manager

[[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];    //we want to hear about accessories connecting and disconnecting
like image 42
Vijay-Apple-Dev.blogspot.com Avatar answered Sep 19 '22 06:09

Vijay-Apple-Dev.blogspot.com


Not an answer but I can't post a comment. I'm seeing this double notification too using the code provided in the answer above. I am also seeing it in the EADemo sample code provided by Apple.

like image 20
spring Avatar answered Sep 21 '22 06:09

spring