Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not a runLoop or ExternalAccessories problem. This is a daily OOP problem.</p> <p>The best way is to create a Communication object that can write to the outputStream and wait for response. Use @protocols to do this! (Event-Listener driven procedure)</p> <p><strong>TRY THIS:</strong></p> <p>First of all you have to attach input/output streams to a runLoop:</p> <pre><code>[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [[session inputStream] open]; [[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [[session outputStream] open]; </code></pre> <p>Become their delegate:</p> <pre><code>[[session outputStream] setDelegate:self]; [[session inputStream] setDelegate:self]; </code></pre> <p>Once you become delegate you have to implement this method:</p> <pre><code>-(void)stream:handleEvent:{}; </code></pre> <p>This is the command to write out data to a stream:</p> <pre><code>/* data is a NSData containing data to transmit. */ [[session outputStream] write:(uint8_t *)[data bytes] maxLength:[data length]]; </code></pre> <p>This is an example code, (once you have session created, whereas the answer we expect is a byte):</p> <p><strong>in Comm.h:</strong></p> <pre><code>/* Define your protocol */ @protocol CommDelegate &lt;NSObject&gt; -(void)byteReceived: (char) byte; @end @interface Comm &lt;NSObject&gt; { [...] id&lt;CommDelegate&gt; delegate; } @end @property (nonatomic, retain) id&lt;CommDelegate&gt; delegate; </code></pre> <p><strong>In Comm.m:</strong></p> <pre><code>@implementation Comm [...] -(id)init { [...] delegate = nil; [...] } -(void)write: (NSData *) data { [[session outputStream] write:(uint8_t *)[data bytes] maxLength:[data length]]; } -(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:&amp;ch maxLength:1]; /* now ch contains a byte from your MFI device ** and 'read' function decrease the length of the rx buffer by -1 */ /* Now you can notify this to the delegate */ if(self.delegate != nil) [delegate byteReceived: ch]; } break; } } </code></pre> <p><strong>your_app_controller.h:</strong> </p> <pre><code>@interface MyApp : UIViewController &lt;CommDelegate&gt; { Comm comm; } @end </code></pre> <p><strong>your_app_controller.m:</strong></p> <pre><code>@implementation MyApp -(id)init { [...] comm = [[Comm alloc] init]; [comm setDelegate: self]; /* Now your thread is listening your communication. */ } -(void)write { byte out = 'X'; [comm write: [NSData dataWithBytes: &amp;out length: 1]]; } -(void)bytereceived:(char)reply { if(reply == 'Y') { [self write]; //[self performSelectorInBackground:@selector(write) withObject:nil]; IT'S BETTER!!! } } @end </code></pre> <p><strong>Hope this helps!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload