0

I'm trying to understand how EADemo works and how the External Accessory Framework works. EADemo is available here:

http://developer.apple.com/library/ios/#samplecode/EADemo/Introduction/Intro.html

All I want to do is modify Apple's EADemo project to display the bytes it receives (assuming they are ASCII type characters) instead of just counting how many bytes it has received.. So I changed EASessionTransferViewController.m...from:

- (void)_sessionDataReceived:(NSNotification *)notification
{
    EADSessionController *sessionController = (EADSessionController *)[notification object];
    uint32_t bytesAvailable = 0;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0) {
        NSData *data = [sessionController readData:bytesAvailable];
        if (data) {
            _totalBytesRead += bytesAvailable;
        }
    }

    [_receivedBytesLabel setText:[NSString stringWithFormat:@"Bytes Received from Session: %d", _totalBytesRead]];
}

@end

To...

- (void)_sessionDataReceived:(NSNotification *)notification
{
    EADSessionController *sessionController = (EADSessionController *)[notification object];
    uint32_t bytesAvailable = 0;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0) {
        NSData *data = [sessionController readData:bytesAvailable];
        if (data) {
            _totalBytesRead += bytesAvailable;
            NSString *asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        }
    }
    [_receivedBytesLabel setText:[NSString stringWithFormat:@"ASCII bytes read: %@", asciiStringFromData]];
}

@end

But this is not working at all. Last time I tried, it displayed nothing. It was connected to a bluetooth board that was just echoing back ASCII characters or strings it received.

Can someone help?

Jay Kim
  • 843
  • 4
  • 12
  • 28
  • Did you verify that you were getting a response at all? When you say that "it displayed nothing", do you mean that it displayed "ASCII bytes read: " and nothing else, or do you mean that it didn't even display that? – Jim May 09 '12 at 18:42
  • It displays "ASCII bytes read:", but it displays nothing else. Board is definitely functioning as it works with a android bluetooth program that shows echoes. – Jay Kim May 09 '12 at 18:54
  • The fact that it works with Android means nothing. There's several things the board needs to do to communicate with an iPhone that it does not need to do with Android. Does the original program correctly report that bytes are being read before you modify it? – Jim May 09 '12 at 19:35
  • Jim, yup the original program works fine. I also made a slight change to print within the loop. Still nothing. – Jay Kim May 09 '12 at 20:36
  • Are you sure this was a MFi-compliant board? The External Accessory framework only works with MFi devices, even over Bluetooth. – Brad Larson May 09 '12 at 22:19
  • 1
    Brad, Yup, it's MFi compliant. It's the Roving Networks RN-41-APL. – Jay Kim May 10 '12 at 02:18
  • 1
    Maybe I'm missing something, but is there a reason why your `asciiStringFromData` isn't declared outside of your `while` loop? Because you are declaring it within the `while` loop, but accessing it for writing to the display outside of the loop. I would think your application wouldn't even compile like this. – RLH May 10 '12 at 13:47

1 Answers1

0

You should try to use the NSUTF8StringEncoding encoding instead of NSASCIIStringEncoding.

Here's an example:

NSString* asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Henrique de Sousa
  • 5,727
  • 49
  • 55