我已成功创建与 Apple MFi 打印机的连接,但由于未调用委托,我无法让 NSOutputStream 写入打印机。
这是我的 LibPrint.h:-
#import <Foundation/Foundation.h>
#import <ExternalAccessory/ExternalAccessory.h>
#import "FlashRuntimeExtensions.h"
@interface LibPrint : NSObject <NSStreamDelegate>
- (void)printR;
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent;
@end
// C interface
FREObject fncPrintR(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]);
LibPrint.m:-
#import "LibPrint.h"
@interface LibPrint ()
@property (nonatomic, strong) EASession *easPrinter;
@property (nonatomic, strong) NSData *datPrintData;
@end
@implementation LibPrint
FREContext eventContext;
LibPrint *AirInAppRefToSelf;
#pragma mark - NSObject
-(id)init
{
self = [super init];
if (self)
{
AirInAppRefToSelf = self;
self.easPrinter = nil;
self.datPrintData = nil;
}
return self;
}
-(void)dealloc
{
self.easPrinter = nil;
self.datPrintData = nil;
AirInAppRefToSelf = nil;
}
#pragma mark - ANE setup
void LibPrintExtContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{
*numFunctionsToTest = 1;
FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * *numFunctionsToTest);
func[0].name = (const uint8_t*) "printR";
func[0].functionData = NULL;
func[0].function = &fncPrintR;
*functionsToSet = func;
eventContext = ctx;
if ((LibPrint*) AirInAppRefToSelf == nil)
{
AirInAppRefToSelf = [[LibPrint alloc] init];
}
}
void LibPrintContextFinalizer(FREContext ctx) { }
void LibPrintExtensionInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* ctxFinalizerToSet)
{
*extDataToSet = NULL;
*ctxInitializerToSet = &LibPrintExtContextInitializer;
*ctxFinalizerToSet = &LibPrintContextFinalizer;
}
void LibPrintFinalizer(void* extData) { }
#pragma mark - C interface
FREObject fncPrintR(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
FREObject result = NULL;
if ([AirInAppRefToSelf datPrintData] != nil) {
[AirInAppRefToSelf setDatPrintData:nil];
}
// Retrieve message
NSData *ldatPrintData = toNSDataByteArray(argv[0]);
[AirInAppRefToSelf setDatPrintData:ldatPrintData];
[(LibPrint*) AirInAppRefToSelf printR];
return result;
}
- (void)printR
{
BOOL lblnConnected = false;
BOOL lblnFound = false;
NSArray *accessories = [EAAccessoryManager sharedAccessoryManager].connectedAccessories;
for (EAAccessory *accessory in accessories)
{
if (accessory.connected)
{
lblnConnected = true;
for (NSString *protocol in accessory.protocolStrings)
{
if ([protocol isEqualToString:@"jp.star-m.starpro"])
{
lblnFound = true;
[self setEasPrinter:[[EASession alloc] initWithAccessory:accessory forProtocol:protocol]];
[[self easPrinter].outputStream setDelegate:self];
[[self easPrinter].outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[self easPrinter].outputStream open];
}
}
}
}
FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"connected: %s", lblnConnected ? "true" : "false"] UTF8String]);
FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"found: %s", lblnFound ? "true" : "false"] UTF8String]);
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [@"NSStreamDelegate" UTF8String]);
switch (streamEvent)
{
case NSStreamEventNone:
{
break;
}
case NSStreamEventOpenCompleted:
{
break;
}
case NSStreamEventHasBytesAvailable:
{
break;
}
case NSStreamEventHasSpaceAvailable:
{
FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [@"NSStreamEventHasSpaceAvailable" UTF8String]);
NSInteger len = [[self easPrinter].outputStream write:[[self datPrintData] bytes] maxLength:[[self datPrintData] length]];
FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"data len: %ld, returned len: %ld", [[self datPrintData] length], len] UTF8String]);
break;
}
case NSStreamEventErrorOccurred:
{
break;
}
case NSStreamEventEndEncountered:
{
break;
}
}
}
NSData *toNSDataByteArray(FREObject *ba)
{
FREByteArray byteArray;
FREAcquireByteArray(ba, &byteArray);
NSData *d = [NSData dataWithBytes:(void *)byteArray.bytes length:(NSUInteger)byteArray.length];
FREReleaseByteArray(ba);
return d;
}
@end
我的打印机连接良好,但委托方法 [- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent] 从未运行。我想可能是 currentRunLoop 问题,我尝试将其更改为 mainRunLoop,但仍然没有任何线索。请帮忙!谢谢先进。