我对从外部附件异步方式发送和接收数据感到困惑。我使用MFi External Accessory,我检查了EADemo,但似乎发送和接收数据同步方式。对此有任何建议,在此先感谢。
问问题
1159 次
1 回答
2
首先,您必须将输入/输出流附加到 runLoop:
[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session inputStream] open];
[[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session outputStream] open];
成为他们的代表:
[[session outputStream] setDelegate:self];
[[session inputStream] setDelegate:self];
一旦你成为委托,你必须实现这个方法:
-(void)stream:handleEvent:{};
这是一个例子:
-(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)_event {
switch (_event)
{
case NSStreamEventHasBytesAvailable:
/* This part will be executed every time your rx buffer contains at least 1 byte */
switch(state) {
uint8_t ch;
/* Read byte per byte */
[stream read:&ch maxLength:1];
/* now ch contains a byte from your MFI device
** and 'read' function decrease the length of the rx buffer by -1 */
}
break;
}
}
这是将数据写入流的命令:
/* txQueue is a NSData containing data to transmit. */
[[session outputStream] write:(uint8_t *)[txQueue bytes] maxLength:[txQueue length]];
于 2013-03-11T10:46:26.897 回答