我使用 Apple 的 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];
}
我是这样写的:
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 一个 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)
有人对我做错了什么有任何想法吗?
谢谢!