Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EASession, EAAccessoryDelegate and "ERROR - opening session failed"

I am working with External Accessory framework. I am encountering problems reestablishing EASession after app goes into background and then returns to foreground. If I terminate my app and relaunch, then Bluetooth connection is reestablished as one would expect. I suspect that there is some part of the teardown that I am missing - or which is not exposed (??).

[EAAccessoryManager sharedAccessoryManager] connectedAccessories]] is returning my connected accessory, and I am able to query it to get name, modelNumber, etc. However, the following line sets _session to nil.

_session = [[EASession alloc] initWithAccessory:_accessory forProtocol:_protocolString];

Is there any way to diagnosis the reason for failed EASession initialization?

Is there some mantra for clearing out old EASession?

This question is related to this one - but I am not asking for advice on which path to follow. I am asking why this path has this large pitfall and how to navigate around it.

like image 421
westsider Avatar asked Dec 09 '10 02:12

westsider


1 Answers

I've found (in the post iOS4.1 world) that leaving the app (Backgrounding or quitting) will cause a DidDisconnectNotification to fire off. In the case of just hitting the power button or having the device sleep; we don't see the connection go down.

Now if the BT device goes out of range or goes to sleep itself. Then the connection goes down.

As a result, we no longer depend on anything but the ConnectionNotifications. We don't even trust the [[EAAccessoryManager sharedAccessoryManager] connectedAccessories] list because we've found it can sometimes contain 'ghost accessories' that will say they are connected and have streams that you can connect to and get write available events from even after the entire bluetooth system has gone down (BT icon off)

ConnectionNotifications are cached when you are in the background, so you should get a fresh state when you re-enter the app.

Of course on first entry; you want to make sure that you've set up all your listeners (etc) properly.

like image 184
Dru Freeman Avatar answered Sep 29 '22 09:09

Dru Freeman