Why does EAAccessoryDidConnectNotification occur twice?

吃可爱长大的小学妹 提交于 2019-12-09 17:15:51

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


The answer is on the documentation of EAAccessoryDidConnectNotification

In some cases, the connection notification may be sent before authentication has completed, resulting in an empty protocolStrings array and a subsequent disconnection message. If this happens, another connection message is sent later, when authentication succeeds.

It's definitely not supposed to happen all the time, but if you receive this connected / disconnected / connected sequence, check the protocol strings. It's probably related to the authentication failing.



来源:https://stackoverflow.com/questions/6874853/why-does-eaaccessorydidconnectnotification-occur-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!