更多输出流问题...原始数据和奇数输出。我做错了什么?

时间:2012-06-24 16:32:58

标签: objective-c ios xcode stream external-accessory

我使用Apple的外部附件框架打开了以下输入和输出蓝牙流:

session = [[EASession alloc] initWithAccessory:acc forProtocol:protocol];

if (session){
    [[session inputStream] setDelegate:self];
    [[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [[session inputStream] open];

    [[session outputStream] setDelegate:self];
    [[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [[session outputStream] open];
}

我写的是这样的:

uint8_t aByte[] = {0x02, 0x06, 0x04};
[[session outputStream] write:aByte maxLength:4];
NSLog(@"%d", aByte[2]);

我正是这样读的:

        case NSStreamEventHasBytesAvailable:
            NSLog(@"NSStreamEventHasBytesAvailable");
            uint8_t readBuf[128];
            memset(readBuf, 0, sizeof(readBuf));
            NSInteger numberRead = [[session inputStream] read:readBuf maxLength:3];

            if(numberRead < 0){
                NSError *error = [[session inputStream] streamError];
                NSLog(@"%@", [error localizedDescription]);
            }
            else if (numberRead > 0) {
                NSLog(@"numberRead: %d", numberRead);
                NSLog(@"readBuf: %s", readBuf);
            }
            else{
                break;
            }
            break;

我应该从设备“AA4”接收回来,因为它会向我发回两个字母字符,后跟在最后一个流事件中发送给它的3个字节。设备上的LCD屏幕报告它已收到2 a 4和6.并且它报告它发送了A和A以及4.但是“NSLog(@”readBuf:%s“,readBuf); “总是打印:

AA + a upside question mark//(can't seem to copy and paste that symbol from xcode)

任何人对我做错了什么都有任何想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

...没关系

我单独打印每个字节而不是字符串。将前两个打印为char,将最后打印为十进制。 C风格输出对我来说仍然很奇怪......