当前位置: 首页 > news >正文

写NFC标签支持Android安卓Ohos纯血鸿蒙唤醒微信小程序

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1b8bEEGz&ft=t&id=61539185785 

Python

languagecodestr = "en".encode('gbk')
titlestr=self.lineEdit_title.text().strip().encode('gbk')
uriheaderindex = self.comboBox_headindex.currentIndex()
uristr=self.lineEdit_uri.text().strip().encode('gbk')
status = Objdll.tagbuf_adduri(languagecodestr, len(languagecodestr), titlestr, len(titlestr), uriheaderindex,uristr, len(uristr)) % 256apptypestr="ohos.com:pkg".encode('gbk')
packagestr = "weixin://dl/business/?ticket=la0041f2cedf9630c9c7cc8c8715ac0c6".encode('gbk')
status = Objdll.tagbuf_addapp1(packagestr, len(packagestr),apptypestr,len(apptypestr)) % 256self.WriteTag('NDEF缓冲区数据')def WriteTag(self,funcname):     #将NDEF缓冲区数据写入各种不同类型的NFC标签,清除标签内NDEF数据mypiccserial = bytes(8)mypiccseriallen=bytes(1)try:cardtyep = self.checkcardtype()  #判断发卡器上的标签类型if(cardtyep==1):        #写forumtype2 Ntag2标签if self.checkBox_before.isChecked():myctrlword = 16else:myctrlword = 0status = Objdll.forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType2,UID:'for num in range(0, 7):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:'+cardno)if (self.checkBox_before.isChecked() and not self.checkBox_later.isChecked()) or (not self.checkBox_before.isChecked() and self.checkBox_later.isChecked() or (self.checkBox_before.isChecked() and self.checkBox_later.isChecked())):self.NtagkeyEn()else:self.ListErrInf(status)elif(cardtyep==2):       #写forumtype5 15693标签myctrlword = 0afi=0status = Objdll.forumtype5_write_ndeftag(myctrlword,afi, mypiccserial) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType5,UID:'for num in range(0, 8):cardno = cardno + '%02X' % (mypiccserial[num])if self.checkBox_later.isChecked():    #15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,请谨慎锁定Objdll.iso15693lockblock(0,1,mypiccserial)self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif (cardtyep == 3):  # 写MifareClassic标签if(funcname=='NDEF_Clear'):if self.checkBox_before.isChecked():myctrlword = 210else:myctrlword = 146status = Objdll.piccclear_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256else:if self.checkBox_before.isChecked():myctrlword = 210else:myctrlword = 146if self.checkBox_later.isChecked():  #写标签后给标签加密保护myctrlword=myctrlword+4status = Objdll.piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey,newpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'MifareClass,UID:'for num in range(0, 4):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif(cardtyep==4):       #写forumtype4 标签if self.checkBox_before.isChecked():myctrlword = 64else:myctrlword = 0status = Objdll.forumtype4_write_ndeftag(myctrlword, mypiccserial,mypiccseriallen,newpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType4,UID:'for num in range(0, mypiccseriallen[0]):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif (cardtyep == 23):self.ListErrInf(23)else:self.ListErrInf(8)except:self.ListErrInf(8)

C#

string languagecodestr = "en";  //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;string titlestr = ""; //标题
int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度int uriheaderindex = 0;   //前缀string uristr = "weixin://dl/business/?ticket=la0041f2cedf9630c9c7cc8c8715ac0c6";   //uri
int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度string packagestr = "com.tencent.mm" ;   //APP名称            
int packagestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(packagestr).Length; //APP名称长度string apptypestr="ohos.com:pkg";
int apptypelen=System.Text.Encoding.GetEncoding(936).GetBytes(apptypestr).Length; byte status0 = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);	
byte status1=  tagbuf_addapp1(packagestr, packagestrlen,apptypestr,apptypelen);
if (status0 +status1==0 ){bool havelock = checkBox1.Checked;             //卡片是否已加锁  bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息WriteDataBufToTag("NDEF组合标签", havelock, KeyEn);
}private void WriteDataBufToTag(string NDEFinfo, bool havelock, bool keyEn)
{byte status;byte afi;byte myctrlword;//控制字byte[] mypiccserial = new byte[8];//卡序列号byte[] mypiccseriallen = new byte[1];byte[] oldpicckey = new byte[6];  //卡片旧密码byte[] newpicckey = new byte[6];  //卡片新密码oldpicckey[0] = 0x19; oldpicckey[1] = 0x74; oldpicckey[2] = 0x02; oldpicckey[3] = 0x02; oldpicckey[4] = 0x01; oldpicckey[5] = 0x11;//为防止测试中忘记以设定的密码,标签统一用此组密码加密,客户可自行设置其他的标签保护密码newpicckey[0] = 0x19; newpicckey[1] = 0x74; newpicckey[2] = 0x02; newpicckey[3] = 0x02; newpicckey[4] = 0x01; newpicckey[5] = 0x11;int cardtype = checkcardtype();switch (cardtype){case 1:     //Ntag2x标签if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);if (status == 0){NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能//if (KeyEn) { NtagPageLock(); }  锁定Ntag2标签数据块,锁定后不可再改修改,请谨慎使用pcdbeep(38);string carduid = "Ntag2UID:";for (int i = 0; i < 7; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 2:     //15693标签myctrlword = 0;afi = 0;status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial);if (status == 0){//if (KeyEn)    //15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,仅在确定需要才开启此段代码。//{//    status = iso15693lockblock(34, 1, mypiccserial);//}pcdbeep(38);string carduid = "15693UID:";for (int i = 0; i < 8; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 3:             //MifareClass标签if (havelock) { myctrlword = 0x80 + 0x40 + 0x10; } else { myctrlword = 0x80 + 0x10; }if (keyEn) { myctrlword = (byte)(myctrlword + 0x04); }status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);if (status == 0){pcdbeep(38);string carduid = "MifareClassUID:";for (int i = 0; i < 4; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 4:     //ForumType4标签myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);if (status == 0){pcdbeep(38);string carduid = "ForumType4UID:";for (int i = 0; i < mypiccseriallen[0]; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;default:MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);break;}
}

