嗨,我正在为 iPAD 制作应用程序,其中我必须捕获来自每秒发送 512 个值的外部设备的数据。我使用了 Apple 的 External Accessories API,代码如下:-
- (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent
{
switch (streamEvent)
{
case NSStreamEventNone: //Sent when open complete
//NSLog(@"NSStreamEventNone");
break;
case NSStreamEventOpenCompleted: //Sent when open complete
//NSLog(@"NStreamEventOpenCompleted: %@", theStream);
break ;
case NSStreamEventHasBytesAvailable:
{
//NSLog(@"NSStreamEventHasBytesAvailable");
uint8_t buffer[1024];
unsigned int len=0;
len=[(NSInputStream *)theStream read:buffer maxLength:1024];
if(len>0){
NSData* data=[NSData dataWithBytes:buffer length:len];
NSString *value = [[NSString alloc] initWithData:dataencoding:NSASCIIStringEncoding];
}
}
break;
case NSStreamEventHasSpaceAvailable:
os = theStream;
break;
case NSStreamEventErrorOccurred:
//NSLog(@"NSStreamEventErrorOccurred");
break;
case NSStreamEventEndEncountered:
////NSLog(@"NSStreamEventEndEncountered");
break;
default:
break;
}
}
我从中得到的是一些没有正确编码的乱码。我也尝试过使用以下内容:-
NSString *value = [[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding];
但这只是一直返回null。编辑:- 当我 NSLog 时,我得到以下信息:-
vªª€ÿù…ªª€ ÿûƒªª€ÿK3ªª€ÿ£Ûªª€ÿ˳ªª€ÿ}ªª€ÿ噪ª€
我应该在这里得到整数。有人可以告诉我我在这里做错了什么。