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

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

HarmonyOS
2024-09-09 10:06:05
1253浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
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", 
  } 
  ] 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

以下是相关代码:

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); 
      } 
      } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.

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

分享
微博
QQ
微信
回复
2024-09-09 15:55:56
火雨变换

NFC读卡SDK开发包,可以实现NFC读取身份证信息和IC卡等,​​https://aidoing.com.cn/openapi.html鸿蒙的NFC读卡SDK​


分享
微博
QQ
微信
回复
2025-03-18 15:56:11


相关问题
HarmonyOS 能否提供一个视频压缩的demo
617浏览 • 1回复 待解决
需要一个NFC读取的demo
1313浏览 • 1回复 待解决
能否提供一个关于SM3加密的demo
1375浏览 • 1回复 待解决
HarmonyOS 驾驶证书识别的demo
499浏览 • 1回复 待解决
能否提供一个SM3加密案例
1172浏览 • 1回复 待解决
提供一个关于地图组件使用的小demo
1336浏览 • 1回复 待解决
HarmonyOS 活体检测和卡证识别的demo
658浏览 • 1回复 待解决
有没有实时相机预览结合OCR识别的demo
2357浏览 • 1回复 待解决