VB源码 

Dim dispstr As String
Dim status As Byte
Dim status1 As Byte
Dim afi As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 7) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim languagecodestr As String
Dim languagecodestrlen As Long
Dim titlestr As String
Dim titlestrlen As Long
Dim uriheaderindex As Long
Dim uristr As String
Dim uristrlen As LongDim taginfstr As StringDim packagestr As String
Dim packagestrlen As Longlanguagecodestr = "en"      '语言编码,英文为en,中文为zh
languagecodestrlen = 2noteinf = ""
If isexcel Thengetexcelinf2
End IfIf Trim(Text18.Text) = "" And Trim(Text5.Text) = "" ThenexcelclosMsgBox "请输入要写入的URL!", vbCritical + vbOKOnly, "提示"Text18.SetFocusExit Sub
End IfCheckCardTypetitlestr = Trim(Text4.Text)     '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))uriheaderindex = Combo2.ListIndex   '链接前缀uristr = Trim(Text18.Text)  '链接
If Check4.Value > 0 Then uristr = uristr + cardstr
uristrlen = LenB(StrConv(uristr, vbFromUnicode))
If Combo2.ListIndex > 0 Then taginfstr = Trim(Combo2.Text) + uristr Else taginfstr = uristrpackagestr = Trim(Text5.Text)
packagestrlen = LenB(StrConv(packagestr, vbFromUnicode))
If packagestrlen > 0 Then taginfstr = taginfstr + "," + packagestr Else taginfstr = taginfstr + ","taginfstr = noteinf + "," + taginfstr + "," + cardstrIf Len(Trim(outnote1.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote1.Text) Else taginfstr = taginfstr + ",  "
If Len(Trim(outnote2.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote2.Text) Else taginfstr = taginfstr + ",  "
If Len(Trim(outnote3.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote3.Text) Else taginfstr = taginfstr + ",  "If CardType = 1 Then    'ForumType2、Ntag2tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H10 Else myctrlword = 0status = forumtype2_write_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))dispstr = "NFC_Forum_Type2Uid:" + cardstr + ",写入Url+App包名"dispriv dispstr, status'        WritDevBufferInf taginfstrWritDevBufferCSV taginfstrIf (Check3.Value > 0 And Check2.Value < 1) Or (Check3.Value < 1 And Check2.Value > 0) Then NtagKeyEnElsedispstr = "NFC_Forum_Type2Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 2 Then    'ForumType5、15693tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) Thenmyctrlword = 0afi = 0status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial(0))dispstr = "NFC_Forum_Type5Uid:" + cardstr + ",写入Url+App包名"dispriv dispstr, status'        WritDevBufferInf taginfstrWritDevBufferCSV taginfstrIf Check2.Value > 0 Then status = iso15693lockblock(0, 1, VarPtr(mypiccserial(0)))  '15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,请谨慎锁定Elsedispstr = "NFC_Forum_Type5Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 3 Then    'MifareClassIctagbuf_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H80 + &H40 + &H10 + &H2 Else myctrlword = &H80 + &H10 + &H2  'MifareClass卡是否已经加有保护密码If Check2.Value > 0 Then myctrlword = myctrlword + &H4  '写入NDEF数据后 并加上保护密码status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入Url+App包名"'        WritDevBufferInf taginfstrWritDevBufferCSV taginfstrdispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 4 Then    'ForumType4tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H40 Else myctrlword = 0status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4Uid:" + cardstr + ",写入Url+App包名"'        WritDevBufferInf taginfstrWritDevBufferCSV taginfstrdispriv dispstr, statusElsedispstr = "NFC_Forum_Type4Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd If
End If


http://www.mrgr.cn/news/79365.html

相关文章:

  • Cesium进阶教程——自定义图形、外观、绘图基础、现有着色器移植至Cesium、ShadowMapping、视频GIS、模型压平、卷帘
  • 【代码随想录|动态规划】
  • SpringBoot中使用MyBatis-Plus详细介绍
  • 字符编码讲解(C#)
  • Zigbee智能照明系统实现方案
  • OpenCV与AI深度学习|16个含源码和数据集的计算机视觉实战项目(建议收藏!)
  • java面向对象实验——扫雷+24点
  • windsurf简介
  • [软件工程]九.可依赖系统(Dependable Systems)
  • 多层感知机imdb情感分析分块第一部分
  • 大型网站演化实例
  • Java---每日小题
  • 【网盘系统】递归删除批量文件、从回收站恢复文件、彻底删除文件
  • PS核心知识点
  • 【组件封装】uniapp vue3 封装一个完整的Tabs(标签页)组件教程,功能由简到杂实现讲解。
  • Python实现BBS论坛自动签到【steamtools论坛】
  • 2023 年“泰迪杯”数据分析技能赛B 题企业财务数据分析与造假识别
  • Java后端面试模板(技术面)
  • [软件工程]十.可靠性工程(reliable engineering)
  • android studio 读写文件操作(应用场景二)
  • 代码随想录-算法训练营day41(动态规划04:01背包,01背包滚动数组,分割等和子集)
  • 信奥常考点:二叉树的构建(已知中序和 前序或后序 的情况下)
  • 网络原理之 TCP 协议
  • pubmed关键词搜索技能1:待更新
  • No APK Signature Scheme v2 signature in package
  • T C P