#如何使用 Qt 5.6 在 Android 上启用 NFC

ops/2024/9/23 20:30:40/

如何使用 Qt 5.6 在 Android 上启用 NFC

NFC 技术在 Android 应用开发中变得越来越重要。在本文中,我将介绍如何使用 Qt 5.6 来实现 Android 上的 NFC 功能。这个教程基于一个创建于 8 年 8 个月前的问题,并在 7 年 3 个月前进行了修改,讨论了如何在 Android 手机上使用 Qt 的 NFC 模块读取 NFC 标签。

环境设置

根据 Qt 官方文档,从 Qt 5.6 版本开始,Qt 将支持 Android 的 NFC 功能。以下是关键步骤:

1. 从源码构建 Qt 5.6 并安装

由于 Qt 5.6 版本当时尚未正式发布,需要从源码构建并安装到 Qt Creator。可以参考 Qt 的官方构建指南 来进行构建。

2. 创建 NFC 测试应用

在 Qt 应用中,我们需要一个 QNearFieldManager 实例来处理 NFC 的检测。以下是一个简单的示例应用代码:

#include <QLabel>
#include <QVBoxLayout>
#include <QNearFieldManager>
#include <QNearFieldTarget>
#include <QDebug>
#include "window.h"Window::Window(QWidget *parent)
: QWidget(parent)
{nfcLabel_ = new QLabel(this);QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addWidget(nfcLabel_, 1);setLayout(mainLayout);setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));setWindowTitle(tr("NFC Test"));nfc_ = new QNearFieldManager(this);if (nfc_->isAvailable()) {nfcLabel_->setText("NFC available");} else {nfcLabel_->setText("NFC not available");qWarning() << "NFC not available";}nfc_->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess); // 可选nfc_->registerNdefMessageHandler(this, SLOT(handleNdefMessage(QNdefMessage,QNearFieldTarget*))); // 可选connect(nfc_, SIGNAL(targetDetected(QNearFieldTarget*)), this, SLOT(targetDetected(QNearFieldTarget*)));connect(nfc_, SIGNAL(targetLost(QNearFieldTarget*)), this, SLOT(targetLost(QNearFieldTarget*)));if (!nfc_->startTargetDetection()) {qWarning() << "NFC target detection could not be started";}
}Window::~Window()
{nfc_->stopTargetDetection();
}void Window::targetDetected(QNearFieldTarget * /*target*/)
{nfcLabel_->setText("Target detected");
}void Window::targetLost(QNearFieldTarget *target)
{nfcLabel_->setText("Target lost");target->deleteLater();
}void Window::handleNdefMessage(const QNdefMessage &/*message*/, QNearFieldTarget */*target*/)
{qDebug() << "Ndef Message";
}

3. 修改 AndroidManifest.xml

为了在 Android 上使用 NFC,我们需要修改 AndroidManifest.xml 文件。下面是一个示例文件:

<?xml version="1.0"?>
<manifest package="org.qtproject.example" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto"><application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="-- %%INSERT_APP_NAME%% --" android:theme="@android:style/Theme.Holo"><activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="-- %%INSERT_APP_NAME%% --" android:screenOrientation="unspecified" android:launchMode="singleTop"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter><!-- 添加此 intent-filter 以触发 targetDetected 和 targetLost 事件 --><intent-filter><action android:name="android.nfc.action.TAG_DISCOVERED"/><category android:name="android.intent.category.DEFAULT"/></intent-filter><meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/><meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/><meta-data android:name="android.app.repository" android:value="default"/><meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/><meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/><meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/><meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/><meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/><meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/><meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/><meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/><meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/><meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/><meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/><meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/><meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/></activity></application><uses-sdk android:minSdkVersion="10" android:targetSdkVersion="14"/><supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/><uses-feature android:name="android.hardware.nfc" android:required="true"/><uses-permission android:name="android.permission.NFC"/>
</manifest>

4. 解决应用启动问题

当 NFC 标签靠近设备时,应用会重新启动,这可能导致崩溃。为了解决这个问题,我们可以在 AndroidManifest.xml 中增加以下属性:

