ios - Initiate EASession Fail -- EAAccessory Doesn't Have Protocols -


i'm working on ios app communicate bluetooth 2.1. app connects bt when becomes active. works fine if app goes background , becomes active again.

but noticed problem:

if turn bt module power off, app notification following:

  - (void)accessorydiddisconnect:(eaaccessory *)accessory     {         nslog(@"eacontroller::accessorydiddisconnect:");         _selectedaccessory = nil;         uialertview *alert = [[uialertview alloc]initwithtitle:@"alert" message:@"lost connection. " delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil];         [alert show];         nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];         [defaults setbool:yes forkey:@"lost communication"];         [self closesession];     } 

then turn module power on, go settings -> bluetooth, connect bt module iphone, active app (come background), easession can not initiated:

if (_session == nil)     {         nslog(@"eacontroller::opensession");         [_selectedaccessory setdelegate:self];         _session = [[easession alloc] initwithaccessory:[self selectedaccessory] forprotocol:_protocolstring];          if (_session)         {                     // set delegate........         }         else         {             nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];             [defaults setbool:no forkey:unit_has_connection_userdefault_key];             nslog(@"creating session failed");         }     } 

initiate _accessorylist , _selectedaccessory:

_accessorylist = [[nsmutablearray alloc] initwitharray:[[eaaccessorymanager sharedaccessorymanager] connectedaccessories]]; _selectedaccessory = [_accessorylist objectatindex:0]; 

i found out _accessorylist has accessory i'm using it's "protocols" empty.

$12 = 0x1dd58050 <__nsarrayi 0x1dd58050>( <eaaccessory: 0x1dd1dce0> {   connected:yes   connectionid:xxx   name: xxx   manufacturer: xxx   modelnumber: xxx   serialnumber:    firmwarerevision: xxx   hardwarerevision: xxx   macaddress: xxx   protocols: ( )   delegate: (null) } ) 

if kill app , restart, works fine.

does know how solve this?

this question similar this one , happens in different situations , answer question doesn't seem solve problem.

after days of searching solution, solved problem(or hope solved it.). i'm posting answer here if has same problem can have idea.

you can never trust eaaccessorymanager, may have ghost accessory. initiate _accessorylist using following command doesn't work.

_accessorylist = [[nsmutablearray alloc] initwitharray:[[eaaccessorymanager sharedaccessorymanager] connectedaccessories]]; 

if initiate accessorylist ghost accessory, sure cannot initiate easession.

the right way use eaaccessory notification, described in this answer, didn't how in detail.

first, need create post 2 notifications:

[[nsnotificationcenter defaultcenter]addobserver:self selector:@selector(accessoryconnected:) name:eaaccessorydidconnectnotification object:nil]; [[nsnotificationcenter defaultcenter]addobserver:self selector:@selector(accessorydisconnected:) name:eaaccessorydidconnectnotification object:nil];  [[eaaccessorymanager sharedaccessorymanager]registerforlocalnotifications]; 

then in accessoryconnected , accessorydisconnected, can following.

- (void)accessoryconnected:(nsnotification *)notification {     nslog(@"eacontroller::accessoryconnected");      eaaccessory *connectedaccessory = [[notification userinfo] objectforkey:eaaccessorykey];     [[self accessorylist] addobject:connectedaccessory];     if ([_accessorylist count])     {         _selectedaccessory = [_accessorylist objectatindex:0];         nsarray *protocolstrings = [_selectedaccessory protocolstrings];         if ([protocolstrings count]) {             self.protocolstring = [protocolstrings objectatindex:0];             [self opensession];         }     } }   - (void)accessorydisconnected:(nsnotification *)notification {     eaaccessory *disconnectedaccessory = [[notification userinfo] objectforkey:eaaccessorykey];      int disconnectedaccessoryindex = 0;     for(eaaccessory *accessory in [self accessorylist]) {         if ([disconnectedaccessory connectionid] == [accessory connectionid]) {             break;         }         disconnectedaccessoryindex++;     }      if (disconnectedaccessoryindex < [[self accessorylist] count]) {         [[self accessorylist ] removeobjectatindex:disconnectedaccessoryindex];     } else {         nslog(@"could not find disconnected accessory in accessory list");     }     nslog(@"_accessory did disconnect: %@",_accessorylist);  } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -