How to Understand Java API Usage for Contactless CPU Cards: Difference between revisions

From wizarPOS
No edit summary
No edit summary
Line 1: Line 1:
* Get RFCardReaderDevice
== Overview ==
This section provides a step-by-step guide on using the Java API for interacting with contactless CPU cards.
== Steps for Java API Usage ==
* '''Get RFCardReaderDevice:'''
Retrieve the instance of the RFCardReaderDevice to initiate communication with the contactless card reader.
<syntaxhighlight lang="java" line='line'>
<syntaxhighlight lang="java" line='line'>
   device = (RFCardReaderDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_RF_CARD_READER);
   device = (RFCardReaderDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_RF_CARD_READER);
</syntaxhighlight>
</syntaxhighlight>
* Open device
* '''Open Device:'''
Execute the command to open the card reader device. This step establishes a connection between the Java application and the card reader.
<syntaxhighlight lang="java" line='line'>
<syntaxhighlight lang="java" line='line'>
   device.open();
   device.open();
</syntaxhighlight>
</syntaxhighlight>
* Search Card
* '''Search for Card:'''
Initiate a search for the contactless CPU card. This involves scanning for available cards within the reader's range.
<syntaxhighlight lang="java" line='line'>
<syntaxhighlight lang="java" line='line'>
   OperationListener listener = new OperationListener() {
   OperationListener listener = new OperationListener() {
Line 22: Line 28:
   device.listenForCardPresent(listener, TimeConstants.FOREVER); The result will be returned in the callback listener.  
   device.listenForCardPresent(listener, TimeConstants.FOREVER); The result will be returned in the callback listener.  
</syntaxhighlight>
</syntaxhighlight>
* Communicate with Card
* '''Communicate with Card:'''
Once the card is detected, establish communication. This step may involve reading from or writing to the card, depending on the application's requirements.
<syntaxhighlight lang="java" line='line'>   
<syntaxhighlight lang="java" line='line'>   
   if (rfCard instanceof CPUCard) {
   if (rfCard instanceof CPUCard) {
Line 31: Line 38:
   }
   }
</syntaxhighlight>
</syntaxhighlight>
* Close device
* '''Close Device:'''
After the communication with the card is complete, close the device to end the session. This step is crucial for maintaining device security and integrity.
<syntaxhighlight lang="java" line='line'>
<syntaxhighlight lang="java" line='line'>
   device.close();
   device.close();
</syntaxhighlight>
</syntaxhighlight>
== Important Notes ==
* Each step should be performed in sequence to ensure successful communication with the contactless CPU card.
* Handle exceptions and errors appropriately to maintain the stability of your application.

Revision as of 19:54, 6 January 2024

Overview

This section provides a step-by-step guide on using the Java API for interacting with contactless CPU cards.

Steps for Java API Usage

  • Get RFCardReaderDevice:

Retrieve the instance of the RFCardReaderDevice to initiate communication with the contactless card reader.

  device = (RFCardReaderDevice) POSTerminal.getInstance(mContext).getDevice(POSTerminal.DEVICE_NAME_RF_CARD_READER);
  • Open Device:

Execute the command to open the card reader device. This step establishes a connection between the Java application and the card reader.

  device.open();
  • Search for Card:

Initiate a search for the contactless CPU card. This involves scanning for available cards within the reader's range.

  OperationListener listener = new OperationListener() {
    @Override
    public void handleResult(OperationResult arg0) {
      if (arg0.getResultCode() == OperationResult.SUCCESS) {
        sendSuccessLog2(mContext.getString(R.string.find_card_succeed));
        rfCard = ((RFCardReaderOperationResult) arg0).getCard();
      } else {
        sendFailedLog2(mContext.getString(R.string.find_card_failed));
      }
    }
  };
  device.listenForCardPresent(listener, TimeConstants.FOREVER); The result will be returned in the callback listener.
  • Communicate with Card:

Once the card is detected, establish communication. This step may involve reading from or writing to the card, depending on the application's requirements.

  
  if (rfCard instanceof CPUCard) {
    CPUCard cpucard = ((CPUCard) rfCard);
    ATR atr = cpucard .connect();
    result = cpucard .transmit(arryAPDU, 0);
    cpucard .disconnect();
  }
  • Close Device:

After the communication with the card is complete, close the device to end the session. This step is crucial for maintaining device security and integrity.

  device.close();

Important Notes

  • Each step should be performed in sequence to ensure successful communication with the contactless CPU card.
  • Handle exceptions and errors appropriately to maintain the stability of your application.