Why does EAAccessoryDidConnectNotification happen twice?

I have a class that controls messages from an external accessory to an iPad. In 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]; ... } 

at 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 an external accessory to my iPad, the DidConnect accessory: fires and then the optional DidDisconnect device is added: it follows the DidConnect accessory:

I can’t understand why I get an additional connection and disconnect. Any ideas?

+7
source share
4 answers

eaaccessory always starts 2 connections and 2 disconnects notifications for any reason. The first pair of connectors for the connection will not contain protocol lines, you can ignore them.

+5
source

Change this sequence. First register 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
source

Not an answer, but I can not post a comment. I also see this double notification using the code provided in the answer above. I also see this in the EADemo code example provided by Apple.

+2
source

EAAccessoryDidConnectNotification Documentation EAAccessoryDidConnectNotification

In some cases, a connection notification may be sent before authentication is completed, resulting in an empty protocol array and a subsequent disconnect message. If this happens, another connection message will be sent later when authentication is successful.

This definitely should not happen all the time, but if you get this connected / disconnected / connected sequence, check the log lines. This is probably due to an authentication error.

0
source

All Articles