ACR1255U-J1阅读器身份验证

时间:2018-08-22 16:31:13

标签: android bluetooth

我正在使用ACR1255U-J1开发一个Android应用程序。我有一个TextView(用于显示标签的UID)和一个Button(用于连接ACR1255U-J1阅读器)。我的代码运行没有问题。

但是我不想单击“按钮”进行连接。所以我将Button单击的代码移动到onCreate。这是行不通的。请给我建议如何解决我的问题。

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.acs.bluetooth.Acr1255uj1Reader;
import com.acs.bluetooth.Acr3901us1Reader;
import com.acs.bluetooth.BluetoothReader;
import com.acs.bluetooth.BluetoothReaderGattCallback;
import com.acs.bluetooth.BluetoothReaderManager;

import com.acs.bluetooth.BluetoothReader.OnCardStatusChangeListener;
import com.acs.bluetooth.BluetoothReaderGattCallback.OnConnectionStateChangeListener;
import com.acs.bluetooth.BluetoothReaderManager.OnReaderDetectionListener;
import com.acs.bluetooth.BluetoothReader.OnResponseApduAvailableListener;
import com.acs.bluetooth.BluetoothReader.OnAuthenticationCompleteListener;

public class BTRActivity extends AppCompatActivity {

private String mDeviceAddress = "20:91:48:5B:52:44";
private static final byte[] AUTO_POLLING_START = { (byte) 0xE0, 0x00, 0x00, 0x40, 0x01 };
private int mConnectState = BluetoothReader.STATE_DISCONNECTED;

private static final String DEFAULT_1255_MASTER_KEY = "ACR1255U-J1 Auth";
private byte masterKey[] = {65, 67, 82, 49, 50, 53, 53, 85, 45, 74, 49, 32, 65, 117, 116, 104};
byte[] apdu = { (byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00  };

private BluetoothReader mBluetoothReader;
private BluetoothReaderGattCallback mGattCallback;
private BluetoothReaderManager mBluetoothReaderManager;
private BluetoothGatt mBluetoothGatt;

private TextView txtConnectionState;
private TextView txtDisplay;
private Button cmdAuthentication;


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

    findUiViews();
    setOnClickListener();

    mGattCallback = new BluetoothReaderGattCallback();
    mGattCallback.setOnConnectionStateChangeListener(new OnConnectionStateChangeListener() {
        @Override
        public void onConnectionStateChange(final BluetoothGatt gatt, final int state, final int newState) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (state != BluetoothGatt.GATT_SUCCESS) {
                        mConnectState = BluetoothReader.STATE_DISCONNECTED;

                        if (newState == BluetoothReader.STATE_CONNECTED) {
                            txtConnectionState.setText("Connect fail.");
                        } else if (newState == BluetoothReader.STATE_DISCONNECTED) {
                            txtConnectionState.setText("Disconnect fail.");
                        }
                        clearAllUi();
                        invalidateOptionsMenu();
                        return;
                    }

                    updateConnectionState(newState);

                    if (newState == BluetoothProfile.STATE_CONNECTED)
                    {
                        if (mBluetoothReaderManager != null) {
                            mBluetoothReaderManager.detectReader(gatt, mGattCallback);
                        }
                    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                        mBluetoothReader = null;
                        if (mBluetoothGatt != null)
                        {
                            mBluetoothGatt.close();
                            mBluetoothGatt = null;
                        }
                    }
                }

            });
        }
    });


    mBluetoothReaderManager = new BluetoothReaderManager();
    mBluetoothReaderManager.setOnReaderDetectionListener(new OnReaderDetectionListener() {
        @Override
        public void onReaderDetection(BluetoothReader reader) {
            if (reader instanceof Acr1255uj1Reader)
            {
                //String strMessage = "On Acr1255uj1Reader Detected.";
                //txtDisplay.setText(strMessage);
            }
            else
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(BTRActivity.this, "The device is not supported!", Toast.LENGTH_SHORT).show();

                        disconnectReader();
                        updateConnectionState(BluetoothReader.STATE_DISCONNECTED);
                    }
                });

                return;
            }

            mBluetoothReader = reader;
            setListener(reader);
            activateReader(reader);

            //My move code from Button click
            String str;
            if (!mBluetoothReader.authenticate(masterKey)) {
               str = "Card reader not ready";
            } else {
               str = "Authenticating...";
            }
            //----------------------------
        }

    });


    connectReader();
}

protected void onDestroy()
{
    super.onDestroy();
    disconnectReader();
}

private void findUiViews()
{
    txtConnectionState = (TextView) findViewById(R.id.tv_connectionState);
    txtDisplay = (TextView) findViewById(R.id.tv_display);
    cmdAuthentication =(Button) findViewById(R.id.cmd_authentication);
}

private void clearAllUi()
{
    txtDisplay.setText("");
}

