ACR1255U-J1 Аутентификация читателяAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 ACR1255U-J1 Аутентификация читателя

Сообщение Anonymous »

Я разрабатываю приложение для Android с ACR1255U-J1. У меня есть один TextView (для отображения UID с тегом) и одна кнопка (для подключения читателя ACR1255U-J1). Мой код запускается без проблем. < /P>

Но я не хочу нажимать кнопку, чтобы подключиться. Поэтому я перемещаю код кнопки, нажмите на CREATE. Это не работа. Пожалуйста, дайте мне предложение, как решить мою проблему. < /P>

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));
}
});
}
});

}
}
< /code>

clientutil class < /p>

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 & 0xFF);
if (hexChar.length() == 1) {
hexChar = "0" + hexChar;
}
bufferString += hexChar.toUpperCase(Locale.US) + " ";
}
}
return bufferString;
}
}


Подробнее здесь: https://stackoverflow.com/questions/519 ... entication
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»