我實現了Android應用程序 - 服務器端應用程序。 Android應用程序與服務器通信以獲得智能卡的認證。當我單擊應用程序中的按鈕時,將建立TCP連接並且正在交換消息,直到協議結束爲止。當我單擊應用程序中的按鈕時,目前我遇到了問題。同樣的過程是通過 ,但從智能卡檢索的數據是不同的。Channel.transmit(新的CommandAPDU(數組))在再次單擊按鈕時檢索另一個值
在智能卡類 - > forwardMessage():
- >第一個按鈕點擊在Android應用 - >第一ClientSocket的:
我正在此字節[0, -92, 2, 12, 2, 0, 2]
陣列如屏幕截圖並呼籲channel.transmit(new CommandAPDU(array));
當我得到正確的反應[-112,0]
- >第二個按鈕點擊Android應用程序 - >第二ClientSocket的(不運行服務器應用NEU)
我得到這個字節數組[0, -92, 2, 12, 2, 0, 2]
如屏幕截圖,並呼籲channel.transmit(new CommandAPDU(array));
當我得到正確的反應[106,-126]
這個結果[106,-126]
應該像第一clientSocket
也[-112,0]
之一。作爲從智能卡檢索到的數據[106,-126]
的結果,該協議不會被執行到最後。
我曾試着撥打卡類的斷開()斷開()的智能卡內,但我
我明白任何幫助!
第一ClientSocket的數據
第二ClientSocket的數據
Android應用程序 - >服務器 - >讀卡器
服務器類
public class Server {
private final static int RECEIVE_BUFFER_LENGTH = 512;
public final static int MESSAGE_MAXIMUM_LENGTH = 256;
private final static int MESSAGE_HEADER_LENGTH = 8;
private static InputStream bufferedInputStream = null;
private static OutputStream outputStream = null;
private static SmartCard smartCard = null;
public static void main(String args[]) throws Exception {
ServerSocket serverSocket = new ServerSocket(27015);
System.out.println("SS construction of server socket");
System.out.println("SS listenting for incoming connection on port 27015");
while (true) {
Socket clientSocket = serverSocket.accept();
Server.bufferedInputStream = new BufferedInputStream(clientSocket.getInputStream());
Server.outputStream = clientSocket.getOutputStream();
Server.smartCard = new SmartCard();
Server.handleConnection();
Server.bufferedInputStream.close();
Server.outputStream.close();
Server.smartCard = null;
clientSocket.close();
System.out.println("Smart card instance was deleted ");
System.out.println("--------------------- Finished ---------------------------");
}
}
private static void handleConnection() throws IOException {
ByteBuffer receiveBuffer = ByteBuffer.allocate(Server.RECEIVE_BUFFER_LENGTH);
int readBytes = 0;
while (true) {
readBytes = Server.bufferedInputStream.read(receiveBuffer.array(), receiveBuffer.position(),
receiveBuffer.remaining());
System.out.println("readBytes: " + readBytes);
if (readBytes < 0) {
break;
}
//Here I am reading the received bytes and communicating with the Smart card.
}
}
}
}
智能卡
public class SmartCard {
public Card card;
private String protocol;
public void connect(String preferredProtocol) {
Card cardTemp = null;
this.protocol = preferredProtocol;
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
CardTerminal terminal = terminals.get(0);
System.out.println("Reader name: " + terminal.getName());
if (preferredProtocol.equals(ProtocolType.SC_T0.getProtocolName())) {
cardTemp = terminal.connect(preferredProtocol);
} else if (preferredProtocol.equals(ProtocolType.SC_T1.getProtocolName())) {
cardTemp = terminal.connect(preferredProtocol);
}
System.out.println("SC connect --> SCARD Protocol " + preferredProtocol);
this.card = cardTemp;
} catch (CardException e) {
e.printStackTrace();
}
}
private boolean isConnect() {
if (this.card != null) {
return true;
} else {
return false;
}
}
public void disconnect() throws CardException {
if (this.isConnect()) {
this.card = null;
//this.card.disconnect(false);
System.out.println("SC disconnect()");
}
}
private void reconnect(String preferredProtocol) {
if (!this.isConnect()) {
this.connect(preferredProtocol);
}
}
public byte[] requestATR() {
ATR atr = this.card.getATR();
if (atr.getBytes().length > 0xFF) {
throw new IllegalArgumentException(
"Package too big, not supported with protocol -> Answer To Test byte array is too big!");
}
return atr.getBytes();
}
public byte[] forwardMessage(byte[] array) throws CardException {
try {
if (!this.isConnect()) {
this.reconnect(this.protocol);
System.out.println("SC reconnect()");
}
Cg2AapiServer.printData(array, array.length, SmartCard.OPERATOR, Cg2AapiServer.RECEIVE);
CardChannel channel = this.card.getBasicChannel();
System.out.println("data from the client socket: " + Arrays.toString(array));
ResponseAPDU responseAPDU = channel.transmit(new CommandAPDU(array));
byte[] byteArray = responseAPDU.getBytes();
System.out.println("retrieved data from the smart card: " + Arrays.toString(byteArray));
if (responseAPDU.getBytes().length > 0xFF) {
throw new IllegalArgumentException("Package too big, not supported with protocol.");
}
Cg2AapiServer.printData(responseAPDU.getBytes(), responseAPDU.getBytes().length, SmartCard.OPERATOR,
Cg2AapiServer.TRANSMIT);
return responseAPDU.getBytes();
} catch (CardException e) {
e.printStackTrace();
}
return null;
}
}