NDEF 全称 NFC data exchange format 即 nfc 数据交换格式,是一种标准化的数据格式,可用于在任何兼容的NFC设备与另一个NFC设备或标签之间交换信息。数据格式由NDEF消息和NDEF记录组成。
NDEF信息可以写到不同类型的NFC芯片中,如Ntag系列芯片标、15693系列芯片、MifareClassic系列芯片、Forum_Type4_Tag标签等,不同类型的芯片NDEF信息的存储方式也略有不同,这就大大增加了NDEF信息写入、读取的难度。
广州荣士电子将各种不同类型的NDEF记录类型的写入、读取方式都函数化,开发人员不需再了解复杂的NDEF记录格式,只需调用相应的函数就可快速写入正确的NDEF信息。
本示例使用的发卡器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
Public Declare Function piccclear_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte) As Byte
Public Declare Function piccwrite_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte, ByRef newkey As Byte) As Byte
Public Declare Function piccread_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte) As Byte
Public Declare Sub tagbuf_clear Lib "OUR_MIFARE.dll" ()
Public Declare Function tagbuf_addtext Lib "OUR_MIFARE.dll" (ByVal languagecodestr As String, ByVal languagecodestrlen As Long, ByVal textstr As String, ByVal textstrlen As Long) As Byte
Public Declare Function tagbuf_adduri Lib "OUR_MIFARE.dll" (ByVal languagecodestr As String, ByVal languagecodestrlen As Long, ByVal titlestr As String, ByVal titlestrlen As Long, ByVal uriheaderindex As Long, ByVal uristr As String, ByVal uristrlen As Long) As Byte
Public Declare Function tagbuf_addbusinesscard Lib "OUR_MIFARE.dll" (ByVal infostr As String, ByVal infostrlen As Long) As Byte
Public Declare Function tagbuf_addwifi Lib "OUR_MIFARE.dll" (ByVal ssidstr As String, ByVal ssidstrlen As Long, ByVal authtype As Long, ByVal crypttype As Long, ByVal keystr As String, ByVal keystrlen As Long) As Byte
Public Declare Function tagbuf_addbluetooth Lib "OUR_MIFARE.dll" (ByVal blenamestr As String, ByVal blenamestrlen As Long, ByRef blemac As Byte) As Byte
Public Declare Function tagbuf_addapp Lib "OUR_MIFARE.dll" (ByVal packagestr As String, ByVal packagestrlen As Long) As Byte
Public Declare Function tagbuf_adddata Lib "OUR_MIFARE.dll" (ByVal typestr As String, ByVal typestrlen As Long, ByVal datastr As String, ByVal datastrlen As Long) As Byte
Public Declare Sub tagbuf_read Lib "OUR_MIFARE.dll" (ByVal revstr As String, ByRef revstrlen As Long, ByRef recordnumber As Long)
Public Declare Sub tagbuf_forumtype4_clear Lib "OUR_MIFARE.dll" ()
Public Declare Function forumtype4request Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte) As Byte
Public Declare Function forumtype4_write_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte, ByRef ndefwritekey As Byte) As Byte
Public Declare Function forumtype4_read_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte, ByRef ndefreadkey As Byte) As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim languagecodestr As String
Dim languagecodestrlen As Long
Dim textstr As String
Dim textstrlen As Long
languagecodestr = "en"
languagecodestrlen = 2
textstr = Trim(Text1.Text) '文本
textstrlen = LenB(StrConv(textstr, vbFromUnicode))
CheckCardType
If CardType = 3 Then 'MifareClass
tagbuf_clear
status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen)
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF文本"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF文本数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then 'forumtype4
tagbuf_forumtype4_clear
status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen)
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF文本"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF文本数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) 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
languagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2
titlestr = Trim(Text4.Text) '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))
uriheaderindex = Combo2.ListIndex '链接前缀
uristr = Trim(Text18.Text) '链接
uristrlen = LenB(StrConv(uristr, vbFromUnicode))
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF访问网址"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF访问网址数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF访问网址"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF访问网址数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) 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
languagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2
titlestr = Trim(Text6.Text) '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))
uriheaderindex = 0 '地理位置没有链接前缀
uristr = "geo:" & Trim(Text2.Text) & "," & Trim(Text3.Text) '地址位置
uristrlen = LenB(StrConv(uristr, vbFromUnicode))
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF地图坐标"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF地图坐标数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF地图坐标"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF地图坐标数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码
Dim blenamestr As String
Dim blenamestrlen As Long
Dim blemac(0 To 5) As Byte '蓝牙MAC地址
blenamestr = Trim(Text8.Text)
blenamestrlen = LenB(StrConv(blenamestr, vbFromUnicode))
macstr = Split(Text9, ":")
blemac(0) = "&H" & macstr(0)
blemac(1) = "&H" & macstr(1)
blemac(2) = "&H" & macstr(2)
blemac(3) = "&H" & macstr(3)
blemac(4) = "&H" & macstr(4)
blemac(5) = "&H" & macstr(5)
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_addbluetooth(blenamestr, blenamestrlen, blemac(0))
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF蓝牙连接"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF蓝牙连接数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_addbluetooth(blenamestr, blenamestrlen, blemac(0))
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF蓝牙连接"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF蓝牙连接数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码
Dim ssidstr As String
Dim ssidstrlen As Long
Dim authtype As Long
Dim crypttype As Long
Dim keystr As String
Dim keystrlen As Long
ssidstr = Trim(Text12.Text) 'WIFI名称
ssidstrlen = LenB(StrConv(ssidstr, vbFromUnicode))
authtype = Combo3.ListIndex '加密方式
crypttype = Combo4.ListIndex '加密算法
keystr = Trim(Text11.Text) '密码
keystrlen = LenB(StrConv(keystr, vbFromUnicode))
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen)
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF无线热点连接"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF无线热点连接数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen)
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF无线热点连接"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF无线热点连接数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码
Dim infostr As String
Dim infostrlen As Long
'名片信息
infostr = "BEGIN:VCARD" & Chr(10)
infostr = infostr & "VERSION:3.0" & Chr(10)
infostr = infostr & "FN:" & Trim(Text14.Text) & Chr(10) '姓名
infostr = infostr & "TEL:" & Trim(Text13.Text) & Chr(10) '电话
infostr = infostr & "ORG:" & Trim(Text15.Text) & Chr(10) '单位名称
infostr = infostr & "ADR:" & Trim(Text19.Text) & Chr(10) '地址
infostr = infostr & "EMAIL:" & Trim(Text16.Text) & Chr(10) '邮箱
infostr = infostr & "URL:" & Trim(Text17.Text) & Chr(10) '网址
infostr = infostr & "END:VCARD"
infostrlen = LenB(StrConv(infostr, vbFromUnicode))
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_addbusinesscard(infostr, infostrlen) '可以写入多条记录
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF电子名片"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF电子名片数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_addbusinesscard(infostr, infostrlen) '可以写入多条记录
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF电子名片"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF电子名片数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码
Dim languagecodestr As String
Dim languagecodestrlen As Long
Dim uristr As String
Dim uristrlen As Long
languagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2
uristr = Trim(Text10.Text) '呼叫电话
uristrlen = LenB(StrConv(uristr, vbFromUnicode))
CheckCardType
If CardType = 3 Then
tagbuf_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, "", 0, 5, uristr, uristrlen)
If (status = 0) Then
myctrlword = &H80 + &H10
status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))
dispstr = "MifareClassUid:" + cardstr + ",写入NDEF呼叫电话"
dispriv dispstr, status
Else
dispstr = "MifareClassUid:" + cardstr + ",生成NDEF呼叫电话数据"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
tagbuf_forumtype4_clear
status = tagbuf_adduri(languagecodestr, languagecodestrlen, "", 0, 5, uristr, uristrlen)
If (status = 0) Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF呼叫电话"
dispriv dispstr, status
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF呼叫电话数据"
dispriv dispstr, status
End If
End If
Dim dispstr As String
Dim status As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim myctrlword As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim ndefstr As String
Dim revstrlen(1) As Long
Dim recordnumber(1) As Long
CheckCardType
If CardType = 3 Then
myctrlword = &H80 + &H10
status = piccread_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))
If (status = 0) Then
ndefstr = String(2048, 0)
tagbuf_read ndefstr, revstrlen(0), recordnumber(0)
dispstr = "MifareClassUid:" + cardstr + ",读取卡内NDEF信息"
dispriv dispstr, status
Text22 = ndefstr
Else
dispstr = "MifareClassUid:" + cardstr + ",读取卡内NDEF信息"
dispriv dispstr, status
End If
ElseIf CardType = 4 Then
myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码
status = forumtype4_read_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))
If (status = 0) Then
ndefstr = String(2048, 0)
tagbuf_read ndefstr, revstrlen(0), recordnumber(0)
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",读取卡内NDEF信息"
dispriv dispstr, status
Text22 = ndefstr
Else
dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",读取卡内NDEF信息"
dispriv dispstr, status
End If
End If