我正在开发一款使用蓝牙2.1的应用。每次应用程序变为活动状态时,它都会打开一个EASession。每次应用程序进入后台或终止时,EASession都会关闭。
如果已连接蓝牙模块,并且应用程序已启动,则会打开正常,发送/获取数据全部正常工作。
但是,如果蓝牙模块在应用程序运行时失去通信,则在重新连接并从后台打开应用程序之后。 EASession open失败。
并且没有列出连接附件。
{
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;
}
此处[_accessoryList count]为0。
有谁知道发生了什么事? 谢谢!
编辑:
我已经运行了更多测试,而这次它看起来有所不同。
- (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);
}
这次选择的配件看起来很好。但它无法初始化会话。
在initWithAccessory行之后,它打印出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
答案 0 :(得分:1)
您必须手动将_protocolString的值添加到您应用的info.plist(支持的外部附件协议部分)。