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

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

Python

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)) % 256

apptypestr="ohos.com:pkg".encode('gbk')
packagestr = "weixin://dl/business/?ticket=la0041f2cedf9630c9c7cc8c8715ac0c6".encode('gbk')
status = Objdll.tagbuf_addapp1(packagestr, len(packagestr),apptypestr,len(apptypestr)) % 256

self.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 = 16
                else:
                    myctrlword = 0
                status = Objdll.forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256
                if (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 = 0
                afi=0
                status = Objdll.forumtype5_write_ndeftag(myctrlword,afi, mypiccserial) % 256
                if (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 = 210
                    else:
                        myctrlword = 146
                    status = Objdll.piccclear_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256
                else:
                    if self.checkBox_before.isChecked():
                        myctrlword = 210
                    else:
                        myctrlword = 146
                    if self.checkBox_later.isChecked():  #写标签后给标签加密保护
                        myctrlword=myctrlword+4
                    status = Objdll.piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey,newpicckey) % 256
                if (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 = 64
                else:
                    myctrlword = 0
                status = Objdll.forumtype4_write_ndeftag(myctrlword, mypiccserial,mypiccseriallen,newpicckey) % 256
                if (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#

cs 复制代码
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源码

vbnet 复制代码
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 Byte
    
Dim 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 Long

Dim taginfstr As String

Dim packagestr As String
Dim packagestrlen As Long
    
languagecodestr = "en"      '语言编码,英文为en,中文为zh
languagecodestrlen = 2

noteinf = ""
If isexcel Then
    getexcelinf2
End If
    
If Trim(Text18.Text) = "" And Trim(Text5.Text) = "" Then
    excelclos
    MsgBox "请输入要写入的URL!", vbCritical + vbOKOnly, "提示"
    Text18.SetFocus
    Exit Sub
End If
    
CheckCardType
    
titlestr = 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 = uristr

packagestr = Trim(Text5.Text)
packagestrlen = LenB(StrConv(packagestr, vbFromUnicode))
If packagestrlen > 0 Then taginfstr = taginfstr + "," + packagestr Else taginfstr = taginfstr + ","

taginfstr = noteinf + "," + taginfstr + "," + cardstr

If 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、Ntag2
    tagbuf_forumtype4_clear
    If uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0
    If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0
    If (status + status1 = 0) Then
        If Check3.Value > 0 Then myctrlword = &H10 Else myctrlword = 0
        status = forumtype2_write_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))
        dispstr = "NFC_Forum_Type2Uid:" + cardstr + ",写入Url+App包名"
        dispriv dispstr, status
        
'        WritDevBufferInf taginfstr
        WritDevBufferCSV taginfstr
        
        If (Check3.Value > 0 And Check2.Value < 1) Or (Check3.Value < 1 And Check2.Value > 0) Then NtagKeyEn
    Else
        dispstr = "NFC_Forum_Type2Uid:" + cardstr + ",生成Url+App包名NDEF记录"
        dispriv dispstr, status
    End If
    
ElseIf CardType = 2 Then    'ForumType5、15693
    tagbuf_forumtype4_clear
    If uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0
    If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0
    If (status + status1 = 0) Then
        myctrlword = 0
        afi = 0
        status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial(0))
        dispstr = "NFC_Forum_Type5Uid:" + cardstr + ",写入Url+App包名"
        dispriv dispstr, status
        
'        WritDevBufferInf taginfstr
        WritDevBufferCSV taginfstr
        
        If Check2.Value > 0 Then status = iso15693lockblock(0, 1, VarPtr(mypiccserial(0)))  '15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,请谨慎锁定
    Else
        dispstr = "NFC_Forum_Type5Uid:" + cardstr + ",生成Url+App包名NDEF记录"
        dispriv dispstr, status
    End If
    
ElseIf CardType = 3 Then    'MifareClassIc
    tagbuf_clear
    If uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0
    If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0
    If (status + status1 = 0) Then
        If 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 taginfstr
        WritDevBufferCSV taginfstr
        
        dispriv dispstr, status
    Else
        dispstr = "MifareClassUid:" + cardstr + ",生成Url+App包名NDEF记录"
        dispriv dispstr, status
    End If
    
ElseIf CardType = 4 Then    'ForumType4
    tagbuf_forumtype4_clear
    If uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0
    If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0
    If (status + status1 = 0) Then
        If Check3.Value > 0 Then myctrlword = &H40 Else myctrlword = 0
        status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
        dispstr = "NFC_Forum_Type4Uid:" + cardstr + ",写入Url+App包名"
        
'        WritDevBufferInf taginfstr
        WritDevBufferCSV taginfstr
        
        dispriv dispstr, status
    Else
        dispstr = "NFC_Forum_Type4Uid:" + cardstr + ",生成Url+App包名NDEF记录"
        dispriv dispstr, status
    End If
End If
相关推荐
计算机-秋大田10 小时前
基于微信小程序的校园失物招领系统设计与实现(LW+源码+讲解)
java·前端·后端·微信小程序·小程序·课程设计
Goat恶霸詹姆斯11 小时前
微信小程序压缩图片
微信小程序·小程序
计算机-秋大田12 小时前
基于微信小程序的汽车保养系统设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
程序员徐师兄14 小时前
Java 基于 SpringBoot 的校园外卖点餐平台微信小程序(附源码,部署,文档)
java·spring boot·微信小程序·校园外卖点餐·外卖点餐小程序·校园外卖点餐小程序
Q_274378510915 小时前
springboot基于微信小程序的周边游小程序
spring boot·微信小程序·小程序
计算机学姐16 小时前
基于微信小程序的民宿预订管理系统
java·vue.js·spring boot·后端·mysql·微信小程序·小程序
寰宇软件19 小时前
PHP防伪溯源一体化管理系统小程序
小程序·uni-app·vue·php
trabecula_hj19 小时前
微信小程序中实现进入页面时数字跳动效果(自定义animate-numbers组件)
微信小程序·小程序
菜鸟码神19 小时前
微信小程序隐藏右侧胶囊按钮,分享和关闭即右侧三个点和小圆圈按钮
微信小程序·小程序
大叔_爱编程21 小时前
wx035基于springboot+vue+uniapp的校园二手交易小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计