• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

iOS ANE NSStream delegate not called

New Here ,
Aug 21, 2015 Aug 21, 2015

Copy link to clipboard

Copied

Hi, I have successfully created connection to Apple MFi printer, but I'm failed to have NSOutputStream to write to the printer because delegate did not called.

Here is my LibPrint.h:-

  1. #import <Foundation/Foundation.h>
  2. #import <ExternalAccessory/ExternalAccessory.h>
  3. #import "FlashRuntimeExtensions.h"
  4. @Interface LibPrint : NSObject <NSStreamDelegate>
  5. - (void)printR;
  6. - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent;
  7. @End
  8. // C interface
  9. FREObject fncPrintR(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]);

LibPrint.m:-

  1. #import "LibPrint.h"
  2. @Interface LibPrint ()
  3. @property (nonatomic, strong) EASession *easPrinter;
  4. @property (nonatomic, strong) NSData *datPrintData;
  5. @End
  6. @implementation LibPrint
  7. FREContext eventContext;
  8. LibPrint *AirInAppRefToSelf;
  9. #pragma mark - NSObject
  10. -(id)init
  11. {
  12.     self = [super init];
  13.     if (self)
  14.     {
  15.         AirInAppRefToSelf = self;
  16.         self.easPrinter = nil;
  17.         self.datPrintData = nil;
  18.     }
  19.     return self;
  20. }
  21. -(void)dealloc
  22. {
  23.     self.easPrinter = nil;
  24.     self.datPrintData = nil;
  25.     AirInAppRefToSelf = nil;
  26. }
  27. #pragma mark - ANE setup
  28. void LibPrintExtContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
  29. {
  30.     *numFunctionsToTest = 1;
  31.     FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * *numFunctionsToTest);
  32.     func[0].name = (const uint8_t*) "printR";
  33.     func[0].functionData = NULL;
  34.     func[0].function = &fncPrintR;
  35.     *functionsToSet = func;
  36.     eventContext = ctx;
  37.     if ((LibPrint*) AirInAppRefToSelf == nil)
  38.     {
  39.         AirInAppRefToSelf = [[LibPrint alloc] init];
  40.     }
  41. }
  42. void LibPrintContextFinalizer(FREContext ctx) { }
  43. void LibPrintExtensionInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* ctxFinalizerToSet)
  44. {
  45.     *extDataToSet = NULL;
  46.     *ctxInitializerToSet = &LibPrintExtContextInitializer;
  47.     *ctxFinalizerToSet = &LibPrintContextFinalizer;
  48. }
  49. void LibPrintFinalizer(void* extData) { }
  50. #pragma mark - C interface
  51. FREObject fncPrintR(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
  52. {
  53.     FREObject result = NULL;

  54.     if ([AirInAppRefToSelf datPrintData] != nil) {
  55.         [AirInAppRefToSelf setDatPrintData:nil];
  56.     }
  57.     // Retrieve message
  58.     NSData *ldatPrintData = toNSDataByteArray(argv[0]);
  59.     [AirInAppRefToSelf setDatPrintData:ldatPrintData];
  60.     [(LibPrint*) AirInAppRefToSelf printR];
  61.    
  62.     return result;
  63. }
  64. - (void)printR
  65. {
  66.     BOOL lblnConnected = false;
  67.     BOOL lblnFound = false;
  68.     NSArray *accessories = [EAAccessoryManager sharedAccessoryManager].connectedAccessories;
  69.     for (EAAccessory *accessory in accessories)
  70.     {
  71.         if (accessory.connected)
  72.         {
  73.             lblnConnected = true;
  74.             for (NSString *protocol in accessory.protocolStrings)
  75.             {
  76.                 if ([protocol isEqualToString:@"jp.star-m.starpro"])
  77.                 {
  78.                     lblnFound = true;
  79.                     [self setEasPrinter:[[EASession alloc] initWithAccessory:accessory forProtocol:protocol]];
  80.                     [[self easPrinter].outputStream setDelegate:self];
  81.                     [[self easPrinter].outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  82.                     [[self easPrinter].outputStream open];
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"connected: %s", lblnConnected ? "true" : "false"]  UTF8String]);
  88.     FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"found: %s", lblnFound ? "true" : "false"]  UTF8String]);
  89. }
  90. - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
  91. {
  92.     FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [@"NSStreamDelegate"  UTF8String]);
  93.     switch (streamEvent)
  94.     {
  95.         case NSStreamEventNone:
  96.         {
  97.             break;
  98.         }
  99.         case NSStreamEventOpenCompleted:
  100.         {
  101.             break;
  102.         }
  103.         case NSStreamEventHasBytesAvailable:
  104.         {
  105.             break;
  106.         }
  107.         case NSStreamEventHasSpaceAvailable:
  108.         {
  109.             FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [@"NSStreamEventHasSpaceAvailable"  UTF8String]);
  110.             NSInteger len = [[self easPrinter].outputStream write:[[self datPrintData] bytes] maxLength:[[self datPrintData] length]];
  111.             FREDispatchStatusEventAsync(eventContext, (uint8_t*)"DEBUG_DATA", (uint8_t*) [[NSString stringWithFormat: @"data len: %ld, returned len: %ld", [[self datPrintData] length], len]  UTF8String]);
  112.             break;
  113.         }
  114.         case NSStreamEventErrorOccurred:
  115.         {
  116.             break;
  117.         }
  118.         case NSStreamEventEndEncountered:
  119.         {
  120.             break;
  121.         }
  122.     }
  123. }
  124. NSData *toNSDataByteArray(FREObject *ba)
  125. {
  126.     FREByteArray byteArray;
  127.     FREAcquireByteArray(ba, &byteArray);
  128.     NSData *d = [NSData dataWithBytes:(void *)byteArray.bytes length:(NSUInteger)byteArray.length];
  129.     FREReleaseByteArray(ba);
  130.     return d;
  131. }
  132. @End

I have printer connected good, but the delegate method [- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent] never run. I guess maybe

the currentRunLoop issue, I tried to change it to mainRunLoop, but still no clue. Please help, thanks in advanced!

TOPICS
Development

Views

587

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines