Die Eingabe- und Ausgabe-Streams werden ständig geöffnet und die entsprechenden Delegate-Methoden werden mit dem richtigen NStreamEvent-Code (NSStreamEventOpenCompleted) aufgerufen. Bei einigen openSession-Versuchen werden die Streams jedoch erfolgreich geöffnet, funktionieren jedoch nicht. Manchmal kann ich nur in den Ausgabestream schreiben, aber ich kann nicht aus dem Eingabestream lesen. Manchmal kann ich nicht lesen oder schreiben.Externer Accessory-Stream funktioniert nicht 100% der Zeit
Hat jemand jemals in dieses Problem gerannt oder haben Sie eine Idee, warum das passiert? Jede Hilfe wird geschätzt. Vielen Dank. Hier
ist einiger relevanter Code
- (BOOL)openSession
{
_session = [[EASession alloc] initWithAccessory:_selectedAccessory
forProtocol:_protocolString];
if (!_session)
return false;
[_selectedAccessory setDelegate:self];
[[_session inputStream] setDelegate:self];
[[_session outputStream] setDelegate:self];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
[[_session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[[_session inputStream] open];
[[_session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[[_session outputStream] open];
[[NSRunLoop currentRunLoop] run];
});
return true;
}
- (void)setupControllerForAccessory:(EAAccessory *)accessory withProtocolString:(NSString *)protocolString
{
_selectedAccessory = accessory;
_protocolString = [protocolString copy];
}
/**
* There is something weird happening here too, this always get called twice when an accessory
* gets connected. Once without a protocol string and once with a protocol string. I just
* ignore the one without a protocol string.
*/
- (void)accessoryConnected:(NSNotification *)notification
{
EAAccessory *connectedEA = [notification.userInfo objectForKey:@"EAAccessoryKey"];
// Check to see if the connected EA has a protocol string
if ([[connectedEA protocolStrings] count] == 0)
return;
[self setupControllerForAccessory:connectedEA
withProtocolString:[[connectedEA protocolStrings] objectAtIndex:0]];
[self openSession];
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventOpenCompleted:
// This case always gets called twice, once for input and once for output
NSLog(@"stream %@ opened", aStream);
break;
case NSStreamEventHasBytesAvailable:
break;
case NSStreamEventHasSpaceAvailable:
break;
case NSStreamEventErrorOccurred:
break;
case NSStreamEventEndEncountered:
[self closeSession];
break;
default:
break;
}
}
- (void)closeSession
{
[[_session inputStream] close];
[[_session inputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session inputStream] setDelegate:nil];
[[_session outputStream] close];
[[_session outputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session outputStream] setDelegate:nil];
_session = nil;
_selectedAccessory = nil;
_protocolString = nil;
}
Haben Sie versucht, die mainQueue anstelle der globalen zu senden? – Putz1103
Ich habe auch dispatch_get_main_queue() ausprobiert. Das gleiche Problem tritt auf, manchmal scheitert es 9 mal hintereinander, manchmal arbeitet es 9 mal hintereinander. – jason
Wann versuchen Sie, diese Streams im Code zu verwenden? Seit dem Versand können Sie die Streams verwenden, bevor das Öffnen abgeschlossen ist. – Putz1103