<activity ... android:alwaysRetainTaskState="true" android:launchMode="singleInstance">

5. 处理标签检测信号

确保在应用运行时处理标签检测信号,而不触发应用重新启动。

结论

通过修改和配置 AndroidManifest.xml 文件及调整代码逻辑,可以实现在 Android 上使用 Qt 5.6 成功读取 NFC 标签。如果你遇到问题,可能需要检查你的 NFC 硬件兼容性,或者为你的设备定制解决方案。希望这篇文章对你的开发工作有所帮助。如果你有其他问题或建议,欢迎留言讨论。


http://www.ppmy.cn/ops/54876.html

相关文章

熊猫烧香是什么?

熊猫烧香&#xff08;Worm.WhBoy.cw&#xff09;是一种由李俊制作的电脑病毒&#xff0c;于2006年底至2007年初在互联网上大规模爆发。这个病毒因其感染后的系统可执行文件图标会变成熊猫举着三根香的模样而得名。熊猫烧香病毒具有自动传播、自动感染硬盘的能力&#xff0c;以及…

Electron去掉窗口边框并添加关闭按钮

在 Electron 中&#xff0c;如果你想去掉默认的窗口边框&#xff08;frame&#xff09;并添加额外的按键&#xff0c;你可以通过以下步骤来实现&#xff1a; 去掉默认的窗口边框&#xff1a; 使用 BrowserWindow 的 frame 选项&#xff0c;并将其设置为 false 来创建一个无边…

spl实现循环计算

需求 需要对一批数据进行价格计算 这里面的一部分单价来自于历史记录&#xff0c;但是另外一部分的单价&#xff0c;需要边计算边存储 数据库结构 CREATE TABLE tbl_mix_trace_price (lot_id_out varchar(255) DEFAULT NULL COMMENT 产出,lot_id_in varchar(255) DEFAULT NULL…

【51单片机入门】速通定时器

文章目录 前言定时器是什么初始化定时器初始化的大概步骤TMOD寄存器C/T寄存器 触发定时器中断是什么中断函数定时器点亮led 总结 前言 在嵌入式系统的开发中&#xff0c;定时器是一个非常重要的组成部分。它们可以用于产生精确的时间延迟&#xff0c;或者在特定的时间间隔内触…

[PyTorch]:加速Pytorch 模型训练的几种方法(几行代码),最快提升八倍(附实验记录)

本篇文章转自&#xff1a;Some Techniques To Make Your PyTorch Models Train (Much) Faster 本篇博文概述了在不影响 PyTorch 模型准确性的情况下提高其训练性能的技术。为此&#xff0c;将 PyTorch 模型包装在 LightningModule 中&#xff0c;并使用 Trainer 类来实现各种训…

华为手机怎么打印文件?

关于华为手机打印的问题&#xff0c;如果您有打印机&#xff0c;并且已经成功和华为手机相连&#xff0c;在解决上就要容易很多。 具体操作如下&#xff1a; 选择文件 文件来源&#xff1a;华为手机上的文件可以来自多个应用&#xff0c;如图库、备忘录、文件管理等&#xf…

Webpack: 如何借助预处理器、PostCSS 等构建现代 CSS 工程环境

概述 在开发 Web 应用时&#xff0c;我们通常需要编写大量 JavaScript 代码 —— 用于控制页面逻辑&#xff1b;编写大量 CSS 代码 —— 用于调整页面呈现形式。问题在于&#xff0c;CSS 语言在过去若干年中一直在追求样式表现力方面的提升&#xff0c;工程化能力薄弱&#xff…

30、matlab现代滤波:维纳滤波/LMS算法滤波/小波变换滤波

1、前言 在 MATLAB 中&#xff0c;可以使用现代滤波方法如维纳滤波、LMS 算法滤波和小波变换滤波来处理信号和图像数据。这些方法在处理复杂信号和图像时具有很好的效果。以下是关于这些现代滤波方法的简要介绍和示例 MATLAB 代码&#xff1a; 维纳滤波&#xff1a; 维纳滤波是…