Hello RFID Application

RFID SDK For Android 2.0.3.148

Overview

This guide provides a walk-through of the steps for creating an application that uses RFID3 API to perform RFID operations

Note: The demo app in this guide is intended for tutorial purposes only and should not be used in production environments.


Prerequisites

  1. Developer machine with latest Android studio

  2. Developer machine with Zebra Android device with WLAN connectivity

  3. Zebra Android device with developer mode turned on

  4. Reader regulatory is configured as per norms using FXR Console application

Create The Project

Start by creating a new project in Android Studio. Call it Hello RFID to match later references in this guide. For help, see the Android Studio tutorial.

Adding essentials

Modify the application's MainActivity.java file to use the RFID SDK library.

  1. Declare RFID readers fields:

    
        private static Readers readers;`
        private static RFIDReader reader;
        private static String TAG = "DEMO";
    
  2. Create SDK Readers instance
    
            // SDK
            if (readers == null) {
                reader = new new RFIDReader(hostName,ZIOTCport, timeoutMilliSeconds, "ZIOTC", "ZIOTC");
    			mReader.setPassword(password);
    			mReader.secureConnection(false);
    			mReader.connect();
    
            }
    
  3. Add ConfigureReader method to configure the reader for trigger type and adding event handler / listener addEventsListener
  4. 
         private void ConfigureReader() {
     	if (reader.isConnected()) {
    	            TriggerInfo triggerInfo = new TriggerInfo();
    	            triggerInfo.StartTrigger.setTriggerType(START_TRIGGER_TYPE.START_TRIGGER_TYPE_IMMEDIATE);
    	            triggerInfo.StopTrigger.setTriggerType(STOP_TRIGGER_TYPE.STOP_TRIGGER_TYPE_IMMEDIATE);	
    	            try {
    	                // receive events from reader
    	                if (eventHandler == null)
    	                    eventHandler = new EventHandler();
    	                    
    	                reader.Events.addEventsListener(eventHandler);
    	                reader.Events.setInventoryStartEvent(true);
    	                reader.Events.setInventoryStopEvent(true);
    	                // tag event with tag data
    	                reader.Events.setTagReadEvent(true);
    	                // application will collect tag using getReadTags API
    	                reader.Events.setAttachTagDataWithReadEvent(false);
    	                
    	            } catch (InvalidUsageException e) {
    	                e.printStackTrace();
    	            } catch (OperationFailureException e) {
    	                e.printStackTrace();
    
        
  5. Add EventHandler class to handle Reader operation events - eventReadNotify and Reader status events - eventStatusNotify
    
    	    // Read/Status Notify handler
    	    // Implement the RfidEventsListener class to receive event notifications
    	    public class EventHandler implements RfidEventsListener {
    	        // Read Event Notification
    	        public void eventReadNotify(RfidReadEvents e) {
    	            TagData[] myTags = reader.Actions.getReadTags(100);
    	            if (myTags != null) {
    	                for (int index = 0; index < myTags.length; index++) {
    	                    Log.d(TAG, "Tag ID " + myTags[index].getTagID());
    	                }
    	            }
    	        }
    	
    	        // Status Event Notification
    	        public void eventStatusNotify(RfidStatusEvents rfidStatusEvents) {
                Log.d(TAG, "Status Notification: " + rfidStatusEvents.StatusEventData.getStatusEventType());     
    	        }
            }
    
  6. Assign android:id to - TextView to print some useful information on screen
    
    
    
    
    
  7. Add EventHandler class to handle Reader operation events - eventReadNotify and Reader status events - eventStatusNotify

    
        // Read/Status Notify handler
        // Implement the RfidEventsListener class to receive event notifications
        public class EventHandler implements RfidEventsListener {
            // Read Event Notification
            public void eventReadNotify(RfidReadEvents e) {
                TagData[] myTags = reader.Actions.getReadTags(100);
                if (myTags != null) {
                    for (int index = 0; index < myTags.length; index++) {
                        Log.d(TAG, "Tag ID " + myTags[index].getTagID());
                    }
                }
            }
    
            // Status Event Notification
            public void eventStatusNotify(RfidStatusEvents rfidStatusEvents) {
                Log.d(TAG, "Status Notification: " + rfidStatusEvents.StatusEventData.getStatusEventType());
                if (rfidStatusEvents.StatusEventData.getStatusEventType() == STATUS_EVENT_TYPE.HANDHELD_TRIGGER_EVENT) {
                    if (rfidStatusEvents.StatusEventData.HandheldTriggerEventData.getHandheldEvent() == HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_PRESSED) {
    
                    }
                    if (rfidStatusEvents.StatusEventData.HandheldTriggerEventData.getHandheldEvent() == HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_RELEASED) {
    
                    }
                }
            }
        }
        
    
  8. Assign android:id to - TextView to print some useful information on screen

  9. 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TagText"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    

Running the application

Connection with device

  1. Connect MC3300R or TC20+RFD2000 or any android terminal with PC over USB

  2. Make sure project is already built successfully

  3. Click on Run app (shift+F10) from top right corner

  4. Now Application should be launched on Zebra Android device fine

  5. 5.4. Application should be showing Reader connected on screen

  6. img

Programming tips

  1. At time of exit, application shound disconnect with reader using Disconnect API and free up SDK instance. Refer MainActivity OnDestroy method OnDestroy method

  2. In case of failures or exceptions refer messages returned by API call to get more details

What's Next

  1. Add code to show the tag reporting unique count and total tag count

  2. Add code to configure various reader parameters e.g. Antenna power level and Singulation control to change opertional behavior as per need

  3. Refer various guides Guides and RFID demo application source for various features

Application MainActivity full source


					package com.zebra.demorfid.hellorfid;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.zebra.rfid.api3.*;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static ReaderDevice readerDevice;
    private static RFIDReader reader;
    private static String TAG = "DEMO";
    TextView textView;
    private EventHandler eventHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        //
        // UI
        textView = (TextView) findViewById(R.id.TagText);
        // SDK
        if (reader == null) {
1.	            
2.	   reader = new new RFIDReader(hostName,ZIOTCport, timeoutMilliSeconds, "ZIOTC", "ZIOTC");
3.	mReader.setPassword(password);
mReader.secureConnection(false);
4.	mReader.connect();
reader = new RFIDReader(hostname, port, timeout);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void ConfigureReader() {
    if (reader.isConnected()) {
            TriggerInfo triggerInfo = new TriggerInfo();
            triggerInfo.StartTrigger.setTriggerType(START_TRIGGER_TYPE.START_TRIGGER_TYPE_IMMEDIATE);
            triggerInfo.StopTrigger.setTriggerType(STOP_TRIGGER_TYPE.STOP_TRIGGER_TYPE_IMMEDIATE);
            try {
                // receive events from reader
                if (eventHandler == null)
                    eventHandler = new EventHandler();
                reader.Events.addEventsListener(eventHandler);
                reader.Events.setInventoryStartEvent(true);
                reader.Events.setInventoryStopEvent(true);
                // tag event with tag data
                reader.Events.setTagReadEvent(true);
                // application will collect tag using getReadTags API
                reader.Events.setAttachTagDataWithReadEvent(false);
                
            } catch (InvalidUsageException e) {
                e.printStackTrace();
            } catch (OperationFailureException e) {
                e.printStackTrace();
            }
        }
   }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (reader != null) {
                reader.Events.removeEventsListener(eventHandler);
                reader.disconnect();
                Toast.makeText(getApplicationContext(), "Disconnecting reader", Toast.LENGTH_LONG).show();
                reader = null;
                readers.Dispose();
                readers = null;
            }
        } catch (InvalidUsageException e) {
            e.printStackTrace();
        } catch (OperationFailureException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Read/Status Notify handler
    // Implement the RfidEventsLister class to receive event notifications
    public class EventHandler implements RfidEventsListener {
        // Read Event Notification
        public void eventReadNotify(RfidReadEvents e) {
            // Recommended to use new method getReadTagsEx for better performance in case of large tag population
            TagData[] myTags = reader.Actions.getReadTags(100);
            if (myTags != null) {
                for (int index = 0; index < myTags.length; index++) {
                    Log.d(TAG, "Tag ID " + myTags[index].getTagID());
                    if (myTags[index].getOpCode() == ACCESS_OPERATION_CODE.ACCESS_OPERATION_READ &&
                            myTags[index].getOpStatus() == ACCESS_OPERATION_STATUS.ACCESS_SUCCESS) {
                        if (myTags[index].getMemoryBankData().length() > 0) {
                            Log.d(TAG, " Mem Bank Data " + myTags[index].getMemoryBankData());
                        }
                    }
                }
            }
        }

        // Status Event Notification
        public void eventStatusNotify(RfidStatusEvents rfidStatusEvents) {
            Log.d(TAG, "Status Notification: " + rfidStatusEvents.StatusEventData.getStatusEventType());
        }
    }
}