外部附件通知:

Posted

技术标签:

【中文标题】外部附件通知:【英文标题】:External accessory notification: 【发布时间】:2010-09-30 06:13:55 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

如果设置 / 通用 / 关于 iPhone 3G 显示两个相同的设备,这意味着 iPhone 3G 在睡眠时未能检测到配件的“关闭”状态。您必须确保在 iPhone 3G 睡眠时,配件也进入睡眠状态(低电量状态),在您的配件从 3G 接收到告知其正在改变其电源状态的字节后的特定毫秒内。

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

【讨论】:

以上是关于外部附件通知:的主要内容,如果未能解决你的问题,请参考以下文章

外部附件 api 出错,如何使用外部附件

外部附件命令协议

使用 iOS 外部附件协议在外部附件上支持多个应用程序

iOS 外部附件框架附件DidConnect 未触发

iOS、Appcelerator Titanium 和外部附件框架

外部附件框架...EADemo问题