External Accessory stream does not work 100% of the time
Asked Answered
G

1

6

The input and output streams get opened all the time and the respective delegate methods get called with the correct NSStreamEvent code (NSStreamEventOpenCompleted). However, on some openSession attempts the streams get opened successfully but they don't work. Sometimes I can only write to the output stream but I can't read from the input stream. Sometimes I can't read or write.

Has anyone has ever ran into this problem or have any idea to why this is happening? Any help is appreciated. Thanks.

Here is some relevant 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;
}
Gotthard answered 4/3, 2014 at 17:33 Comment(5)
Have you tried dispatching to the mainQueue instead of the global?Handley
I've tried dispatch_get_main_queue() as well. Same issue occurs, sometimes it fails 9 times in a row, sometimes it works 9 times in a row.Gotthard
When do you try to use these streams in code? Since it's dispatch you could be using the streams before the open is complete.Handley
I start writing to the stream after they successfully open (I find this out via the NSStreamEventOpenCompleted eventCode in the delegate method). However, the bluetooth device I am talking with start streaming (constant flow) data to the phone upon connection.Gotthard
What type of hardware are you using? Are you using BT or Hardware port?Obstinate
E
0

I cannot add a comment to your question(less points!) but i have some hint because my team is also facing the same problem which is intermittent using streams. We have raised a bug to the network team for the same. If possible try to log error, most probably you will getting an error "Stream ended Unexpectedly" in your debugger console. This may appear or may not appear.

Here is the similar code provided in apple documentation which is also having the same issue. https://developer.apple.com/library/ios/samplecode/SimpleURLConnections/Listings/PostController_m.html#//apple_ref/doc/uid/DTS40009245-PostController_m-DontLinkElementID_12

Ernst answered 25/3, 2014 at 21:59 Comment(5)
This error has stopped creeping up on me since we switched to another bluetooth module. That might have been the problem? Cannot be 100% sure.. ThanksGotthard
@jason, I am facing the same problem. Could you let me know what another bluetooth module did you switch to?Bandler
@Bandler I am using ST's bluetooth moduleGotthard
@jason. Could you let me know any links or references about ST's bluetooth module?Bandler
@jason,Could you let me know any links or references about ST's bluetooth module?Bandler

© 2022 - 2024 — McMap. All rights reserved.