more outputstream problems…raw data and odd output. what did i do wrong?
-
16-06-2021 - |
문제
I've opened the following input and output bluetooth streams using Apple's External Accessory Framework:
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];
}
I'm writing to it like this:
uint8_t aByte[] = {0x02, 0x06, 0x04};
[[session outputStream] write:aByte maxLength:4];
NSLog(@"%d", aByte[2]);
I'm reading from it like this:
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;
I SHOULD be receiving back from the device "AA4" because it's sends me back two alpha characters followed by the 3 byte that was sent to it in the last stream event. The LCD screen on the device is reporting that it has received a 2 a 4 and a 6. And it is reporting that it sent an A and A and a 4. But "NSLog(@"readBuf: %s", readBuf);" always prints:
AA + a upside question mark//(can't seem to copy and paste that symbol from xcode)
Anyone have any ideas on what I've done wrong?
Thanks!
해결책
Nevermind...
I printed each byte individually instead of as a string. Printed the first two as char's and the last as a decimal. C style output is still weird to me...