欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 鸿蒙开发(API 12 Beta6版)【NFC标签读写】 网络篇

鸿蒙开发(API 12 Beta6版)【NFC标签读写】 网络篇

2024/10/25 9:31:31 来源:https://blog.csdn.net/m0_70748845/article/details/142069949  浏览:    关键词:鸿蒙开发(API 12 Beta6版)【NFC标签读写】 网络篇

简介

近场通信(Near Field Communication,NFC)是一种短距高频的无线电技术,在13.56MHz频率运行,通信距离一般在10厘米距离内。电子设备可以通过NFC通信技术和NFC标签通信,从标签中读取数据,或写入数据到标签。

NFC标签可能会支持一种或多种通信技术,具体技术如下:

  • NfcA (也称为 ISO 14443-3A)
  • NfcB (也称为 ISO 14443-3B)
  • NfcF (也称为 JIS 6319-4)
  • NfcV (也称为 ISO 15693)
  • IsoDep
  • NDEF
  • MifareClassic
  • MifareUltralight

场景介绍

电子设备通过NFC天线位置触碰NFC标签卡片,完成NFC标签卡片的读取或写入。从使用场景上,可以分成NFC标签前台读写,和NFC标签后台读写。

  • NFC标签前台读写

    前台读写是指在触碰NFC标签之前,用户先在电子设备上打开特定的应用程序,用户明确想使用所打开的应用程序和NFC标签进行读写操作。用户打开应用程序在前台,并且进入应用的刷卡页面之后,电子设备触碰NFC标签,只会把读取到的卡片分发给前台应用。

  • NFC标签后台读写

    后台读写是指不打开特定的NFC标签应用程序,电子设备触碰发现NFC标签后,根据NFC标签的技术类型,分发给能够处理的应用程序。如果能匹配到多个应用程序,则弹出应用选择器列举出应用列表给用户手动选择。用户选择指定的应用后,自动跳转到应用程序的NFC标签读写卡页面。

  • 标签读写约束条件

    不管是前台读写,还是后台读写,电子设备能够发现NFC标签的前提条件是设备必须是亮屏和解锁状态。

接口说明

获取不同技术类型标签对象的接口说明如下表,根据不同技术的标签对象来执行NFC标签的读写。

接口名功能描述
getNfcA(tagInfo: TagInfo): NfcATag获取NfcA技术类型的标签对象。
getNfcB(tagInfo: TagInfo): NfcBTag获取NfcB技术类型的标签对象。
getNfcF(tagInfo: TagInfo): NfcFTag获取NfcF技术类型的标签对象。
getNfcV(tagInfo: TagInfo): NfcVTag获取NfcV技术类型的标签对象。
getIsoDep(tagInfo: TagInfo): IsoDepTag获取IsoDep技术类型的标签对象。
getNdef(tagInfo: TagInfo): NdefTag获取NDEF技术类型的标签对象。
getMifareClassic(tagInfo: TagInfo): MifareClassicTag获取MifareClassic技术类型的标签对象。
getMifareUltralight(tagInfo: TagInfo): MifareUltralightTag获取MifareUltralight技术类型的标签对象。

开发步骤