private void updateConnectionState(final int connectState)
{
    mConnectState = connectState;

    if (connectState == BluetoothReader.STATE_CONNECTING) {
        txtConnectionState.setText("Connecting...");
    } else if (connectState == BluetoothReader.STATE_CONNECTED) {
        txtConnectionState.setText("Connected");
    } else if (connectState == BluetoothReader.STATE_DISCONNECTING) {
        txtConnectionState.setText("Disconnecting...");
    } else {
        txtConnectionState.setText("Disconnected");
        clearAllUi();
    }
    invalidateOptionsMenu();
}

private String getResponseString(byte[] response, int errorCode) {
    if (errorCode == BluetoothReader.ERROR_SUCCESS) {
        if (response != null && response.length > 0) {
            return ClientUtil.toHexString(response);
        }
        return "";
    }
    else{
        return "";
    }
}


private void activateReader(BluetoothReader reader)
{
    if (reader == null) {
        return;
    }

    if (reader instanceof Acr3901us1Reader) {
        /* Start pairing to the reader. */
        ((Acr3901us1Reader) mBluetoothReader).startBonding();
    } else if (mBluetoothReader instanceof Acr1255uj1Reader) {
        /* Enable notification. */
        mBluetoothReader.enableNotification(true);
    }
}

private boolean connectReader()
{
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        updateConnectionState(BluetoothReader.STATE_DISCONNECTED);
        return false;
    }

    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null) {
        updateConnectionState(BluetoothReader.STATE_DISCONNECTED);
        return false;
    }

    /* Clear old GATT connection. */
    if (mBluetoothGatt != null) {
        mBluetoothGatt.disconnect();
        mBluetoothGatt.close();
        mBluetoothGatt = null;
    }

    /* Create a new connection. */
    final BluetoothDevice device = bluetoothAdapter
            .getRemoteDevice(mDeviceAddress);

    if (device == null) {
        return false;
    }

    /* Connect to GATT server. */
    updateConnectionState(BluetoothReader.STATE_CONNECTING);
    mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
    return true;
}

private void disconnectReader()
{
    if (mBluetoothGatt == null) {
        updateConnectionState(BluetoothReader.STATE_DISCONNECTED);
        return;
    }
    updateConnectionState(BluetoothReader.STATE_DISCONNECTING);
    mBluetoothGatt.disconnect();
}

private void setOnClickListener()
{
    cmdAuthentication.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mBluetoothReader == null)
            {
                return;
            }

            if (masterKey != null && masterKey.length > 0)
            {
                if (!mBluetoothReader.authenticate(masterKey)) {
                    txtDisplay.setText("Card reader not ready");
                } else {
                    txtDisplay.setText("Authenticating...");
                }

            }
            else
            {
                txtDisplay.setText("Incorrecct master key.");
            }
        }
    });

}


private void setListener(BluetoothReader reader)
{
    mBluetoothReader.setOnCardStatusChangeListener(new OnCardStatusChangeListener() {
        @Override
        public void onCardStatusChange(BluetoothReader bluetoothReader, final int cardStatus) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //txtCardStatus.setText(getCardStatusString(sta));
                    if (cardStatus == BluetoothReader.CARD_STATUS_PRESENT) {
                        mBluetoothReader.transmitApdu(apdu);
                    }
                    else
                    {
                        txtDisplay.setText("");
                    }

                }
            });
        }
    });

    mBluetoothReader.setOnAuthenticationCompleteListener(new OnAuthenticationCompleteListener() {
        @Override
        public void onAuthenticationComplete(BluetoothReader bluetoothReader, final int errorCode) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (errorCode == BluetoothReader.ERROR_SUCCESS)
                    {
                        //txtDisplay.setText("Authentication Success!");
                        cmdAuthentication.setEnabled(false);

                        //Testing
                        mBluetoothReader.transmitEscapeCommand(AUTO_POLLING_START);
                    }
                    else
                    {
                        //txtDisplay.setText("Authentication Failed!");
                        int errorText = errorCode;
                    }
                }
            });
        }
    });

    mBluetoothReader.setOnResponseApduAvailableListener(new OnResponseApduAvailableListener() {
        @Override
        public void onResponseApduAvailable(BluetoothReader bluetoothReader, final byte[] apdu, final int errorCode) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    txtDisplay.setText(getResponseString(apdu, errorCode));
                }
            });
        }
    });


}
}

Client ClientUtil类

public class ClientUtil {
    public static String toHexString(byte[] array) {

        String bufferString = "";

        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                String hexChar = Integer.toHexString(array[i] & 0xFF);
                if (hexChar.length() == 1) {
                    hexChar = "0" + hexChar;
                }
                bufferString += hexChar.toUpperCase(Locale.US) + " ";
            }
        }
        return bufferString;
    }
}

1 个答案:

答案 0 :(得分:0)

现在我已经解决了。我添加了另一个侦听器(setOnDeviceInfoAvailableListener)。

  1. 我在其中调用了mBluetoothReader.authenticate(masterKey)代码。
  2. 我在onCreate()方法中调用了mBluetoothReader.getDeviceInfo(...)代码。

现在我不需要单击按钮。所有操作都在OnCreate()方法中完成。