How to send and received data from external accessory asynchronize
-
13-07-2021 - |
문제
I am confused about send and received data from external accessory asynchronized way. I use MFi External Accessory, I checked EADemo, but seems that send and received data synchronized way. Any suggestions about this, thanks in advance.
해결책
First of all you have to attach input/output streams to a runLoop:
[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session inputStream] open];
[[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session outputStream] open];
Become their delegate:
[[session outputStream] setDelegate:self];
[[session inputStream] setDelegate:self];
Once you become delegate you have to implement this method:
-(void)stream:handleEvent:{};
Here is an example:
-(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;
}
}
This is the command to write out data to a stream:
/* txQueue is a NSData containing data to transmit. */
[[session outputStream] write:(uint8_t *)[txQueue bytes] maxLength:[txQueue length]];
제휴하지 않습니다 StackOverflow