前台读取标签

  1. 在module.json5文件中声明NFC标签读取的权限,以及声明NFC标签特定的action;
  2. import需要的tag模块和其他相关的模块;
  3. 判断设备是否支持NFC能力;
  4. 调用tag模块中前台优先的接口,使能前台应用程序优先处理所发现的NFC标签功能;
  5. 获取特定技术类型的NFC标签对象;
  6. 执行读写接口完成标签数据的读取或写入数据到标签;
  7. 退出应用程序NFC标签页面时,调用tag模块退出前台优先功能。
    "abilities": [{"name": "EntryAbility","srcEntry": "./ets/entryability/EntryAbility.ts","description": "$string:EntryAbility_desc","icon": "$media:icon","label": "$string:EntryAbility_label","startWindowIcon": "$media:icon","startWindowBackground": "$color:start_window_background","exported": true,"skills": [{"entities": ["entity.system.home"],"actions": ["action.system.home",// Add the nfc tag action to filter out for this application."ohos.nfc.tag.action.TAG_FOUND"]}]}],"requestPermissions": [{// Add the permission for nfc tag operations."name": "ohos.permission.NFC_TAG","reason": "$string:app_name",}]
import { tag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit';let nfcTagElementName: bundleManager.ElementName;
let foregroundRegister: boolean;async function readerModeCb(error : BusinessError, tagInfo : tag.TagInfo) {if (!error) {// 获取特定技术类型的NFC标签对象if (tagInfo == null || tagInfo == undefined) {hilog.error(0x0000, 'testTag', 'readerModeCb tagInfo is invalid');return;}if (tagInfo.uid == null || tagInfo.uid == undefined) {hilog.error(0x0000, 'testTag', 'readerModeCb uid is invalid');return;}if (tagInfo.technology == null || tagInfo.technology == undefined || tagInfo.technology.length == 0) {hilog.error(0x0000, 'testTag', 'readerModeCb technology is invalid');return;}// 执行读写接口完成标签数据的读取或写入数据到标签// use the IsoDep technology to access this nfc tag.let isoDep : tag.IsoDepTag | null = null;for (let i = 0; i < tagInfo.technology.length; i++) {if (tagInfo.technology[i] == tag.ISO_DEP) {try {isoDep = tag.getIsoDep(tagInfo);} catch (error) {hilog.error(0x0000, 'testTag', 'readerModeCb getIsoDep error = %{public}s', JSON.stringify(error));return;}}// use other technology to access this nfc tag if neccessary.}if (isoDep == undefined) {hilog.error(0x0000, 'testTag', 'readerModeCb getIsoDep is invalid');return;}// connect to this nfc tag using IsoDep technology.try {isoDep.connect(); } catch (error) {hilog.error(0x0000, 'testTag', 'readerModeCb isoDep.connect() error = %{public}s', JSON.stringify(error));return;}if (!isoDep.isConnected()) {hilog.error(0x0000, 'testTag', 'readerModeCb isoDep.isConnected() false.');return;}// transmit data to the connected tag.let cmdData = [0x01, 0x02, 0x03, 0x04]; // please change the raw data to be correct.try {isoDep.transmit(cmdData).then((response : number[]) => {hilog.info(0x0000, 'testTag', 'readerModeCb isoDep.transmit() response = %{public}s.', JSON.stringify(response));}).catch((err : BusinessError)=> {hilog.error(0x0000, 'testTag', 'readerModeCb isoDep.transmit() err = %{public}s.', JSON.stringify(err));return;});} catch (businessError) {hilog.error(0x0000, 'testTag', 'readerModeCb isoDep.transmit() businessError = %{public}s.', JSON.stringify(businessError));return;}} else {hilog.info(0x0000, 'testTag', 'readerModeCb readerModeCb error %{public}s', JSON.stringify(error));}
}export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');// 判断设备是否支持NFC能力if (!canIUse("SystemCapability.Communication.NFC.Core")) {hilog.error(0x0000, 'testTag', 'nfc unavailable.');return;}nfcTagElementName = {bundleName: want.bundleName ?? '',abilityName: want.abilityName ?? '',moduleName: want.moduleName,}}onForeground() {// Ability has brought to foregroundhilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');if (nfcTagElementName != undefined) {// 调用tag模块中前台优先的接口,使能前台应用程序优先处理所发现的NFC标签功能let techList : number[] = [tag.NFC_A, tag.NFC_B, tag.NFC_F, tag.NFC_V];try {tag.on('readerMode', nfcTagElementName, techList, readerModeCb);foregroundRegister = true;} catch (error) {hilog.error(0x0000, 'testTag', 'on readerMode error = %{public}s', JSON.stringify(error));}}}onBackground() {// Ability has back to backgroundhilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');// 退出应用程序NFC标签页面时,调用tag模块退出前台优先功能if (foregroundRegister) {foregroundRegister = false;try {tag.off('readerMode', nfcTagElementName);} catch (error) {hilog.error(0x0000, 'testTag', 'off readerMode error = %{public}s', JSON.stringify(error));}}}
}

后台读取标签

  1. 在module.json5文件中声明NFC标签读取的权限,声明NFC标签特定的action,以及声明本应用程序的能够处理的NFC标签技术类型;
  2. import需要的tag模块和其他相关的模块;
  3. 获取特定技术类型的NFC标签对象;
  4. 执行读写接口完成标签数据的读取或写入数据到标签。
    "abilities": [{"name": "EntryAbility","srcEntry": "./ets/entryability/EntryAbility.ts","description": "$string:EntryAbility_desc","icon": "$media:icon","label": "$string:EntryAbility_label","startWindowIcon": "$media:icon","startWindowBackground": "$color:start_window_background","exported": true,"skills": [{"entities": ["entity.system.home"],"actions": ["action.system.home",// Add the nfc tag action to filter out for this application."ohos.nfc.tag.action.TAG_FOUND"],"uris": [{"type":"tag-tech/NfcA"},{"type":"tag-tech/IsoDep"}// Add other technologies if neccessary,// such as: NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable]}]}],"requestPermissions": [{// Add the permission for nfc tag operations."name": "ohos.permission.NFC_TAG","reason": "$string:app_name",}]
import { tag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');// 获取特定技术类型的NFC标签对象let tagInfo : tag.TagInfo;try {tagInfo = tag.getTagInfo(want);} catch (error) {hilog.error(0x0000, 'testTag', 'getTagInfo error = %{public}s', JSON.stringify(error));return;}if (tagInfo == null || tagInfo == undefined) {hilog.error(0x0000, 'testTag', 'tagInfo is invalid');return;}if (tagInfo.uid == null || tagInfo.uid == undefined) {hilog.error(0x0000, 'testTag', 'uid is invalid');return;}if (tagInfo.technology == null || tagInfo.technology == undefined || tagInfo.technology.length == 0) {hilog.error(0x0000, 'testTag', 'technology is invalid');return;}// 执行读写接口完成标签数据的读取或写入数据到标签// use the IsoDep technology to access this nfc tag.let isoDep : tag.IsoDepTag | null = null;for (let i = 0; i < tagInfo.technology.length; i++) {if (tagInfo.technology[i] == tag.ISO_DEP) {try {isoDep = tag.getIsoDep(tagInfo);} catch (error) {hilog.error(0x0000, 'testTag', 'getIsoDep error = %{public}s', JSON.stringify(error));return;}}// use other technology to access this nfc tag if neccessary.}if (isoDep == undefined) {hilog.error(0x0000, 'testTag', 'getIsoDep is invalid');return;}// connect to this nfc tag using IsoDep technology.try {isoDep.connect(); } catch (error) {hilog.error(0x0000, 'testTag', 'isoDep.connect() error = %{public}s', JSON.stringify(error));return;}if (!isoDep.isConnected()) {hilog.error(0x0000, 'testTag', 'isoDep.isConnected() false.');return;}// transmit data to the connected tag.let cmdData = [0x01, 0x02, 0x03, 0x04]; // please change the raw data to be correct.try {isoDep.transmit(cmdData).then((response : number[]) => {hilog.info(0x0000, 'testTag', 'isoDep.transmit() response = %{public}s.', JSON.stringify(response));}).catch((err : BusinessError)=> {hilog.error(0x0000, 'testTag', 'isoDep.transmit() err = %{public}s.', JSON.stringify(err));return;});} catch (businessError) {hilog.error(0x0000, 'testTag', 'isoDep.transmit() businessError = %{public}s.', JSON.stringify(businessError));return;}}
}

最后呢

很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。

而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。

在这里插入图片描述

针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。

  • 《鸿蒙 (OpenHarmony)开发学习视频》
  • 《鸿蒙生态应用开发V2.0白皮书》
  • 《鸿蒙 (OpenHarmony)开发基础到实战手册》
  • OpenHarmony北向、南向开发环境搭建
  • 《鸿蒙开发基础》
  • 《鸿蒙开发进阶》
  • 《鸿蒙开发实战》

在这里插入图片描述

总结

鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。

并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿
1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com