文章40 | 阅读 20710 | 点赞0
// 查询本机是否支持NFC
if (context != null) {
NfcController nfcController = NfcController.getInstance(context);
} else {
return;
}
boolean isAvailable = nfcController.isNfcAvailable();
if (isAvailable) {
// 调用查询NFC是否打开接口,返回值为NFC是否是打开的状态
boolean isOpen = nfcController.isNfcOpen();
}
| 类名 | 接口名 | 功能描述 |
| SEService | SEService() | 创建一个安全单元服务的实例 |
| isConnected() | 查询安全单元服务是否已连接 |
| shutdown() | 关闭安全单元服务 |
| getReaders() | 获取全部安全单元 |
| getVersion() | 获得安全单元服务的版本 |
| OnCallback | 用于回调的内部类,用于定义回调接口。<br>在服务连接成功后,回调该接口通知应用 |
| Reader | getName() | 获取安全单元的名称 |
| isSecureElementPresent() | 检查安全单元是否在位 |
| openSession() | 打开当前安全单元上的session |
| closeSession() | 关闭当前安全单元上的所有session |
| Session | openBasicChannel(Aid aid) | 打开基础通道 |
| openLogicalChannel(Aid aid) | 创建逻辑通道 |
| getATR() | 获得重设安全单元指令的响应 |
| closeSessionChannels() | 关闭当前session的所有通道 |
| Channel | isClosed() | 判断通道是否关闭 |
| isBasicChannel() | 判断是否是基础通道 |
| transmit(byte[] command) | 发送指令到安全单元 |
| getSelectResponse() | 获得应用程序选择指令的响应 |
| closeChannel() | 关闭通道 |
| Aid | Aid(byte[] aid, int offset, int length) | 构造一个AID类的实例 |
| isAidValid() | 查询AID是否有效 |
| getAidBytes() | 获取AID的字节数组形式的值 |
private static final String ESE = "eSE";
private class AppServiceConnectedCallback implements SEService.OnCallback {
@Override
public void serviceConnected() {
// 应用自实现
}
}
// 创建安全单元服务实例
SEService sEService = new SEService(context, new AppServiceConnectedCallback());
// 查询安全单元服务的连接状态
boolean isConnected = sEService.isConnected();
// 获取本机的全部安全单元,并获取指定的安全单元eSE
Reader[] elements = sEService.getReaders();
Reader eSe = null;
for (int i = 0; i < elements.length; i++) {
if (ESE.equals(elements[i].getName())) {
eSe = elements[i];
break;
}
}
if (eSe == null) {
return;
}
// 查询安全单元是否在位
boolean isPresent = eSe.isSecureElementPresent();
// 打开Session
Optional<Session> optionalSession = eSe.openSession();
Session session = optionalSession.orElse(null);
if (session == null) {
return;
}
// 打开通道
if (eSe != null) {
byte[] aidValue = new byte[]{(byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05};
// 创建Aid实例
Aid aid = new Aid(aidValue, 0, aidValue.length);
// 打开基础通道
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});
// 关闭通道资源
if (basicChannel.isPresent()) {
basicChannel.closeChannel();
}
if (logicalChannel.isPresent()) {
logicalChannel.closeChannel();
}
// 关闭Session资源
session.close();
// 关闭安全单元资源
eSe.closeSessions();
// 关闭安全单元服务资源
sEService.shutdown();
| 类名 | 接口名 | 功能描述 |
| CardEmulation | getInstance(NfcController controller) | 创建一个卡模拟类的实例 |
| isSupported(int feature) | 查询是否支持卡模拟功能 |
| setListenMode(int mode) | 设置卡模拟模式 |
| isListenModeEnabled() | 查询卡模拟功能是否打开 |
| getNfcInfo(String key) | 获取NFC的信息 |
| getSelectionType(String category) | 根据NFC服务的类型获取刷卡时选择服务的方式 |
| registerForegroundPreferred(Ability appAbility, ElementName appName) | 动态设置前台优先应用 |
| unregisterForegroundPreferred(Ability appAbility) | 取消设置前台优先应用 |
| isDefaultForAid(ElementName appName, String aid) | 判断应用是否是指定AID的默认处理应用 |
| registerAids(ElementName appName, String type, List aids) | 给应用注册指定类型的AID |
| removeAids(ElementName appName, String type) | 删除应用的指定类型的AID |
| getAids(ElementName appName, String type) | 获取应用中指定类型的AID列表 |
| HostService | sendResponse(byte[] response) | 发送响应的数据到对端设备 |
| handleRemoteCommand(byte[] cmd, IntentParams params) | 处理对端设备发送的命令 |
| disabledCallback(int errCode) | 连接异常的回调 |
// 获取NFC控制对象
NfcController nfcController = NfcController.getInstance(context);
// 获取卡模拟控制对象
CardEmulation cardEmulation = CardEmulation.getInstance(nfcController);
// 查询是否支持HCE、UICC、ESE卡模拟,返回值表示是否支持对应安全单元的卡模拟
boolean isSupportedHce = cardEmulation.isSupported(CardEmulation.FEATURE_HCE);
boolean isSupportedUicc = cardEmulation.isSupported(CardEmulation.FEATURE_UICC);
boolean isSupportedEse = cardEmulation.isSupported(CardEmulation.FEATURE_ESE);
// 获取NFC控制对象
NfcController nfcController = NfcController.getInstance(context);
// 获取卡模拟控制对象
CardEmulation cardEmulation = CardEmulation.getInstance(nfcController);
// 打开卡模拟
cardEmulation.setListenMode(CardEmulation.ENABLE_MODE_ALL);
// 调用查询卡模拟开关状态的接口,返回值为卡模拟是否是打开的状态
boolean isEnabled = cardEmulation.isListenModeEnabled();
// 关闭卡模拟
cardEmulation.setListenMode(CardEmulation.DISABLE_MODE_A_B);
// 调用查询卡模拟开关状态的接口,返回值为卡模拟是否是打开的状态
isEnabled = cardEmulation.isListenModeEnabled();
// 获取NFC控制对象
NfcController nfcController = NfcController.getInstance(context);
// 获取卡模拟控制对象
CardEmulation cardEmulation = CardEmulation.getInstance(nfcController);
// 查询本机当前使能的安全单元类型
String seType = cardEmulation.getNfcInfo(CardEmulation.KEY_ENABLED_SE_TYPE); // ENABLED_SE_TYPE_ESE
// 查询Hisee上电状态
String hiseeState = cardEmulation.getNfcInfo(CardEmulation.KEY_HISEE_READY);
// 查询是否支持RSSI的查询
String rssiAbility = cardEmulation.getNfcInfo(CardEmulation.KEY_RSSI_SUPPORTED);
// 获取NFC控制对象
NfcController nfcController = NfcController.getInstance(context);
// 获取卡模拟控制对象
CardEmulation cardEmulation = CardEmulation.getInstance(nfcController);
// 获取选择服务的方式
int result = cardEmulation.getSelectionType(CardEmulation.CATEGORY_PAYMENT); // SELECTION_TYPE_PREFER_DEFAULT
result = cardEmulation.getSelectionType(CardEmulation.CATEGORY_OTHER); // SELECTION_TYPE_ASK_IF_CONFLICT
// 获取NFC控制对象
NfcController nfcController = NfcController.getInstance(context);
// 获取卡模拟控制对象
CardEmulation cardEmulation = CardEmulation.getInstance(nfcController);
// 动态设置前台优先应用
Ability ability = new Ability();
cardEmulation.registerForegroundPreferred(ability, new ElementName());
// 注销前台优先应用
cardEmulation.unregisterForegroundPreferred(ability);
描述 | 通知名 | 附加参数 |
---|---|---|
NFC状态 | usual.event.nfc.action.ADAPTER_STATE_CHANGED | extra_nfc_state |
进场消息 | usual.event.nfc.action.RF_FIELD_ON_DETECTED | extra_nfc_transaction |
离场消息 | usual.event.nfc.action.RF_FIELD_OFF_DETECTED | - |
// 构建消息接收者/注册者
class NfcStateEventSubscriber extends CommonEventSubscriber {
NfcStateEventSubscriber (CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
if (commonEventData == null || commonEventData.getIntent() == null) {
return;
}
if (NfcController.STATE_CHANGED.equals(commonEventData.getIntent().getAction())) {
IntentParams params = commonEventData.getIntent().getParams();
int currState = commonEventData.getIntent().getIntParam(NfcController.EXTRA_NFC_STATE, NfcController.STATE_OFF);
}
}
}
// 注册消息
MatchingSkills matchingSkills = new MatchingSkills();
// 增加获取NFC状态改变消息
matchingSkills.addEvent(NfcController.STATE_CHANGED);
matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_NFC_ACTION_ADAPTER_STATE_CHANGED);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
NfcStateEventSubscriber subscriber = new NfcStateEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.error(TAG, "doSubscribe occur exception: %{public}s" ,e.toString());
}
// 构建消息接收者/注册者
class NfcFieldOnAndOffEventSubscriber extends CommonEventSubscriber {
NfcFieldOnAndOffEventSubscriber (CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
if (commonEventData == null || commonEventData.getIntent() == null) {
return;
}
if (NfcController.FIELD_ON_DETECTED.equals(commonEventData.getIntent().getAction())) {
IntentParams params = commonEventData.getIntent().getParams();
if (params == null) {
HiLog.info(TAG, "Pure FIELD_ON_DETECTED");
} else {
HiLog.info(TAG, "Transaction FIELD_ON_DETECTED");
Intent transactionIntent = (Intent) params.getParam("transactionIntent");
}
} else if (NfcController.FIELD_OFF_DETECTED.equals(commonEventData.getIntent().getAction())) {
HiLog.info(TAG, "FIELD_OFF_DETECTED");
}
HiLog.info(TAG, "NfcFieldOnAndOffEventSubscriber onReceiveEvent: %{public}s", commonEventData.getIntent().getAction());
}
}
// 注册消息
MatchingSkills matchingSkills = new MatchingSkills();
// 增加获取NFC状态改变消息
matchingSkills.addEvent(NfcController.FIELD_ON_DETECTED);
matchingSkills.addEvent(NfcController.FIELD_OFF_DETECTED);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
HiLog.info(TAG, "subscribeInfo permission: %{public}s", subscribeInfo.getPermission());
NfcFieldOnAndOffEventSubscriber subscriber = new NfcFieldOnAndOffEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.error(TAG, "doSubscribe occur exception: %{public}s", e.toString());
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Forever_wj/article/details/118393557
内容来源于网络,如有侵权,请联系作者删除!