androidmultithreadinghandlerrfidcardreader

How to display a single value in android Threads


In android , i create a application based on RFID Card Reader on mobile. While i tapping Card on RFID Reader it generates same value at many times until the card untapped from the reader.

I want to show only one value per tapping card on RFID reader. Here i place my code and Sample snapshot of my application.

Guide me and tell solution for my problem.

public static MediaPlayer mp;
FT_Device ftDev = null;
int DevCount = -1;
int currentIndex = -1;
int openIndex = 0;

/*graphical objects*/
EditText readText;
Button readEnButton;
static int iEnableReadFlag = 1;

/*local variables*/
int baudRate; /*baud rate*/
byte stopBit; /*1:1stop bits, 2:2 stop bits*/
byte dataBit; /*8:8bit, 7: 7bit*/
byte parity;  /* 0: none, 1: odd, 2: even, 3: mark, 4: space*/
byte flowControl; /*0:none, 1: flow control(CTS,RTS)*/
int portNumber; /*port number*/
long wait_sec=3000;
byte[] readData;    //similar to data.
public static final int readLength = 1024;  // changed length from 512
public int iavailable = 0;

char[] readDataToText;
//char readDataToTextSudha;
public boolean bReadThreadGoing = false;
public readThread read_thread;
public static D2xxManager ftD2xx = null;
boolean uart_configured = false;

// Empty Constructor
public MainActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    readData = new byte[readLength];
    readDataToText = new char[readLength];
    //readDataToTextSudha = new char;
    readText = (EditText) findViewById(R.id.ReadValues);
    readText.setInputType(0);
    readEnButton = (Button) findViewById(R.id.readEnButton);
    baudRate = 9600;
    /* default is stop bit 1 */
    stopBit = 1;
    /* default data bit is 8 bit */
    dataBit = 8;
    /* default is none */
    parity = 0;
    /* default flow control is is none */
    flowControl = 0;
    portNumber = 1;

    try {
        ftD2xx = D2xxManager.getInstance(this);
    } catch (D2xxManager.D2xxException ex) {
        ex.printStackTrace();
    }
    //Opening Coding

    if (DevCount <= 0) {
        createDeviceList();
    } else {
        connectFunction();
    }

    //Configuration coding

    if (DevCount <= 0 || ftDev == null) {
        Toast.makeText(getApplicationContext(), "Device not open yet...", Toast.LENGTH_SHORT).show();
    } else {
        SetConfig(baudRate, dataBit, stopBit, parity, flowControl);
    }

    readEnButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (DevCount <= 0 || ftDev == null) {
                Toast.makeText(getApplicationContext(), "Device not open yet...", Toast.LENGTH_SHORT).show();
            } else if (uart_configured == false) {
                Toast.makeText(getApplicationContext(), "UART not configure yet...", Toast.LENGTH_SHORT).show();
                return;
            } else {
                EnableRead();
            }
        }
    });

}

public void EnableRead() {
    iEnableReadFlag = (iEnableReadFlag + 1) % 2;
    if (iEnableReadFlag == 1) {
        ftDev.purge((byte) (D2xxManager.FT_PURGE_TX));
        ftDev.restartInTask();
        readEnButton.setText("Read Enabled");
        Toast.makeText(getApplicationContext(),"Read Enabled",Toast.LENGTH_LONG).show();

    } else {
        ftDev.stopInTask();
        readEnButton.setText("Read Disabled");
        Toast.makeText(getApplicationContext(),"Read Disabled",Toast.LENGTH_LONG).show();
    }
}

public void createDeviceList() {
    int tempDevCount = ftD2xx.createDeviceInfoList(getApplicationContext());

    if (tempDevCount > 0) {
        if (DevCount != tempDevCount) {
            DevCount = tempDevCount;
            updatePortNumberSelector();
        }
    } else {
        DevCount = -1;
        currentIndex = -1;
    }
};

public void disconnectFunction() {
    DevCount = -1;
    currentIndex = -1;
    bReadThreadGoing = false;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if (ftDev != null) {
        synchronized (ftDev) {
            if (true == ftDev.isOpen()) {
                ftDev.close();
            }
        }
    }
}

