HarmonyOS能否提供一个NFC识别的demo

HarmonyOS能否提供一个NFC识别的demo。

HarmonyOS
2024-09-09 10:06:05
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

需要在应用的属性配置文件中,声明与NFC相关的属性值。比如,在module.json5文件中,声明下面属性值:

{ 
  "module": { 
  "abilities": [ 
  { 
    "skills": [ 
    { 
      "actions": [ 
      "ohos.nfc.tag.action.TAG_FOUND" 
      ], 
      "uris": [ 
      { 
        "type":"tag-tech/NfcA" 
      }, 
      { 
        "type":"tag-tech/IsoDep" 
      } 
      ] 
    } 
    ] 
  } 
  ], 
  "requestPermissions": [ 
  { 
    "name": "ohos.permission.NFC_TAG", 
  "reason": "tag", 
  } 
  ] 
} 
}

以下是相关代码:

import tag from '@ohos.nfc.tag'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import Want from '@ohos.app.ability.Want' 
export default class EntryAbility extends UIAbility { 
  onCreate(want : Want, launchParam: AbilityConstant.LaunchParam) { 
    // add other code here... 
    // want is initialized by nfc service, contains tag info for this found tag 
    let tagInfo : tag.TagInfo | null = null; 
    try { 
      tagInfo = tag.getTagInfo(want); 
    } catch (error) { 
      console.error("tag.getTagInfo catched error: " + error); 
    } 
    if (tagInfo == null || tagInfo == undefined) { 
      console.log("no TagInfo to be created, ignore it."); 
      return; 
    } 
    // get the supported technologies for this found tag. 
    let isNfcATag =  false; 
    let isIsoDepTag =  false; 
    for (let i = 0; i < tagInfo.technology.length; i++) { 
      if (tagInfo.technology[i] == tag.NFC_A) { 
        isNfcATag = true; 
      } 
 
      if (tagInfo.technology[i] == tag.ISO_DEP) { 
        isIsoDepTag = true; 
      } 
      // also check for technology: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE 
    } 
    // use NfcA APIs to access the found tag. 
    if (isNfcATag) { 
      let nfcA : tag.NfcATag | null = null; 
      try { 
        nfcA = tag.getNfcATag(tagInfo); 
      } catch (error) { 
        console.error("tag.getNfcATag catched error: " + error); 
      } 
      // other code to read or write this found tag. 
    } 
    // use getIsoDep APIs to access the found tag. 
    if (isIsoDepTag) { 
      let isoDep : tag.IsoDepTag | null = null; 
      try { 
        isoDep = tag.getIsoDep(tagInfo); 
      } catch (error) { 
        console.error("tag.getIsoDep catched error: " + error); 
      } 
      } 
  } 
}

可以参考文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-nfctag-V5

分享
微博
QQ
微信
回复
2024-09-09 15:55:56
相关问题
需要一个NFC读取的demo
274浏览 • 1回复 待解决
能否提供一个关于SM3加密的demo
506浏览 • 1回复 待解决
能否提供一个SM3加密案例
477浏览 • 1回复 待解决
提供一个关于地图组件使用的小demo
323浏览 • 1回复 待解决
有没有实时相机预览结合OCR识别的demo
1567浏览 • 1回复 待解决
HarmonyOS 需要一个筛选页面的demo
199浏览 • 1回复 待解决
HarmonyOS提供路由跳转Demo
273浏览 • 1回复 待解决
HarmonyOS 有没有蓝牙,NFC相关的DEMO
212浏览 • 1回复 待解决
语音识别的方法有哪些?
361浏览 • 1回复 待解决