Qt C++读写NFC标签NDEF网址URI
本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d292c1biFgjSs&ft=t&id=615391857885
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "QLibrary"
#include "QMessageBox"
#include <exception>
unsigned char mypicoldkey[6]={0x19,0x74,0x02,0x02,0x01,0x11}; //旧密钥
unsigned char mypicnewkey[6]={0x19,0x74,0x02,0x02,0x01,0x11}; //新密钥,可自行设置
//本示例可在windows、linux系统内编译、运行,自动判断windows系统加载DLL库、linux系统加载SO库----------------------------------------------------------------------------------------------------------------------------------------------
//让读写器发出声音
typedef unsigned char (*pcdbeep)(unsigned long xms);
#ifdef Q_OS_WIN
pcdbeep mypcdbeep = (pcdbeep)QLibrary("OUR_MIFARE.dll").resolve("pcdbeep");
#else
pcdbeep mypcdbeep = (pcdbeep)QLibrary("./libOURMIFARE.so").resolve("pcdbeep");
#endif
//返回本读写器独一无二的设备编号
typedef unsigned char (*pcdgetdevicenumber)(unsigned char *devicenumber);
#ifdef Q_OS_WIN
pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("OUR_MIFARE.dll").resolve("pcdgetdevicenumber");
#else
pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("./libOURMIFARE.so").resolve("pcdgetdevicenumber");
#endif
//清空MifareClass卡类标签NDEF数据缓冲
typedef unsigned char (*tagbuf_clear)();
#ifdef Q_OS_WIN
tagbuf_clear mytagbuf_clear = (tagbuf_clear)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_clear");
#else
tagbuf_clear mytagbuf_clear = (tagbuf_clear)QLibrary("./libOURMIFARE.so").resolve("tagbuf_clear");
#endif
//清空ForumType4类标签NDEF数据缓冲
typedef unsigned char (*tagbuf_forumtype4_clear)();
#ifdef Q_OS_WIN
tagbuf_forumtype4_clear mytagbuf_forumtype4_clear = (tagbuf_forumtype4_clear)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_forumtype4_clear");
#else
tagbuf_forumtype4_clear mytagbuf_forumtype4_clear = (tagbuf_forumtype4_clear)QLibrary("./libOURMIFARE.so").resolve("tagbuf_forumtype4_clear");
#endif
//轻松读Ntag卡
typedef unsigned char (*piccreadex_ntag)(unsigned char ctrlword,unsigned char *serial,unsigned char *pickey,unsigned char blockadd,unsigned char blocksize,unsigned char *picdata);
#ifdef Q_OS_WIN
piccreadex_ntag mypiccreadex_ntag = (piccreadex_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccreadex_ntag");
#else
piccreadex_ntag mypiccreadex_ntag = (piccreadex_ntag)QLibrary("./libOURMIFARE.so").resolve("piccreadex_ntag");
#endif
//轻松读15693卡
typedef unsigned char (*iso15693readex)(unsigned char ctrlword,unsigned char afi,unsigned char startblock,unsigned char blocknum,unsigned char *uid,unsigned char *piccdatabuf);
#ifdef Q_OS_WIN
iso15693readex myiso15693readex = (iso15693readex)QLibrary("OUR_MIFARE.dll").resolve("iso15693readex");
#else
iso15693readex myiso15693readex = (iso15693readex)QLibrary("./libOURMIFARE.so").resolve("iso15693readex");
#endif
//轻松读M1卡
typedef unsigned char (*piccreadex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
#ifdef Q_OS_WIN
piccreadex mypiccreadex = (piccreadex)QLibrary("OUR_MIFARE.dll").resolve("piccreadex");
#else
piccreadex mypiccreadex = (piccreadex)QLibrary("./libOURMIFARE.so").resolve("piccreadex");
#endif
//寻forumtype4
typedef unsigned char (*forumtype4request)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen);
#ifdef Q_OS_WIN
forumtype4request myforumtype4request = (forumtype4request)QLibrary("OUR_MIFARE.dll").resolve("forumtype4request");
#else
forumtype4request myforumtype4request = (forumtype4request)QLibrary("./libOURMIFARE.so").resolve("forumtype4request");
#endif
//将ndef记录缓冲数据写入forumtype2标签
typedef unsigned char (*forumtype2_write_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
forumtype2_write_ndeftag myforumtype2_write_ndeftag = (forumtype2_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype2_write_ndeftag");
#else
forumtype2_write_ndeftag myforumtype2_write_ndeftag = (forumtype2_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype2_write_ndeftag");
#endif
//读取forumtype2标签信息
typedef unsigned char (*forumtype2_read_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
forumtype2_read_ndeftag myforumtype2_read_ndeftag = (forumtype2_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype2_read_ndeftag");
#else
forumtype2_read_ndeftag myforumtype2_read_ndeftag = (forumtype2_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype2_read_ndeftag");
#endif
//将ndef记录缓冲数据写入forumtype5 15693标签
typedef unsigned char (*forumtype5_write_ndeftag)(unsigned char ctrlword,unsigned char afi,unsigned char *serial);
#ifdef Q_OS_WIN
forumtype5_write_ndeftag myforumtype5_write_ndeftag = (forumtype5_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype5_write_ndeftag");
#else
forumtype5_write_ndeftag myforumtype5_write_ndeftag = (forumtype5_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype5_write_ndeftag");
#endif
//读取forumtype5 15693标签信息
typedef unsigned char (*forumtype5_read_ndeftag)(unsigned char ctrlword,unsigned char afi,unsigned char *serial);
#ifdef Q_OS_WIN
forumtype5_read_ndeftag myforumtype5_read_ndeftag = (forumtype5_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype5_read_ndeftag");
#else
forumtype5_read_ndeftag myforumtype5_read_ndeftag = (forumtype5_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype5_read_ndeftag");
#endif
//将ndef记录缓冲数据写入mifareclass标签
typedef unsigned char (*piccwrite_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey,unsigned char *newkey);
#ifdef Q_OS_WIN
piccwrite_ndeftag mypiccwrite_ndeftag = (piccwrite_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("piccwrite_ndeftag");
#else
piccwrite_ndeftag mypiccwrite_ndeftag = (piccwrite_ndeftag)QLibrary("./libOURMIFARE.so").resolve("piccwrite_ndeftag");
#endif
//读取mifareclass标签信息
typedef unsigned char (*piccread_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
piccread_ndeftag mypiccread_ndeftag = (piccread_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("piccread_ndeftag");
#else
piccread_ndeftag mypiccread_ndeftag = (piccread_ndeftag)QLibrary("./libOURMIFARE.so").resolve("piccread_ndeftag");
#endif
//将ndef记录缓冲数据写入forumtype4标签
typedef unsigned char (*forumtype4_write_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen,unsigned char *newkey);
#ifdef Q_OS_WIN
forumtype4_write_ndeftag myforumtype4_write_ndeftag = (forumtype4_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype4_write_ndeftag");
#else
forumtype4_write_ndeftag myforumtype4_write_ndeftag = (forumtype4_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype4_write_ndeftag");
#endif
//读取forumtype4标签信息
typedef unsigned char (*forumtype4_read_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen,unsigned char *oldkey);
#ifdef Q_OS_WIN
forumtype4_read_ndeftag myforumtype4_read_ndeftag = (forumtype4_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype4_read_ndeftag");
#else
forumtype4_read_ndeftag myforumtype4_read_ndeftag = (forumtype4_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype4_read_ndeftag");
#endif
//解析缓冲区内NDEF记录tagbuf_read
typedef unsigned char (*tagbuf_read)(unsigned char *mypiccdata,unsigned char *revstrlen,unsigned char *recordnumber);
#ifdef Q_OS_WIN
tagbuf_read mytagbuf_read = (tagbuf_read)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_read");
#else
tagbuf_read mytagbuf_read = (tagbuf_read)QLibrary("./libOURMIFARE.so").resolve("tagbuf_read");
#endif
//锁定Ntag标签页数据
typedef unsigned char (*picclock_ntag)(unsigned char locktype,unsigned char *mypicclockdata);
#ifdef Q_OS_WIN
picclock_ntag mypicclock_ntag = (picclock_ntag)QLibrary("OUR_MIFARE.dll").resolve("picclock_ntag");
#else
picclock_ntag mypicclock_ntag = (picclock_ntag)QLibrary("./libOURMIFARE.so").resolve("picclock_ntag");
#endif
//初始化Ntag卡,开启读写卡密码保护功能
typedef unsigned char (*piccinit_ntag)(unsigned char ctrlword,unsigned char *serial,unsigned char *pickey,unsigned char *configdata);
#ifdef Q_OS_WIN
piccinit_ntag mypiccinit_ntag = (piccinit_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccinit_ntag");
#else
piccinit_ntag mypiccinit_ntag = (piccinit_ntag)QLibrary("./libOURMIFARE.so").resolve("piccinit_ntag");
#endif
//生成NDEF文本记录数据缓冲tagbuf_addtext
typedef unsigned char (*tagbuf_addtext)(unsigned char *languagecodestr,int languagecodestrlen,unsigned char *textstr,int textstrlen);
#ifdef Q_OS_WIN
tagbuf_addtext mytagbuf_addtext = (tagbuf_addtext)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addtext");
#else
tagbuf_addtext mytagbuf_addtext = (tagbuf_addtext)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addtext");
#endif
//生成NDEF唤醒android.com:pkg APP
typedef unsigned char (*tagbuf_addapp)(unsigned char *packagestr,int packagestrlen);
#ifdef Q_OS_WIN
tagbuf_addapp mytagbuf_addapp = (tagbuf_addapp)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addapp");
#else
tagbuf_addapp mytagbuf_addapp = (tagbuf_addapp)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addapp");
#endif
//生成NDEF唤醒任意类型APP
typedef unsigned char (*tagbuf_addapp1)(unsigned char *packagestr,int packagestrlen,unsigned char *typestr,int typelen);
#ifdef Q_OS_WIN
tagbuf_addapp1 mytagbuf_addapp1 = (tagbuf_addapp1)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addapp1");
#else
tagbuf_addapp1 mytagbuf_addapp1 = (tagbuf_addapp1)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addapp1");
#endif
//生成NDEF网址URI记录数据缓冲tagbuf_adduri
typedef unsigned char (*tagbuf_adduri)(unsigned char *languagecodestr,int languagecodestrlen,unsigned char *textstr,int textstrlen,int uriheaderindex,unsigned char *uristr,int uristrlen);
#ifdef Q_OS_WIN
tagbuf_adduri mytagbuf_adduri = (tagbuf_adduri)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_adduri");
#else
tagbuf_adduri mytagbuf_adduri = (tagbuf_adduri)QLibrary("./libOURMIFARE.so").resolve("tagbuf_adduri");
#endif
//生成NDEF蓝牙连接数据缓冲
typedef unsigned char (*tagbuf_addbluetooth)(unsigned char *blenamestr,int blenamestrlen,unsigned char *blemac);
#ifdef Q_OS_WIN
tagbuf_addbluetooth mytagbuf_addbluetooth = (tagbuf_addbluetooth)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addbluetooth");
#else
tagbuf_addbluetooth mytagbuf_addbluetooth = (tagbuf_addbluetooth)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addbluetooth");
#endif
//生成NDEF热点wifi连接数据缓冲
typedef unsigned char (*tagbuf_addwifi)(unsigned char *ssidstr,int ssidstrlen,int authtype,int crypttype,unsigned char *keystr,int keystrlen);
#ifdef Q_OS_WIN
tagbuf_addwifi mytagbuf_addwifi = (tagbuf_addwifi)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addwifi");
#else
tagbuf_addwifi mytagbuf_addwifi = (tagbuf_addwifi)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addwifi");
#endif
//生成NDEF电子名片数据缓冲
typedef unsigned char (*tagbuf_addbusinesscard)(unsigned char *infostr,int infostrlen);
#ifdef Q_OS_WIN
tagbuf_addbusinesscard mytagbuf_addbusinesscard = (tagbuf_addbusinesscard)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addbusinesscard");
#else
tagbuf_addbusinesscard mytagbuf_addbusinesscard = (tagbuf_addbusinesscard)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addbusinesscard");
#endif
//生成NDEF新数据类型
typedef unsigned char (*tagbuf_adddata)(unsigned char *typestr,int typestrlen,unsigned char *datastr,int datastrlen);
#ifdef Q_OS_WIN
tagbuf_adddata mytagbuf_adddata = (tagbuf_adddata)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_adddata");
#else
tagbuf_adddata mytagbuf_adddata = (tagbuf_adddata)QLibrary("./libOURMIFARE.so").resolve("tagbuf_adddata");
#endif
//检测动态库文件是否在运行目录内----------------------------------------------------------------------------------------------------------------------------------------------------------
static bool checkdllos(){
#ifdef Q_OS_WIN
QLibrary mylib("OUR_MIFARE.dll");
if (!mylib.load()){ //判断windows系统下,OUR_MIFARE.dll是否在运行目录内
QMessageBox::information(NULL, "提示", "请将OUR_MIFARE.dll文件拷贝到生成exe文件相同目录下!");
return false;
}else{return true;}
#else
QLibrary mylib("./libOURMIFARE.so");
if (!mylib.load()){ //判断linux系统下,libOURMIFARE.so是否正运行目录内
QMessageBox::information(NULL, "提示", "请将libOURMIFARE.so文件拷贝到生成的运行文件相同目录下!");
return false;
}else{return true;}
#endif
}
//判断发卡器感应区内标签类型----------------------------------------------------------------------------------------------------------------------------------------------------------
static int checkcardtype(){
unsigned char status;
unsigned char myctrlword;
unsigned char mypiccserial[8] ;
unsigned char mypicdata[48] ;
unsigned char mypickey[6]={0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char mypiccseriallen[1] ;
myctrlword=0;
status = mypiccreadex_ntag(myctrlword, mypiccserial, mypicoldkey, 4, 1, mypicdata);
if(status==0){
return 1;
}else{
status = myiso15693readex(myctrlword,0,0,1,mypiccserial,mypicdata);
if(status==0){
return 2;
}else{
myctrlword=23;
status = mypiccreadex(myctrlword,mypiccserial,0,1,mypickey,mypicdata);
if(status==0){
return 3;
}else{
mypickey[0]=0xa0;mypickey[1]=0xa1;mypickey[2]=0xa2;mypickey[3]=0xa3;mypickey[4]=0xa4;mypickey[5]=0xa5;
status = mypiccreadex(myctrlword,mypiccserial,0,1,mypickey,mypicdata);
if(status==0){
return 3;
}else{
myctrlword=0;
status=myforumtype4request(myctrlword,mypiccserial,mypiccseriallen);
if(status==0 || status==52){
return 4;
}else{
return 0;
}
}
}
}
}
}
//Ntag标签开启或关闭密钥保护功能,推荐用此方法保护写入标签内的数据------------------------------------------------------------------------------------------------------------------------------------------------------
static int NtagKeyEn(unsigned char *mypiccserial,bool havekey,bool addkey){
unsigned char myctrlword;
unsigned char mypiccdata[16];
if(havekey){myctrlword=0x10;}else{myctrlword=0;}
if(addkey){ //开启密钥保护功能
mypiccdata[0]=0;mypiccdata[1]=0;mypiccdata[2]=0;mypiccdata[3]=4; //有效数据是从第4块开始的
myctrlword=myctrlword+1;
mypiccdata[4]=0; //允许密钥认证失败次数 ,0表示不限制次数
mypiccdata[5]=0;mypiccdata[6]=0;mypiccdata[7]=0; //启用计数器
myctrlword=myctrlword+2;
for(int i=0;i<4;i++){
mypiccdata[8+i]=mypicnewkey[i]; //新认证密钥
}
mypiccdata[12]=0x16;mypiccdata[13]=0x16;mypiccdata[14]=0;mypiccdata[15]=4;
myctrlword=myctrlword+4;
}else{
mypiccdata[0]=0;mypiccdata[1]=0;mypiccdata[2]=0;mypiccdata[3]=0xff;
myctrlword=myctrlword+1;
mypiccdata[4]=0;mypiccdata[5]=0;mypiccdata[6]=0;mypiccdata[7]=0;
myctrlword=myctrlword+2;
}
unsigned char status=mypiccinit_ntag(myctrlword, mypiccserial, mypicoldkey, mypiccdata);
}
//锁定ntag2数据页,锁定后标签不可以再次修改,请谨慎使用锁定功能----------------------------------------------------------------------------------------------------------------------------------------------------------
static int NtagPageLock(){
unsigned char status;
unsigned char mypicclockdata[4] ;
mypicclockdata[0]=0;mypicclockdata[1]=0;mypicclockdata[2]=0xff;mypicclockdata[3]=0xff;
status=mypicclock_ntag(0,mypicclockdata); //静态锁15块之前
mypicclockdata[0]=0xff;mypicclockdata[1]=0xff;mypicclockdata[2]=0xff;mypicclockdata[3]=0;
status=mypicclock_ntag(1,mypicclockdata); //动态锁15块之后
}
//将ndef记录缓冲数据写入不同类型的NFC标签----------------------------------------------------------------------------------------------------------------------------------------------------------
void MainWindow::WriteDataBufToTag(){
bool havelock;
bool keyen;
unsigned char status;
unsigned char myctrlword;
unsigned char mypiccserial[8] ;
unsigned char mypiccseriallen[1] ;
QString uidstr="" ;
int i;
if(ui->checkBox_before->isChecked()){havelock=true;}else{havelock=false;}
if(ui->checkBox_later->isChecked()){keyen=true;}else{keyen=false;}
int cardtype=checkcardtype();
switch (cardtype){
case 1:
if(havelock){myctrlword=0x10;} else{myctrlword=0x00;}
status=myforumtype2_write_ndeftag(myctrlword,mypiccserial,mypicoldkey);
if(status==0){
NtagKeyEn(mypiccserial,havelock,keyen);
mypcdbeep(30);
for (i = 0; i < 7; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
ui->listWidget->addItem("ForumType2Tag:"+uidstr+",NDEF记录缓冲写入成功!");
}else{
disperrinf(status);
}
break;
case 2:
myctrlword=0;
status=myforumtype5_write_ndeftag(myctrlword,0,mypiccserial);
if(status==0){
mypcdbeep(30);
for (i = 0; i < 8; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
ui->listWidget->addItem("ForumType5Tag:"+uidstr+",NDEF记录缓冲写入成功!");
}else{
disperrinf(status);
}
break;
case 3:
if(havelock){myctrlword=208;} else{myctrlword=144;}
if(keyen){myctrlword=myctrlword+4;}
status=mypiccwrite_ndeftag(myctrlword,mypiccserial,mypicoldkey,mypicnewkey);
if(status==0){
mypcdbeep(30);
for (i = 0; i < 4; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
ui->listWidget->addItem("MifareclassTag:"+uidstr+",NDEF记录缓冲写入成功!");
}else{
disperrinf(status);
}
break;
case 4:
myctrlword=0;
status=myforumtype4_write_ndeftag(myctrlword,mypiccserial,mypiccseriallen,mypicnewkey);
if(status==0){
mypcdbeep(30);
for (i = 0; i < mypiccseriallen[0]; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
ui->listWidget->addItem("ForumType4Tag:"+uidstr+",NDEF记录缓冲写入成功!");
}else{
disperrinf(status);
}
break;
default:
QMessageBox::critical(NULL, "提示", "请刷有效的NFC标签");
break;
}
}
//显示返回的错误代码信息-----------------------------------------------------------------------------------------------------------------------------------------------------------------
void MainWindow:: disperrinf(unsigned char result){
switch (result) {
case 8:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",未寻到卡,请重新拿开卡后再放到感应区!");
break;
case 9:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",有多张卡在感应区,寻卡过程中防冲突失败,读序列吗错误!");
break;
case 10:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",该卡可能已被休眠,无法选中卡片!");
break;
case 11:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",密码装载失败!");
break;
case 12:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",卡片密码认证失败!");
break;
case 13:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",读页数据失败,可能需要验证密码!");
break;
case 14:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",写页数据失败,可能需要验证密码!");
break;
case 18:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",写UID失败,此卡可能不是UID卡!");
break;
case 22:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",动态库或驱动程序异常!");
break;
case 23:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",驱动程序错误或发卡器尚未安装!");
break;
case 24:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",操作超时,一般是动态库没有反应!");
break;
case 25:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",发送字数不够!");
break;
case 26:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",驱动程序错误或发卡器尚未安装!");
break;
case 27:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",操作超时,一般是动态库没有反应!");
break;
case 28:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",发送字数不够!");
break;
default:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",未知的错误信息!");
break;
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_beep_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char status;
status = mypcdbeep(30);
if(status==0){
}else{
disperrinf(status);
}
}
void MainWindow::on_pushButton_getdevnum_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char devicenumber[4]; //设备编号缓冲
unsigned char status; //函数返回状态
status = mypcdgetdevicenumber(devicenumber);
if(status==0){
mypcdbeep(30);
QMessageBox::information(NULL, "提示", QString::asprintf("设备编号:%03d-%03d-%03d-%03d", devicenumber[0],devicenumber[1],devicenumber[2],devicenumber[3]));
}else{
disperrinf(status);
}
}
void MainWindow::on_pushButton_clearbuff_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
mytagbuf_forumtype4_clear();
mytagbuf_clear();
QMessageBox::information(NULL, "提示", "NDEF数据缓冲已经清除!");
}
void MainWindow::on_pushButton_writetag_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
WriteDataBufToTag();
}
void MainWindow::on_pushButton_text_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString languagecodestr = "en"; //语言编码,英文为en,中文为zh
QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
int languagecodestrlen=languagecodebuf.length();
QString textstr=ui->lineEdit_text->text(); //文本
QByteArray textbuf=textstr.toLocal8Bit();
int textstrlen=textbuf.length();
unsigned char status=mytagbuf_addtext((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF纯文本标签数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_comboBox_seleapp_currentTextChanged(const QString &arg1)
{
QString seleappstr=ui->comboBox_seleapp->currentText();
QStringList list = seleappstr.split("|");
ui->lineEdit_appname->setText(list.at(1));
}
void MainWindow::on_pushButton_App_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString packagestr=ui->lineEdit_appname->text().trimmed(); //app包名称
QByteArray packagebuf=packagestr.toLocal8Bit();
int packagestrlen=packagebuf.length();
QString typestr=ui->comboBox_appstype->currentText(); //app类型
QByteArray typebuf=typestr.toLocal8Bit();
int typestrlen=typebuf.length();
unsigned char status=mytagbuf_addapp1((unsigned char *)packagebuf.data(),packagestrlen,(unsigned char *)typebuf.data(),typestrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF启动应用数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Url_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString languagecodestr = "en"; //语言编码,英文为en,中文为zh
QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
int languagecodestrlen=languagecodebuf.length();
QString textstr=ui->lineEdit_title->text(); //标题
QByteArray textbuf=textstr.toLocal8Bit();
int textstrlen=textbuf.length();
int uriheaderindex = ui->comboBox_headindex->currentIndex(); //前缀
QString uristr=ui->lineEdit_uri->text(); //uri
QByteArray uribuf=uristr.toLocal8Bit();
int uristrlen=uribuf.length();
unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF网址URI记录数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Map_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString languagecodestr = "en"; //语言编码,英文为en,中文为zh
QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
int languagecodestrlen=languagecodebuf.length();
QString textstr=ui->lineEdit_mapname->text(); //标题
QByteArray textbuf=textstr.toLocal8Bit();
int textstrlen=textbuf.length();
int uriheaderindex = 0; //地理位置没有链接前缀
QString uristr="geo:" +ui->lineEdit_latitude->text().trimmed()+ ","+ui->lineEdit_longitude->text().trimmed() ; //uri
QByteArray uribuf=uristr.toLocal8Bit();
int uristrlen=uribuf.length();
unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF地图坐标数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Bluetooth_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString blenamestr=ui->lineEdit_Bluetoothname->text().trimmed(); //设备名称
QByteArray blenamebuf=blenamestr.toLocal8Bit();
int blenamestrlen=blenamebuf.length();
QString macstr=ui->lineEdit_Bluetoothmac->text().trimmed(); //mac
QStringList list = macstr.split(":");
unsigned char macbuf[6];
try{
for(int i=0;i<6;i++){
bool ok;
macbuf[i]=QString(list.at(i)).toInt(&ok,16);
}
}catch(_exception){
QMessageBox::critical(NULL, "提示", "MAC地址输入错误!");
return;
}
unsigned char status=mytagbuf_addbluetooth((unsigned char *)blenamebuf.data(),blenamestrlen,macbuf);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF蓝牙连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Wifi_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString ssidstr = ui->lineEdit_hostname->text(); //热点名称
QByteArray ssidbuf=ssidstr.toLocal8Bit();
int ssidstrlen=ssidbuf.length();
int authtype=ui->comboBox_auth->currentIndex(); //认证方式
int crypttype=ui->comboBox_Calculation->currentIndex(); //加密算法
QString keystr=ui->lineEdit_password->text(); //密钥
QByteArray keybuf=keystr.toLocal8Bit();
int keystrlen=keybuf.length();
unsigned char status=mytagbuf_addwifi((unsigned char *)ssidbuf.data(),ssidstrlen,authtype,crypttype,(unsigned char *)keybuf.data(),keystrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成WIFI无线连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Card_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString infostr = "BEGIN:VCARD\n" ;
infostr=infostr+"VERSION:3.0" + "\n";
infostr=infostr+"FN:" +ui->lineEdit_name->text()+ "\n";
infostr=infostr+"TEL:"+ui->lineEdit_tel->text() + "\n";
infostr=infostr+"ORG:"+ui->lineEdit_unilname->text() + "\n";
infostr=infostr+"ADR:" +ui->lineEdit_address->text()+ "\n";
infostr=infostr+"EMAIL:"+ui->lineEdit_email->text() + "\n";
infostr=infostr+"URL:" +ui->lineEdit_net->text()+ "\n";
infostr=infostr+"END:VCARD\n";
QByteArray infobuf=infostr.toLocal8Bit();
int infostrlen=infobuf.length();
unsigned char status=mytagbuf_addbusinesscard((unsigned char *)infobuf.data(),infostrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF电子名片数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Data_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString typestr=ui->lineEdit_datatype->text().trimmed(); //类型名称
QByteArray typebuf=typestr.toLocal8Bit();
int typestrlen=typebuf.length();
QString datastr=ui->lineEdit_data->text(); //数据
QByteArray databuf=datastr.toLocal8Bit();
int datastrlen=databuf.length();
unsigned char status=mytagbuf_adddata((unsigned char *)typebuf.data(),typestrlen,(unsigned char *)databuf.data(),datastrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF数据标签缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_Call_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
QString languagecodestr = "en"; //语言编码,英文为en,中文为zh
QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
int languagecodestrlen=languagecodebuf.length();
QString textstr=""; //标题
QByteArray textbuf=textstr.toLocal8Bit();
int textstrlen=textbuf.length();
int uriheaderindex = 5; //呼叫电话的链接前缀为5
QString uristr=ui->lineEdit_callnum->text().trimmed() ; //uri
QByteArray uribuf=uristr.toLocal8Bit();
int uristrlen=uribuf.length();
unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
if(status==0){
QMessageBox::information(NULL, "提示"," 生成NDEF呼叫电话数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
}else {
disperrinf(status);
}
}
void MainWindow::on_pushButton_cleatag_clicked()
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
mytagbuf_forumtype4_clear();
mytagbuf_clear();
WriteDataBufToTag();
}
void MainWindow::on_pushButton_clearinf_clicked()
{
ui->listWidget->clear();
ui->plainTextEdit_taginf->setPlainText("");
}
void MainWindow::on_pushButton_readtag_clicked()
{
bool havelock;
unsigned char status=8;
unsigned char myctrlword;
unsigned char mypiccserial[8] ;
unsigned char mypiccseriallen[1] ;
QString uidstr="" ;
int i;
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
ui->plainTextEdit_taginf->setPlainText("");
if(ui->checkBox_before->isChecked()){havelock=true;}else{havelock=false;} //标签是否已加密钥
int cardtype=checkcardtype();
switch (cardtype){
case 1:
if(havelock){myctrlword=0x10;} else{myctrlword=0x00;}
status=myforumtype2_read_ndeftag(myctrlword,mypiccserial,mypicoldkey);
uidstr="ForumType2Tag:";
for (i = 0; i < 7; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
break;
case 2:
myctrlword=0;
status=myforumtype5_read_ndeftag(myctrlword,0,mypiccserial);
uidstr="ForumType5Tag:";
for (i = 0; i < 8; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
break;
case 3:
myctrlword=144;
status=mypiccread_ndeftag(myctrlword,mypiccserial,mypicoldkey);
uidstr="MifareclassTag:";
for (i = 0; i < 4; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
break;
case 4:
myctrlword=0;
status=myforumtype4_read_ndeftag(myctrlword,mypiccserial,mypiccseriallen,mypicoldkey);
uidstr="ForumType4Tag:";
for (i = 0; i < mypiccseriallen[0]; i++) {
uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
}
break;
default:
QMessageBox::critical(NULL, "提示", "请刷有效的NFC标签");
break;
}
if(status==0){
ui->listWidget->addItem(uidstr+",读取标签内信息成功!");
mypcdbeep(30);
unsigned char mypiccdata[1024];
unsigned char revstrlen[2];
unsigned char recordnumber[2];
mytagbuf_read(mypiccdata,revstrlen,recordnumber);
QString str = QString::fromLocal8Bit(reinterpret_cast<const char *>(mypiccdata));
ui->plainTextEdit_taginf->setPlainText(str);
}
}