public void connectFunction() {
    int tmpProtNumber = openIndex + 1;

    if (currentIndex != openIndex) {
        if (null == ftDev) {
            ftDev = ftD2xx.openByIndex(getApplicationContext(), openIndex);
        } else {
            synchronized (ftDev) {
                ftDev = ftD2xx.openByIndex(getApplicationContext(), openIndex);
            }
        }
        uart_configured = false;
    } else {
        Toast.makeText(getApplicationContext(), "Device port " + tmpProtNumber + " is already opened", Toast.LENGTH_LONG).show();
        return;
    }

    if (ftDev == null) {
        Toast.makeText(getApplicationContext(), "open device port(" + tmpProtNumber + ") NG, ftDev == null", Toast.LENGTH_LONG).show();
        return;
    }

    if (true == ftDev.isOpen()) {
        currentIndex = openIndex;
        Toast.makeText(getApplicationContext(), "open device port(" + tmpProtNumber + ") OK", Toast.LENGTH_SHORT).show();

        if (false == bReadThreadGoing) {
            read_thread = new readThread(handler);
            read_thread.start();
            bReadThreadGoing = true;
        }
    } else {
        Toast.makeText(getApplicationContext(), "open device port(" + tmpProtNumber + ") NG", Toast.LENGTH_LONG).show();
    }
}

public void updatePortNumberSelector() {
    if (DevCount == 2) {
        Toast.makeText(getApplicationContext(), "2 port device attached", Toast.LENGTH_SHORT).show();

    } else if (DevCount == 4) {

        Toast.makeText(getApplicationContext(), "4 port device attached", Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(getApplicationContext(), "1 port device attached", Toast.LENGTH_SHORT).show();
    }
}

public void SetConfig(int baud, byte dataBits, byte stopBits, byte parity, byte flowControl) {
    if (ftDev.isOpen() == false) {
        Log.e("j2xx", "SetConfig: device not open");
        return;
    }

    ftDev.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
    ftDev.setBaudRate(baud);


    ftDev.setDataCharacteristics(dataBits, stopBits, parity);

    uart_configured = true;
    Toast.makeText(getApplicationContext(), "Config done", Toast.LENGTH_SHORT).show();
}


final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (iavailable > 0) {

            mp = MediaPlayer.create(MainActivity.this, R.raw.beep);
            mp.start();
            readText.append(String.copyValueOf(readDataToText, 0, iavailable));
        }
    }
};

private class readThread extends Thread {
    Handler mHandler;

    readThread(Handler h) {
        mHandler = h;
        this.setPriority(Thread.MIN_PRIORITY);
    }

    @Override
    public void run() {
        int i;

        while (true == bReadThreadGoing) {
            try {
                Thread.sleep(1000); //changed
            }   catch (InterruptedException e) {
            }

            synchronized (ftDev) {
                iavailable = ftDev.getQueueStatus();
                if (iavailable > 0) {

                    if (iavailable > readLength) {
                        iavailable = readLength;
                    }
                    ftDev.read(readData, iavailable,wait_sec);
                    for (i = 0; i < iavailable; i++) {
                       readDataToText[i] = (char) readData[i];

                    }
                    Message msg = mHandler.obtainMessage();
                    mHandler.sendMessage(msg);
                }
            }
        }
    }

}

@Override
public void onResume() {
    super.onResume();
    DevCount = 0;
    createDeviceList();
    if (DevCount > 0) {
        connectFunction();
        SetConfig(baudRate, dataBit, stopBit, parity, flowControl);
    }
}
}

My problem would snapped here

Getting Value continuously from Reader

I want this type of value when i tap the card

If i tap some other cards, old card value replaces from the new one.

Guide me. Thanks in Advance


Solution

  • I got the answer by using of Threads. When the card tapping it get some values after sleep of one or two seconds only the next value have to get from the reader.

     public void run() {
            int i;
            while (true == bReadThreadGoing) {  // Means Make sure , getting value from Reader
                try {
                    Thread.sleep(1000);         // Wait for a second to get another value.
                    clearText();                //clear the old value and get new value. 
                }   catch (InterruptedException e) {
                }
    

    And clearing the edittext by using this command.

          public void clearText() {
        runOnUiThread(new Runnable() {
            public void run() {
              readText.setText("");
            }
        });
    }