厦门网站建设ui,网站有可能搜不到吗,沈阳做网站seo,wordpress修改博客文章目录 Qt NFC 读写模式详解1. NFC 读写模式简介1.1 什么是 NFC 读写模式#xff1f;主要功能#xff1a; 1.2 常见应用场景 2. Qt NFC 读写模式原理3. 配置 QtNFC 模块4. NFC 读写操作实现4.1 NFC 标签读取代码示例功能解析 4.2 NFC 标签写入代码示例功能解析 5. 使用注意… 文章目录 Qt NFC 读写模式详解1. NFC 读写模式简介1.1 什么是 NFC 读写模式主要功能 1.2 常见应用场景 2. Qt NFC 读写模式原理3. 配置 QtNFC 模块4. NFC 读写操作实现4.1 NFC 标签读取代码示例功能解析 4.2 NFC 标签写入代码示例功能解析 5. 使用注意事项6. 总结 Qt NFC 读写模式详解
NFC近场通信是一种短距离无线通信技术支持设备间在近距离内10 厘米以内交换信息。Qt 提供了强大的 QtNFC 模块允许开发者轻松实现 NFC 的功能。在本文中我们将聚焦于 NFC 的读写模式详细介绍其工作原理以及如何在 Qt 中实现读写操作。 1. NFC 读写模式简介
1.1 什么是 NFC 读写模式
NFC 读写模式是 NFC 三种工作模式之一。在这种模式下主动设备例如智能手机或 NFC 读卡器可以读取被动设备如 NFC 标签上的数据或者向其写入数据。
主要功能
读取数据主动设备扫描并读取 NFC 标签中的数据如文本、URL 等。写入数据主动设备向 NFC 标签写入数据实现动态数据交互。
1.2 常见应用场景
扫描商品上的 NFC 标签以获取产品信息。写入名片信息到 NFC 标签实现快速分享。更新 NFC 标签内容用于动态广告牌。读取门禁卡或车票上的信息。 2. Qt NFC 读写模式原理
在 Qt 中NFC 读写操作由 QNearFieldManager 和 QNearFieldTarget 类实现 QNearFieldManager 管理 NFC 硬件设备的状态负责扫描附近的 NFC 标签。 QNearFieldTarget 表示检测到的 NFC 标签用于读取或写入数据。 NDEF 消息格式 NFC 数据交换格式NDEFNFC Data Exchange Format是 NFC 标签中存储和交换数据的标准格式。Qt 提供了 QNdefMessage 和 QNdefRecord 类来处理 NDEF 数据。 3. 配置 QtNFC 模块
在使用 QtNFC 模块前需要在 .pro 文件中添加模块依赖
QT nfc在代码中包含头文件
#include QNdefMessage
#include QNdefRecord
#include QNearFieldManager
#include QNearFieldTarget4. NFC 读写操作实现
4.1 NFC 标签读取
以下是一个简单的 NFC 读取示例代码
代码示例
#include QApplication
#include QNearFieldManager
#include QNearFieldTarget
#include QNdefMessage
#include QNdefRecord
#include QDebugclass NfcReader : public QObject {Q_OBJECTpublic:NfcReader(QObject *parent nullptr) {manager new QNearFieldManager(this);connect(manager, QNearFieldManager::targetDetected, this, NfcReader::onTargetDetected);connect(manager, QNearFieldManager::targetLost, this, NfcReader::onTargetLost);manager-startTargetDetection();}~NfcReader() {manager-stopTargetDetection();}private slots:void onTargetDetected(QNearFieldTarget *target) {qDebug() NFC 标签已检测到;// 读取 NDEF 消息connect(target, QNearFieldTarget::ndefMessageRead, this, [](const QNdefMessage message) {for (const QNdefRecord record : message) {qDebug() 记录类型 record.typeNameFormat();qDebug() 记录内容 record.payload();}target-deleteLater();});target-readNdefMessages();}void onTargetLost(QNearFieldTarget *target) {qDebug() NFC 标签已丢失;target-deleteLater();}private:QNearFieldManager *manager;
};int main(int argc, char *argv[]) {QApplication app(argc, argv);NfcReader reader;return app.exec();
}功能解析
使用 QNearFieldManager 开始目标检测。当检测到 NFC 标签时触发 targetDetected 信号。使用 readNdefMessages 方法读取标签中的 NDEF 消息。通过解析 QNdefMessage 和 QNdefRecord 获取记录内容。 4.2 NFC 标签写入
以下是一个 NFC 写入示例代码
代码示例
#include QApplication
#include QNearFieldManager
#include QNearFieldTarget
#include QNdefMessage
#include QNdefRecord
#include QDebugclass NfcWriter : public QObject {Q_OBJECTpublic:NfcWriter(QObject *parent nullptr) {manager new QNearFieldManager(this);connect(manager, QNearFieldManager::targetDetected, this, NfcWriter::onTargetDetected);manager-startTargetDetection();}~NfcWriter() {manager-stopTargetDetection();}private slots:void onTargetDetected(QNearFieldTarget *target) {qDebug() NFC 标签已检测到开始写入数据;// 创建 NDEF 消息QNdefMessage message;QNdefRecord record(QNdefRecord::Mime);record.setPayload(Hello, NFC!.toUtf8());message.append(record);// 写入数据connect(target, QNearFieldTarget::requestCompleted, this, []() {qDebug() 数据写入成功;});connect(target, QNearFieldTarget::error, this, [](QNearFieldTarget::Error error) {qDebug() 写入失败 error;});target-writeNdefMessages(message);}private:QNearFieldManager *manager;
};int main(int argc, char *argv[]) {QApplication app(argc, argv);NfcWriter writer;return app.exec();
}功能解析
使用 QNdefMessage 和 QNdefRecord 创建数据消息。调用 writeNdefMessages 方法向 NFC 标签写入数据。监听 requestCompleted 和 error 信号判断写入结果。 5. 使用注意事项 硬件支持 NFC 功能依赖设备的硬件支持。在开发前请确保目标设备具备 NFC 功能。 权限配置 在 Android 平台上需要在 AndroidManifest.xml 文件中添加 NFC 权限 uses-permission android:nameandroid.permission.NFC /NDEF 支持 并非所有 NFC 标签都支持 NDEF 消息。在操作 NFC 标签时请确认其支持的协议。 数据安全 NFC 通信距离短但仍需注意数据加密和防止标签克隆。 6. 总结
本文详细介绍了 NFC 的读写模式及其在 Qt 中的实现方法。通过 QtNFC 模块开发者可以轻松实现 NFC 标签的读取和写入功能。NFC 读写模式在支付、身份验证和物联网设备交互等场景中具有广泛的应用。希望本文的示例代码和注意事项能为您开发 NFC 应用提供帮助 文章转载自: http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.bppml.cn.gov.cn.bppml.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.kphyl.cn.gov.cn.kphyl.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.bdsyu.cn.gov.cn.bdsyu.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn