场景介绍
应用或者其他模块可以通过接口完成以下功能:
1. 获取安全单元的个数和名称;
2. 判断安全单元是否在位;
3. 在指定安全单元上打开基础通道;
4. 在指定安全单元上打开逻辑通道;
5. 发送 APDU 数据到安全单元上。
完成开卡到安全单元
创建基础通道访问安全单元的开发步骤如下:
1. 调用 SEService 类的构造函数,创建一个安全单元服务的实例,用于访问安全单元。
2. 调用 isConnected()接口,查询安全单元服务的连接状态。
3. 调用 getReaders()接口,获取本机的全部安全单元。
4. 调用 Reader 类的 openSession()接口打开 Session,返回一个打开的Session 实例。
5. 调用 Session 类的 openBasicChannel(aid)接口打开基础通道,或者调用openLogicalChannel(aid)接口打开逻辑通道,返回一个打开通道 Channel实例。
6. 调用 Channel 类的 transmit(byte[] command),发送 APDU 到安全单元。
7. 调用 Channel 类的 closeChannel()接口关闭通道。
8. 调用 Session 类的 closeSessionChannels()接口关闭 Session 的所有通道。
9. 调用 Reader 类的 closeSeSessions()接口关闭安全单元的所有 Session。
10. 调用 SEService 类的 shutdown()接口关闭安全单元服务。xpanx.com
private class AppServiceConnectedCallback implements
SEService.OnCallback {
@Override
public void serviceConnected() {
// 应用自实现
}
}
// 创建安全单元服务实例SEService mSEService = new SEService(context, new
AppServiceConnectedCallback());
// 查询安全单元服务的连接状态
boolean isConnected = mSEService.isConnected();
// 获取本机的全部安全单元,并获取指定的安全单元 eSE
Reader[] elements = mSEService.getReaders();
Reader eSe = null;
for (int i = 0; i < elements.length; i++) {
if ("eSE".equals(elements[i].getName())) {
eSe = elements[i];
break;
}
}
// 查询安全单元是否在位
boolean isPresent = eSe.isSecureElementPresent();
// 打开 Session
Optional<Session> optionalSession = eSe.openSession();
Session session = optionalSession.orElse(null);
// 打开通道
if (eSe != null) {
byte[] aidValue = new byte[]{(byte)0x01, (byte)0x02, (byte)0x03,
(byte)0x04, (byte)0x05};
Aid aid = new Aid(aidValue, 0, aidValue.length); // 创建 Aid 实例
// 打开基础通道
Optional<Channel> optionalChannel = session.openBasicChannel(aid);
Channel basicChannel = optionalChannel.orElse(null);
// 打开逻辑通道
optionalChannel = session.openLogicalChannel(aid);
Channel logicalChannel = optionalChannel.orElse(null);
// 发送指令给安全单元,返回值为安全单元对指令的响应
byte[] resp = logicalChannel.transmit(new byte[]{(byte)0x00,
(byte)0xa4, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x00,
(byte)0x00});
// 关闭通道资源
basicChannel.closeChannel()
logicalChannel.closeChannel();
}// 关闭 Session 资源
session.close();
// 关闭安全单元资源
eSe.closeSeSessions();
// 关闭安全单元服务资源
mSEService.shutdown();
xpanx.com
评论