Reputation: 751
I'm working on an app use Bluetooth 2.1. Each time the app becomes active, it will open an EASession. Each time the app go to background or terminate, EASession closed.
If the bluetooth module is connected, and app is started fresh, it opens alright, send/get data all working.
But if bluetooth module lose communication when app is running, after it's reconnected and app is opened from background. EASession open fails.
And there are no connected accessory listed.
{
if (_selectedAccessory == nil)
{
_accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
NSLog(@"accessory count: %d", [_accessoryList count]);
if ([_accessoryList count])
{
_selectedAccessory = [_accessoryList objectAtIndex:0];
NSArray *protocolStrings = [_selectedAccessory protocolStrings];
self.protocolString = [protocolStrings objectAtIndex:0];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"You don't have any machine connected.\nPlease go to Settings->Bluetooth to set up connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
return _selectedAccessory;
}
Here [_accessoryList count] is 0.
Does anyone know what's going on? Thanks!
EDIT:
I have run more tests, and this time it looks different.
- (BOOL)openSession
{
if (_session == nil)
{
NSLog(@"EAController::openSession");
[_selectedAccessory setDelegate:self];
_session = [[EASession alloc] initWithAccessory:[self selectedAccessory] forProtocol:_protocolString];
if (_session)
{
[[_session inputStream] setDelegate:self];
[[_session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session inputStream] open];
[[_session outputStream] setDelegate:self];
[[_session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session outputStream] open];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:Unit_Has_Connection];
NSLog(@"opened the session");
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:Unit_Has_Connection];
NSLog(@"creating session failed");
}
}
return (_session != nil);
}
This time selectedAccessory seems fine. But it cannot init session.
After initWithAccessory line, it prints out ERROR:
2013-06-17 15:03:39.967 IceMachine[770:60b] ERROR - opening session failed
2013-06-17 15:03:39.968 IceMachine[770:60b] ERROR - /SourceCache/ExternalAccessory/ExternalAccessory-237/EASession.m:-[EASession dealloc] - 139 unable to close session for _accessory=0x15543fe0 and sessionID=65536
Upvotes: 3
Views: 1525
Reputation: 11
You have to add manually the value of _protocolString to your app's info.plist (section Supported external accessory protocols).
Upvotes: 1