1

アプリケーションを開発しています。UART ポート経由でアクセサリからデータを取得します。アプリケーションを長時間実行すると問題が発生します。iPhone がスリープ モードになった後、より多くのメモリを使用し、再び起動すると、アクセサリが完全に認証された後、アプリケーションがアクセサリとのセッションを開くことができません。クラス EAAccessoryManager をデバッグした後、2 つの同一のデバイスが表示されます。これらはすべて私のアクセサリです。設定/一般/情報に移動すると、iPhoneに2つの同一のデバイスが表示されることがわかりました。しかし、私の問題は 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];
     }
}
4

1 に答える 1

0

設定 / 一般 / バージョン情報 に iPhone 3G の同一のデバイスが 2 つ表示される場合、iPhone 3G がスリープ時にアクセサリの「オフ」状態を検出できなかったことを意味します。iPhone 3G がスリープ状態になると、アクセサリが 3G から電源状態の変更を通知するバイトを受信して​​から特定のミリ秒以内に、アクセサリもスリープ状態 (低電力状態) になるようにする必要があります。

ここであまり秘密を語ることはできません。しかし、私の経験からすると、iPhone 3G は 3GS とは電気信号の振る舞いが大きく異なります。あなたの問題は、iOS アプリのコードとは関係ありません。CRO / Logic Analyzer を使用して、スリープ状態になる直前に iPhone 3G から送信された検出ピンの状態とコマンドをデバッグすることを強くお勧めします。

于 2011-09-01T18:19:55.650 に答える