外部附件通知:

问题描述:

我正在开发一个应用程序,它通过UART端口从附件获取数据。当我的应用程序运行很长时间时,我遇到了问题,它在iPhone进入睡眠模式后使用更多内存,并再次唤醒,在配件完全通过验证后,我的应用程序无法打开与配件的会话。在调试EAAccessoryManager类之后,我看到两个相同的设备,它们都是我的附件。转到设置/常规/关于,我发现iPhone显示两个相同的设备。 但我的问题只发生在iPhone 3G(版本iOS 4.1)上,iPhone 3GS(iOS 4.1)没有这个问题。我猜是因为我的程序使用了太多的内存,所以我无法获得accessoryDidDisconnect事件。请给我一些建议。感谢您的回答。外部附件通知:

-(EASession*) openSessionForProtocol: (NSString*)protocolString 
{ 
    NSArray* accessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories]; 

    EAAccessory* accessory = nil; 
    EASession *session = nil; 
    for(EAAccessory* obj in accessories){ 
      if([[obj protocolStrings] containsObject:protocolString]){ 
       accessory = obj; 
       break; 
      } 
    } 
    if(accessory){ 
      [accessory setDelegate:self]; 
      session = [[EASession alloc] initWithAccessory:accessory forProtocol:protocolString]; 
      if(session){ 
       NSString *msg = @""; 
       for(EAAccessory* obj in accessories){ 
        msg = [msg stringByAppendingFormat:@"\n%@",[obj name]]; 
       } 
       NSString *openSession = [NSString stringWithFormat:@"The number of devices is: %d.%@",[accessories count],msg]; 

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OpenSession" message:openSession delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
       [alert show]; 
       [alert release]; 
       [[session inputStream] setDelegate:self]; 
       [[session inputStream] scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
       [[session inputStream] open]; 
       [[session outputStream] setDelegate:self]; 
       [[session outputStream] scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
       [[session outputStream] open]; 
       [session autorelease]; 
       iRemoteAppDelegate *appDelegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate]; 
       [appDelegate SetApplicationRotation:TRUE]; 
      } 
    } 
    return session; 
} 

- (void)accessoryDidDisconnect:(EAAccessory *)accessory 
{ 
    //[HardwareController performSelectorOnMainThread:@selector(UpdateStringOnMessage:) withObject:@"Can not connect hardware module.\nPlease check hardware again." waitUntilDone:YES]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Accessory is unpluged!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [[serialSession inputStream] removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
    [[serialSession outputStream] removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 

    [serialSession release]; 
     self.serialSession = nil; 
    iRemoteAppDelegate *delegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [delegate setUserCancel:NO]; 
    AllowedEmitSignal = TRUE; 

    [delegate UpdateAboutHardwareDisconnect]; 
    [delegate SetApplicationRotation:FALSE]; 
} 

- (void)accessoryDidConnect:(NSNotification *) notification 
{ 
    iRemoteAppDelegate *appDelegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [appDelegate setUserCancel:NO]; 
    [self OpenPort]; 
    AllowedEmitSignal = TRUE; 
    [appDelegate UpdateAboutHardwareDisconnect]; 
    appDelegate.CallNumber = appDelegate.CallNumber+1; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"Accessory is attached!%d",appDelegate.CallNumber] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

-(void)OpenPort 
{ 
    int i =0; 
    [self initAllVariable]; 
    iRemoteAppDelegate *delegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    for (;self.serialSession==nil && i<2; i++) { 
      self.serialSession = [self openSessionForProtocol:PROTOCOLSTRING]; 
    } 
} 

如果设置/一般/关于显示iPhone 3G两个相同的设备,这意味着iPhone 3G无法检测到附件的“关闭”状态,当它睡觉。您必须确保附件在iPhone 3G睡眠时也进入睡眠状态(低功耗状态),在您的附件从3G接收字节后几毫秒内即告知它正在更改其电源状态。

这里不能讲太多的秘密。但从我的经验来看,iPhone 3G与3GS有着许多不同的电信号行为。您的问题与iOS应用代码无关。我强烈建议您在即将进入睡眠之前使用CRO/Logic Analyzer来调试从iPhone 3G发送的检测引脚状态和命令。