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



我使用 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 个字节。设备上的液晶屏报告已收到 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)

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

谢谢!

关系...

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

相关内容

最新更新