2016-04-20 1 views
2

Ich erwarte, dass ich ein react native bridge module, Threading, Delegat oder Lifetime-Problem habe, das ich nicht verstehe, das verhindert, dass Delegiertenmethodenaufrufe empfangen werden.EADemo in React Native Module empfängt keine Delegate-Methode HandleEvent NSStreamEventOpenCompleted?

Muss ich die NSStream scheduleInRunLoop Methoden ändern?

Ich versuche, eine react native iOS bridge module zu implementieren, um eine Bluetooth "Classic" (nicht BLE) External Accessory basierend auf Apple's EADemo example zu verbinden. EADemo funktioniert gut eigenständig.

Wenn ich EADSessionController openSession von der nativen Bridge-Methode aufrufen, wird die Methode handleEvent nie aufgerufen?

Ich erwarte handelte ein NSStreamEventOpenCompleted Ereignis sowohl für den input und die output zu erhalten. Es werden jedoch keine Ereignisse empfangen.

Datei: index.js

'use strict'; 
var RNBluetooth = require('react-native').NativeModules.RNBluetooth; 
var Bluetooth = { 
    connectTo(accessory, result) { 
    RNBluetooth.connectTo(accessory, result); 
    }, 
    }; 
    module.exports = Bluetooth; 

Datei: RNBluetooth.m

// open the external accesssory session from javascript 
RCT_EXPORT_METHOD(connectTo:(NSDictionary *)accessoryProperties 
        callback:(RCTResponseSenderBlock)callback) 
{ 
    // findAccessory returns the EAAccessory matching the accessoryProperties 
    EAAccessory * accessory = [self findAccessory:accessoryProperties]; 
    if(nil != accessory) { 
     NSLog(@"Connect to: {%@}", accessoryProperties[@"name"]); 
     NSLog(@"name: {%@}", accessory.name); 
     NSLog(@"serialNumber: {%@}", accessory.serialNumber); 
     NSLog(@"connectionID: {%d}", (int)accessory.connectionID); 
    } 

    // Singleton 
    EADSessionController * eaSessionController = [EADSessionController sharedController]; 
    [eaSessionController setupControllerForAccessory:accessory 
            withProtocolString:accessoryProperties[@"protocolStrings"]]; 
    [eaSessionController openSession]; 

    NSString *dummyResponseString = @"openSession"; 
    callback(@[dummyResponseString]); 
} 

Datei: EADSessionController.m

#import "EADSessionController.h" 

NSString *EADSessionDataReceivedNotification = @"EADSessionDataReceivedNotification"; 

@implementation EADSessionController 

@synthesize accessory = _accessory; 
@synthesize protocolString = _protocolString; 

#pragma mark Internal 

#pragma mark Public Methods 
+ (EADSessionController *)sharedController 
{ 
    static EADSessionController *sessionController = nil; 
    if (sessionController == nil) { 
     sessionController = [[EADSessionController alloc] init]; 
    } 
    return sessionController; 
} 

- (void)dealloc 
{ 
    [self closeSession]; 
    [self setupControllerForAccessory:nil withProtocolString:nil]; 
    [super dealloc]; 
} 

// initialize the accessory with the protocolString 
- (void)setupControllerForAccessory:(EAAccessory *)accessory withProtocolString:(NSString *)protocolString 
{ 
    [_accessory release]; 
    _accessory = [accessory retain]; 
    [_protocolString release]; 
    _protocolString = [protocolString copy]; 
} 

// open a session with the accessory and set up the input and output stream on the default run loop 
- (BOOL)openSession 
{ 
    [_accessory setDelegate:self]; 
    _session = [[EASession alloc] initWithAccessory:_accessory forProtocol:_protocolString]; 
    if (_session) 
    { 
     [[_session inputStream] setDelegate:self]; 
     [[_session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
     [[_session inputStream] open]; 

     [[_session outputStream] setDelegate:self]; 
     [[_session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
     [[_session outputStream] open]; 
    } 
    else 
    { 
     NSLog(@"creating session failed"); 
    } 
    return (_session != nil); 
} 

// close the session with the accessory. 
- (void)closeSession 
{ 
    [[_session inputStream] close]; 
    [[_session inputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [[_session inputStream] setDelegate:nil]; 
    [[_session outputStream] close]; 
    [[_session outputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [[_session outputStream] setDelegate:nil]; 
    [_session release]; 
    _session = nil; 
    [_writeData release]; 
    _writeData = nil; 
    [_readData release]; 
    _readData = nil; 
} 

#pragma mark EAAccessoryDelegate 
- (void)accessoryDidDisconnect:(EAAccessory *)accessory 
{ 
    // do something ... 
} 

#pragma mark NSStreamDelegateEventExtensions 

// handleEvent never gets called when session opened from react native bridge? 
// 
// asynchronous NSStream handleEvent method 
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode 
{ 
    switch (eventCode) { 
     case NSStreamEventNone: 
      break; 
     case NSStreamEventOpenCompleted: 
      break; 
     case NSStreamEventHasBytesAvailable: 
      [self _readData]; 
      break; 
     case NSStreamEventHasSpaceAvailable: 
      [self _writeData]; 
      break; 
     case NSStreamEventErrorOccurred: 
      break; 
     case NSStreamEventEndEncountered: 
      break; 
     default: 
      break; 
    } 
} 
@end 

Irgendwelche Tipps oder Anregungen sind viel apprec iated.

+1

Ich brauche EADemo reagieren native Modul. Können Sie mir den vollständigen Code des reaktiven nativen Moduls External Accessory zur Verfügung stellen? –

Antwort

3

Gelöst.

Fügen Sie diese auf RNBluetooth.m

// This seems to get NSStream handleEvents and the write command 
// running on the same thread with no contention problems 
// writing to and reading from the write buffer 
- (dispatch_queue_t)methodQueue 
{ 
    return dispatch_get_main_queue(); 
} 

Siehe Threading Abschnitt unter Native Modules

Nun ist die EADSessionController.m von the EADemo example aus dem nativen reagieren Brückenmodul ohne Probleme aufgerufen werden.