NDEF 全称 NFC data exchange format 即 nfc 数据交换格式,是一种标准化的数据格式,可用于在任何兼容的NFC设备与另一个NFC设备或标签之间交换信息。数据格式由NDEF消息和NDEF记录组成。
NDEF信息可以写到不同类型的NFC芯片中,如Forum_Type2_Tag系列NXP公司的Ntag2x芯片、国产复旦微的FM11NT021、FM11NT022、FM11NT041、FM11NT081标签;Forum_Type4_Tag;Forum_Type5_Tag系列的15693标签;MifareClassic系列芯片标签等,不同类型的芯片NDEF信息的存储方式也略有不同,这就大大增加了NDEF信息写入、读取的难度。
广州荣士电子将各种不同类型的NDEF记录类型的写入、读取方式都函数化,开发人员不需再了解复杂的NDEF记录格式,只需使用我们的发卡器并调用相应的函数就可快速写入正确的NDEF信息。
一、NDEF函数声明
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//清空MifareClass卡类标签NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_clear", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_clear();//
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//清空ForumType4类标签NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_forumtype4_clear", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_forumtype4_clear();//
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF文本类型数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addtext", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addtext(string languagecodestr, int languagecodestrlen, string textstr, int textstrlen);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_adddata", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_adddata(string typestr, int typestrlen, string datastr, int datastrlen);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF URI数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_adduri", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_adduri(string languagecodestr, int languagecodestrlen, string titlestr, int titlestrlen, int uriheaderindex, string uristr, int uristrlen);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF电子名片数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addbusinesscard", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addbusinesscard(string infostr, int infostrlen);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF热点连接数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addwifi", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addwifi(string ssidstr, int ssidstrlen,int authtype,int crypttype,string keystr,int keystrlen );
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF蓝牙连接数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addbluetooth", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addbluetooth(string blenamestr, int blenamestrlen, byte[] blemac);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF启动应用数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addapp", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addapp(string packagestr, int packagestrlen);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//解析数据缓冲中的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_read", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_read(byte[] revstr, byte[] revstrlen, byte[] recordnumber);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入MifareClasss标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccwrite_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccwrite_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey, byte[] newkey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//清空MifareClasss类NDEF标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccclear_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccclear_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//读取MifareClasss标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccread_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccread_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType4标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4_write_ndeftag(byte ctrlword, byte[] serial, byte[] seriallen, byte[] ndefwritekey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType4标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4_read_ndeftag(byte ctrlword, byte[] serial, byte[] seriallen, byte[] ndefwritekey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType5_15693 标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype5_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype5_write_ndeftag(byte ctrlword,byte afi, byte[] serial);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType5_15693标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype5_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype5_read_ndeftag(byte ctrlword, byte afi, byte[] serial);
//锁15693块数据----------------------------------------------------------------------------------------------------------------------------------------
[DllImport("OUR_MIFARE.dll", EntryPoint = "iso15693lockblock", CallingConvention = CallingConvention.StdCall)]
static extern byte iso15693lockblock(byte flags, byte blockaddr, byte[] uid);
//----------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType2_Ntag2x 标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype2_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype2_write_ndeftag(byte ctrlword, byte[] serial, byte[] picckey);
//----------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType2_FM11NT022 标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype2_write_ndeftag_fm", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype2_write_ndeftag_fm(byte ctrlword, byte[] serial, byte[] picckey);
//----------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType2_ntag2x标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype2_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype2_read_ndeftag(byte ctrlword, byte[] serial, byte[] picckey);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//读取Ntag卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex_ntag", CallingConvention = CallingConvention.StdCall)]
public static extern byte piccreadex_ntag(byte ctrlword, byte[] serial, byte[] picckey, byte blockadd, byte blocksize, byte[] piccdata);
//----------------------------------------------------------------------------------------------------------------------------------------------------
//开启、关闭ntag卡密码保护功能
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccinit_ntag", CallingConvention = CallingConvention.StdCall)]
public static extern byte piccinit_ntag(byte ctrlword, byte[] serial, byte[] picckey, byte[] piccdata);
//----------------------------------------------------------------------------------------------------------------------------------------------------
//读取15693卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "iso15693readex", CallingConvention = CallingConvention.StdCall)]
static extern byte iso15693readex(byte flags, byte afi, byte startblock, byte blocknum, byte[] uid, byte[] revbuf);
//---------------------------------------------------------------------------------------------------------------------------------------------------
//轻松读MifareClass卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex", CallingConvention = CallingConvention.StdCall)]
static extern byte piccreadex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] picckey, byte[] piccdata0_2);
//---------------------------------------------------------------------------------------------------------------------------------------------------
//轻松读forumtype4卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4request", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4request(byte ctrlword, byte[] serial, byte[] seriallen);
二、轻松写入NDEF智能海报
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]; //卡片新密码
bool havelock = checkBox1.Checked; //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ; //是否启用密码保护NDEF信息
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;
string languagecodestr = "en"; //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;
string titlestr = textBox4.Text.Trim(); //标题
int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度
int uriheaderindex = comboBox1.SelectedIndex; //前缀
string uristr = textBox5.Text.Trim(); //uri
int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度
int cardtype = checkcardtype();
Switch (cardtype)
{
case 1: //Ntag2x标签
tagbuf_forumtype4_clear(); //清空标签数据缓冲
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
if (status == 0)
{
if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
if (status == 0)
{
NtagKeyEn(mypiccserial); //开启或关闭Ntag2x标签密码保护功能
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); }
}
else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
break;
case 2: //15693标签
tagbuf_forumtype4_clear(); //清空标签数据缓冲
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
if (status == 0)
{
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); }
}
else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
break;
case 3: //MifareClass标签
tagbuf_clear(); //清空标签数据缓冲
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
if (status == 0)
{
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); }
}
else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
break;
case 4: //ForumType4标签
tagbuf_forumtype4_clear(); //清空标签数据缓冲
status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
if (status == 0)
{
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); }
}
else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
break;
default:
MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
|