3 Replies Latest reply on May 28, 2018 7:19 AM by Marwin Rott

    RFD2000 trigger event blocked by TCP connection?

    Marwin Rott

      Good morning,

       

      I'm currently developing a relatively simple RFID reader app for the TC20/RFD2000 using the RFID API3.

      Its point is to scan RFIDs and send the tag data to a server application in JSON format via a simple TCP socket.

       

      Now, for some reason I can't figure out, once I run the connection task that creates my TCP client instance and listens for server response, the trigger of the RFD2000 stops working. Checking Logcat I see that instead of the usual

      05-25 09:06:58.444 13429-13447/com.saco.mrott.rfidapp I/Android SDK API: Notification_TriggerEvent TRIGGER_PRESS
          Notification_TriggerEvent TRIGGER_RELEASE
      

      I get a lot of

      05-25 09:06:54.663 13429-13429/com.saco.mrott.rfidapp D/RFIDAPI3: broadcast: TRIGGERScom.symbol.button.R1
      05-25 09:06:54.900 13429-13429/com.saco.mrott.rfidapp D/RFIDAPI3: broadcast: TRIGGERScom.symbol.button.R1
      05-25 09:06:55.155 13429-13429/com.saco.mrott.rfidapp D/RFIDAPI3: broadcast: TRIGGERScom.symbol.button.R1
      05-25 09:06:56.152 13429-13429/com.saco.mrott.rfidapp D/RFIDAPI3: broadcast: TRIGGERScom.symbol.button.R1
      05-25 09:06:56.349 13429-13429/com.saco.mrott.rfidapp D/RFIDAPI3: broadcast: TRIGGERScom.symbol.button.R1
      

      instead.

      I'm relatively new to android development, but from what I gather it would appear that the notification event doesn't fire as it's supposed to.

      I have no idea why this is, the TCPclient class I wrote has literally nothing to do with the API. What am I doing wrong?

       

      Here's the class:

      package com.saco.mrott.rfidapp.home.classes;
      
      import com.saco.mrott.rfidapp.home.application.Interfaces;
      
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.io.PrintWriter;
      import java.net.InetAddress;
      import java.net.Socket;
      import java.util.List;
      
      /**
       * Very basic TCP client class that connects to a server on a specified port (port=6666 for testing,
       * we will see what port we later need to use) and is able to send and receive messages.
      
       */
      
      public class TCPclient {
         public static final String SERVER_IP = "192.168.199.103";
        public static final int SERVER_PORT = 6666;
      
        private String mServerMessage;
        private Interfaces.OnMessageReceived mMessageListener = null;
      
        private Boolean mRun = false;
      
        private PrintWriter mBufferOut;
        private BufferedReader mBufferIn;
      
        public TCPclient (Interfaces.OnMessageReceived listener){
         mMessageListener = listener;
         }
      
         public void sendMessage (final String message){
        Runnable runnable = new Runnable() {
         @Override
         public void run() {
         if (mBufferOut != null) {
         mBufferOut.println(message + "\r\n");
         mBufferOut.flush();
         }
        }
        };
         Thread thread = new Thread(runnable);
         thread.start();
         }
      
         public void stopClient() {
      
         mRun = false;
      
        if (mBufferOut != null) {
         mBufferOut.flush();
         mBufferOut.close();
         }
      
         mMessageListener = null;
         mBufferIn = null;
         mBufferOut = null;
         mServerMessage = null;
      
         }
      
         public void run() {
      
         mRun = true;
      
        try {
        InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
         Socket socket = new Socket(serverAddress, SERVER_PORT);
      
        try {
         mBufferOut = new PrintWriter(socket.getOutputStream());
         mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int charsRead = 0;
        char[] buffer = new char[1024];
      
        while (mRun) {
        charsRead = mBufferIn.read(buffer);
         mServerMessage = new String(buffer).substring(0, charsRead);
        if (mServerMessage != null && mMessageListener != null) {
         mMessageListener.messageReceived(mServerMessage);
         }
         mServerMessage = null;
         }
        } catch (Exception e) {
         // handle this somehow
         } finally {
        socket.close();
         }
        } catch (Exception e) {
         // handle this somehow
         }
        }
      }
      
      

      and the AsyncTask that handles the connection:

      public class ConnectTask extends AsyncTask<String, String, TCPclient> {
      
         @Override
         protected TCPclient doInBackground (String... message) {
         mClient = new TCPclient(new Interfaces.OnMessageReceived() {
         @Override
         public void messageReceived(String message) {
        publishProgress(message);
         }
        });
      
         mClient.run();
      
        return null;
         }
      
         @Override
         protected void onProgressUpdate (String... values) {
         super.onProgressUpdate(values);
      
         // handle server response here
      
      
         }
      
         @Override
         protected void onCancelled() {
         super.onCancelled();
      
         mClient.stopClient();
         }
      
      }
      
        • Re: RFD2000 trigger event blocked by TCP connection?
          Marwin Rott

          If I run the ConnectTask on a button press and cancel it afterwards (still within the OnClickListener), I get some weird behavior:

          Clicking the button once will successfully send whatever message I want to the server, but the trigger will still be blocked.

          Clicking it again will seemingly free up the trigger again.

          Generally, the trigger will only work again if I set the instance of the TCPclient class that the task creates to null, calling the stopClient() function is not enough.

           

          I deduce from this that the problem seems to be the instance of that class existing, but why this would block trigger event notifications I have absolutely no idea.

           

          Anyone able to help? This is a blocker for me, I don't know what to do.

          • Re: RFD2000 trigger event blocked by TCP connection?
            Marwin Rott

            Further testing seems to confirm this.

            I created a barebones project with just the absolutely necessary bits of code in it:

            The API itself as well as connection to the reader, basic setup to allow listening for trigger presses and basic textViews as status output,

            added to this only the TCPclient class shown above and the connection task used to run the client.

            As soon as the client is instantiated, trigger events do not register any more. So it's definitely the client and I can rule out any other interactions within my main app. I still do not understand why this happens, however.

            • Re: RFD2000 trigger event blocked by TCP connection?
              Marwin Rott

              Nevermind, I found the issue myself ...

               

              Executing the ConnectTask on THREAD_POOL_EXECUTOR fixes it. The more you know ...