tags:

views:

22

answers:

0

Dear all,

I am developing an application, it get data from accessory via UART port. I am having a problem when my application runs for a long time, it uses much more memory, after the iPhone into sleep mode, and wake up again, my application can not open the session with accessory after the accessory has been authenticated completely. After I debug class EAAccessoryManager, I see two identical devices, they are all my accessory. Go to Settings / General / About, I found that iphone show two identical devices there. But my problem occurs only on the iPhone 3G (version iOS 4.1), The iPhone 3GS (iOS 4.1) does not have this problem. I guess because my program uses too much memory so I can not get accessoryDidDisconnect events. Please give me some advices. thank for your answer.

-(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];
 }

}