Read multiple NFC cards from android (SERIALPORT)

0

Hello everyone, I'm making an application on android to read NFC cards and so far all the problem is that when I want to read more than one I just can not, I'm making a serial connection with the library UsbSerialForAndroid so far I read the data flow that the antenna gives me when detecting a TAG / CARD but according to this article the NFC antennas make a connection for each card they read and it happens to me just what it says, that being a card closer to the antenna or obstructing the passage of another the antenna will only read that one. Does anyone have any idea how to indicate to the antenna that if I read the TAG / CARD go to the next one? I can make the app only show the TAG / CARDS that I have not read but in this case I use the antenna. Any comment will be of great help, thank you!

This is the way in which the new data is received from the serial.

private final SerialInputOutputManager.Listener mListener =
        new SerialInputOutputManager.Listener() {

    @Override
    public void onRunError(Exception e) {
        Log.d(TAG, "Runner stopped.");
    }

    @Override
    public void onNewData(final byte[] data) {
        SerialConsoleActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SerialConsoleActivity.this.updateReceivedData(data);
            }
        });
    }
};

and this is the class that contains the above metdo

public class SerialInputOutputManager implements Runnable {

private static final String TAG = SerialInputOutputManager.class.getSimpleName();
private static final boolean DEBUG = true;

private static final int READ_WAIT_MILLIS = 200;
private static final int BUFSIZ = 4096;

private final UsbSerialPort mDriver;

private final ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ);

// Synchronized by 'mWriteBuffer'
private final ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);

private enum State {
    STOPPED,
    RUNNING,
    STOPPING
}

// Synchronized by 'this'
private State mState = State.STOPPED;

// Synchronized by 'this'
private Listener mListener;

public interface Listener {
    /**
     * Called when new incoming data is available.
     */
    public void onNewData(byte[] data);

    /**
     * Called when {@link SerialInputOutputManager#run()} aborts due to an
     * error.
     */
    public void onRunError(Exception e);
}

/**
 * Creates a new instance with no listener.
 */
public SerialInputOutputManager(UsbSerialPort driver) {
    this(driver, null);
}

/**
 * Creates a new instance with the provided listener.
 */
public SerialInputOutputManager(UsbSerialPort driver, Listener listener) {
    mDriver = driver;
    mListener = listener;
}

public synchronized void setListener(Listener listener) {
    mListener = listener;
}

public synchronized Listener getListener() {
    return mListener;
}

public void writeAsync(byte[] data) {
    synchronized (mWriteBuffer) {
        mWriteBuffer.put(data);
    }
}

public synchronized void stop() {
    if (getState() == State.RUNNING) {
        Log.i(TAG, "Stop requested");
        mState = State.STOPPING;
    }
}

private synchronized State getState() {
    return mState;
}

/**
 * Continuously services the read and write buffers until {@link #stop()} is
 * called, or until a driver exception is raised.
 *
 * NOTE(mikey): Uses inefficient read/write-with-timeout.
 * TODO(mikey): Read asynchronously with {@link UsbRequest#queue(ByteBuffer, int)}
 */
@Override
public void run() {
    synchronized (this) {
        if (getState() != State.STOPPED) {
            throw new IllegalStateException("Already running.");
        }
        mState = State.RUNNING;
    }

    Log.i(TAG, "Running ..");
    try {
        while (true) {
            if (getState() != State.RUNNING) {
                Log.i(TAG, "Stopping mState=" + getState());
                break;
            }
            step();
        }
    } catch (Exception e) {
        Log.w(TAG, "Run ending due to exception: " + e.getMessage(), e);
        final Listener listener = getListener();
        if (listener != null) {
          listener.onRunError(e);
        }
    } finally {
        synchronized (this) {
            mState = State.STOPPED;
            Log.i(TAG, "Stopped.");
        }
    }
}

private void step() throws IOException {
    // Handle incoming data.
    int len = mDriver.read(mReadBuffer.array(), READ_WAIT_MILLIS);
    if (len > 0) {
        if (DEBUG) Log.d(TAG, "Read data len=" + len);
        final Listener listener = getListener();
        if (listener != null) {
            final byte[] data = new byte[len];
            mReadBuffer.get(data, 0, len) ;
            listener.onNewData(data);
        }
        mReadBuffer.clear();
    }

    // Handle outgoing data.
    byte[] outBuff = null;
    synchronized (mWriteBuffer) {
        len = mWriteBuffer.position();
        if (len > 0) {
            outBuff = new byte[len];
            mWriteBuffer.rewind();
            mWriteBuffer.get(outBuff, 0, len);
            mWriteBuffer.clear();
        }
    }
    if (outBuff != null) {
        if (DEBUG) {
            Log.d(TAG, "Writing data len=" + len);
        }
        mDriver.write(outBuff, READ_WAIT_MILLIS);
    }
}

}

    
asked by Enrique Perez 26.04.2018 в 01:09
source

0 answers