ios - 阻止与 ios 配件的通信

标签 ios objective-c cocoa-touch external-accessory

Apple 似乎建议使用运行循环与外部配件进行数据通信。但是,除非我遗漏了什么,否则 runloop 不太适合某些类型的通信。

我们有一个 -experimental- 配件,我们需要向其发送任意数量的字节(最多,比如 1024),然后配件处理该数据(可变延迟,比如 1ms 到 1000ms 之间),后跟来自配件的可变长度响应(最多 1024 字节)。

我们想开发一个静态库(框架)来与配件进行通信。基本上,这个库将有一个函数,它将 NSArray 或 NSMutableArray 作为输入,并返回包含响应的 NSArray 或 NSMutableArray。

问题是推荐的runloops策略不太适合这类应用。在静态库函数中,在准备好要传输的数据并调度传输之后,我们要进入某种“等待”状态。但是,这种等待状态不能基于轮询方法(例如等待由接收路由设置的 -synchronized- 变量),因为接收例程永远不会执行(因为它们在同一个线程上) .

如果我们不使用 runloops,那么我们就不知道什么时候读取数据,因为我们不知道数据什么时候到达。

关于如何解决这个问题有什么想法或建议吗?有没有例子?

最佳答案

这不是 runLoop 或 ExternalAccessories 问题。 这是一个日常的 OOP 问题。

最好的方法是创建一个可以写入 outputStream 并等待响应的 Communication 对象。 使用@protocols 来做到这一点! (事件监听器驱动的过程)

试试这个:

首先,您必须将输入/输出流附加到 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];

一旦你成为委托(delegate)人,你必须实现这个方法:

-(void)stream:handleEvent:{};

这是将数据写入流的命令:

/* data is a NSData containing data to transmit. */
[[session outputStream] write:(uint8_t *)[data bytes] maxLength:[data length]];

这是一个示例代码,(一旦您创建了 session ,而我们期望的答案是一个字节):

在 Comm.h 中:

/* Define your protocol */
@protocol CommDelegate <NSObject>
    -(void)byteReceived: (char) byte;
@end

@interface Comm <NSObject> {
    [...]
    id<CommDelegate> delegate;
}
@end

@property (nonatomic, retain) id<CommDelegate> delegate;

在 Comm.m 中:

@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:&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;
    }
}

your_app_controller.h:

@interface MyApp : UIViewController <CommDelegate> {
    Comm comm;
}
@end

your_app_controller.m:

@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: &out length: 1]];
}

-(void)bytereceived:(char)reply {
    if(reply == 'Y') {
        [self write];
        //[self performSelectorInBackground:@selector(write) withObject:nil]; IT'S BETTER!!!
    }

}

@end

希望这对您有所帮助!

关于ios - 阻止与 ios 配件的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19768512/

相关文章:

ios - 在 ios 中将阿拉伯语单词转换为英语的简单方法?

ios - 一个 View Controller 中的两个警报 View - buttonIndex 响应

ios - 如何删除添加到导航 Controller View 中的 View ?

ios - TableViewController 中的透明部分

ios - 如何在 Swift 或 Obj-C 中访问 AFP 或 SMB 共享(例如时间胶囊)

ios - 导航 Controller 按下后的 View 高度不正确

jquery - iOS 不滚动 div

iphone - 从选项卡栏返回到第一个 View

iOS:为什么 drawRect: 被调用了两次,为什么这个 ivar 值似乎无缘无故地改变了?

ios - Objective C NSString* 属性保留计数奇数