Listening for Reader Events

Several events may be subscribed to for connected readers even prior to initializing a connection using the CardReaderController class. These are:

  • OnCardReaderConnected
  • OnCardReaderDisconnected
  • OnCardReaderConnectFailed
  • OnCardReaderError

Listeners can therefore be used to handle unanticipated reader disconnects and facilitate asynchronous handling of reader connection logic (eg. not blocking code execution while waiting for a card reader connection)

The following represents a means of subscribing anonymous listeners to the various events that CardReaderController recognizes:

final CardReaderController cardReaderController = CardReaderController.getControllerFor(BBPOSDevice.class);
    
cardReaderController.subscribeOnCardReaderConnected(new GenericEventListenerWithParam<CardReader>() {
    @Override
    public void onEvent(CardReader deviceInfo) {
        if ( deviceInfo == null )
            addText("Unknown device connected");
        else
            addText("Device connected " + deviceInfo.getModelDisplayName());
    }
});

cardReaderController.subscribeOnCardReaderDisconnected(new GenericEventListener() {
    @Override
    public void onEvent() {
        addText("Device disconnected");
    }
});

cardReaderController.subscribeOnCardReaderConnectFailed(new MeaningfulErrorListener() {
    @Override
    public void onError(MeaningfulError error) {
        addText("Device connect failed: " + error.toString());
    }
});

cardReaderController.subscribeOnCardReaderError(new MeaningfulErrorListener() {
    @Override
    public void onError(MeaningfulError error) {
        addText("Device error: " + error.toString());
    }
});
id c = [[ANPCardReaderController sharedController] getBBDeviceControllerInstance];
                
[[ANPCardReaderController sharedController] subscribeOnCardReaderConnected:^(AnyPayCardReader * _Nullable cardReader) {
		NSLog(@"OnCardReaderConnected --> %@", cardReader.productID);
		[self appendText:[NSString stringWithFormat:@"\n\n OnCardReaderConnected --- %@", [cardReader name]]];
}];

[[ANPCardReaderController sharedController] subscribeOnCardReaderError:^(ANPMeaningfulError * _Nullable error) {
		NSLog(@"OnCardReaderError --> %@", error);

		[self appendText:[NSString stringWithFormat:@"\n\nOnCardReaderError --- %@", error.message]];
}];

[[ANPCardReaderController sharedController] subscribeOnCardReaderDisConnected:^{
		NSLog(@"OnCardReaderDisConnected ");

		[self appendText:@"\n\n OnCardReaderDisConnected "];
}];

[[ANPCardReaderController sharedController] subscribeOnCardReaderConnectionFailed:^(ANPMeaningfulError * _Nullable error) {
		NSLog(@"OnCardReaderConnectionFailed --> %@", error);

		[self appendText:[NSString stringWithFormat:@"\n\n OnCardReaderConnectionFailed ----%@", error.message]];
}];