HarmonyOS-鸿蒙app开发 —基于java网络与连接NFC消息通知

HarmonyOS-鸿蒙app开发 —基于java网络与连接NFC消息通知

场景介绍

NFC消息通知是HarmonyOS内部或者与应用之间跨进程通讯的机制,注册者在注册消息通知后,一旦符合条件的消息被发出,注册者即可接收到该消息。

接口说明

表1 NFC消息通知的相关广播介绍

描述

通知名

附加参数

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

注册并获取NFC状态改变消息

  1. 构建消息通知接收者NfcStateEventSubscriber。
  2. 注册NFC状态改变消息。

NfcStateEventSubscriber接收并处理NFC状态改变消息。

// 构建消息接收者/注册者

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

}

注册并获取NFC场强消息

  1. 构建消息通知接收者NfcFieldOnAndOffEventSubscriber。
  2. 注册NFC场强消息。

NfcFieldOnAndOffEventSubscriber接收并处理NFC场强消息。

// 构建消息接收者/注册者

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

}

5 1 投票
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x