Connection Management

RFID SDK for Android 2.0.2.124

Overview

This guide provides steps to perform various connection related task using RFID3 API

Connect to an Handheld Reader

Connection is the first step to talk to an Handheld reader

  1. Import the package
  2. 
    import com.zebra.rfid.api3.*;
    
  3. Create instance of Readers class with passing activity context as first parameter and enum value SERVICE_SERIAL as second parameter
  4. 
    readers = new Readers(this, ENUM_TRANSPORT.SERVICE_SERIAL);
    

    Note : For RFD40XX USB connectivity create reader instance with

    
    readers = new Readers(this, ENUM_TRANSPORT.SERVICE_USB)
    
  5. The Readers class instance gives a list of all available/paired RFID readers with an Android device. Readers list is in the form of ReaderDevice class.
  6. 
    ArrayList readersListArray = readers.GetAvailableRFIDReaderList(); 
    ReaderDevice readerDevice = availableRFIDReaderList.get(0);
    
  7. ReaderDevice class includes instance of RFIDReader class; which is root class to interface and performing all operations with RFID reader.
  8. 
    // Establish connection to the Handheld Reader
    rfidReader.connect();
    

Connect to an Fixed Reader

Connection is the first step to talk to an Fixed reader

  1. Import the package
  2. 
    import com.zebra.rfid.api3.*;
    
  3. For fixed readers create instance of RFIDReader class with passing hostname(ip address) as first parameter and port number 5084 as second parameter and timeout in milliseconds as third paramater
  4. 
    reader = new RFIDReader(hostname, 5084, 1000);
    
  5. ReaderDevice class includes instance of RFIDReader class; which is root class to interface and performing all operations with RFID reader.
  6. 
    // Establish connection to the Fixed Reader
    rfidReader.connect();
    

Special Connection Handling Cases

In a normal scenario, the reader connects fine, but the following are the cases which require special handling at the time of connection. The following example demonstrates the connection is handled under try-catch block and OperationFailure exception is thrown by connection API is stored and used for further analysis


private OperationFailureException ex; 
try { 
    // Establish connection to the RFID Reader 
    rfidReader.connect(); 
} 
catch (InvalidUsageException e) 
{ 
    e.printStackTrace(); 
} 
catch (OperationFailureException e) 
{   
   e.printStackTrace(); 
   ex = e; 
}

Disconnect

When the application is done with the connection and operations on the RFID reader, it calls the following API to close the connection, and to release and clean up the resources.


// Disconnects reader and performs clean-up 
rfidReader.disconnect();
NOTE: If a reader disconnection occurs, the reader.isConnected() flag may return the value false. If application has called the reader.connect() then application should call reader.disconnect() regardless of the flag status

Detecting Connection Loss and Reconnecting

In order to detect connection loss the user should subscribe for Reader Disconnection event and implement RfidEventsListener class, the eventStatusNotify method of RfidEventsListener will notify the connection loss. The below code can be used for detecting connection loss and reconnecting.


public EventHandler eventHandler;
if (eventHandler == null)
    eventHandler = new EventHandler();
reader.Events.addEventsListener(eventHandler);
reader.Events.setTagReadEvent(true);
reader.Events.setReaderDisconnectEvent(true);

class EventHandler implements RfidEventsListener {
     public void eventReadNotify(RfidReadEvents e) {
        TagData[] tag = reader.Actions.getReadTags(200);
    }
    public void eventStatusNotify(RfidStatusEvents rfidStatusEvents) {
        if(rfidStatusEvents.StatusEventData.getStatusEventType() == DISCONNECTION_EVENT){
            try {
                reader.disconnect();
                reader.connect();
                if (reader.isConnected()) {
                    if (eventHandler == null)
                        eventHandler = new EventHandler();

                    reader.Events.addEventsListener(eventHandler);
                    reader.Events.setTagReadEvent(true);
                    reader.Events.setReaderDisconnectEvent(true);
                }
                reader.Actions.Inventory.perform();
            } catch (OperationFailureException | InvalidUsageException e) {
                     e.printStackTrace();
            }
        }
    }
}
                                                        

Dispose

When the application main activity is destroyed, it is required to dispose the SDK instance so that it can cleanly exit (unregistration and unbind by SDK as required).


readers.Dispose();