NDEF 全称 NFC data exchange format 即 nfc 数据交换格式,是一种标准化的数据格式,可用于在任何兼容的NFC设备与另一个NFC设备或标签之间交换信息。数据格式由NDEF消息和NDEF记录组成。

      NDEF信息可以写到不同类型的NFC芯片中,如Ntag系列芯片标、15693系列芯片、MifareClassic系列芯片、Forum_Type4_Tag标签等,不同类型的芯片NDEF信息的存储方式也略有不同,这就大大增加了NDEF信息写入、读取的难度。

        广州荣士电子将各种不同类型的NDEF记录类型的写入、读取方式都函数化,开发人员不需再了解复杂的NDEF记录格式,只需调用相应的函数就可快速写入正确的NDEF信息。

C#轻松读写NDEF智能海报_c#读写NDEF

本示例使用的发卡器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)

 NDEF函数声明
//------------------------------------------------------------------------------------------------------------------------------------------------------
        //外部函数声明:让设备发出声响
        [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdbeep", CallingConvention = CallingConvention.StdCall)]
        static extern byte pcdbeep(UInt32 xms);//xms单位为毫秒 
        
        //------------------------------------------------------------------------------------------------------------------------------------------------------    
        //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
        [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetdevicenumber", CallingConvention = CallingConvention.StdCall)]
        static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号 

        //------------------------------------------------------------------------------------------------------------------------------------------------------    
        //清空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);

        //-------------------------------------------------------------------------------------------------------------------------------------------------------
        //读取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);

        //-------------------------------------------------------------------------------------------------------------------------------------------------------
        //读取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 myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            string languagecodestr = "en";   //语言编码,英文为en,中文为zh
            int languagecodestrlen = languagecodestr.Length;

            string textstr = textBox1.Text.Trim();
            int textstrlen = System.Text.Encoding.GetEncoding(936).GetBytes(textstr).Length;

            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen); 
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen); 
                    if (status == 0)
                    {
                        myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
                        status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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;
            }
写入NDEF智能海报
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            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标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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);
                            MessageBox.Show("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;
            }
写入电子名片
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            string infostr = "BEGIN:VCARD"+"\n";  //语言编码,英文为en,中文为zh
            infostr = infostr + "VERSION:3.0" + "\n";
            infostr = infostr + "FN:" +textBox12.Text.Trim()  + "\n";   //姓名
            infostr = infostr + "TEL:" + textBox11.Text.Trim() + "\n";  //电话
            infostr = infostr + "ORG:" + textBox10.Text.Trim() + "\n";  //单位名称
            infostr = infostr + "ADR:" + textBox15.Text.Trim() + "\n";  //地址
            infostr = infostr + "EMAIL:" + textBox13.Text.Trim() + "\n";//邮箱
            infostr = infostr + "URL:" + textBox14.Text.Trim() + "\n";  //官网
            infostr = infostr + "END:VCARD" + "\n";

            int infostrlen = System.Text.Encoding.GetEncoding(936).GetBytes(infostr).Length; //名片长度

            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);                        
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("NDEF电子名片写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else { disperrinf(status); }
                    }
                    else { MessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                    break;
                case 4:     //ForumType4标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
                        status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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;
            }
写入WIFI连接 
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            string ssidstr =  textBox16.Text.Trim() ;   //热点名称
            int ssidstrlen = System.Text.Encoding.GetEncoding(936).GetBytes(ssidstr).Length; //热点名称长度

            int authtype = comboBox2.SelectedIndex; //认证方式
            int crypttype = comboBox3.SelectedIndex;//加密算法

            string keystr = textBox17.Text.Trim(); //密码
            int keystrlen = System.Text.Encoding.GetEncoding(936).GetBytes(keystr).Length; //密码长度

            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("NDEF无线连接写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else { disperrinf(status); }
                    }
                    else { MessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                    break;
                case 4:     //ForumType4标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
                        status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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;
            }
 写入地图坐标
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            string languagecodestr = "en";  //语言编码,英文为en,中文为zh
            int languagecodestrlen = languagecodestr.Length;

            string titlestr = textBox7.Text.Trim(); //标题
            int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度

            int uriheaderindex = 0;   //地理位置没有链接前缀

            string uristr = "geo:" + textBox6.Text.Trim() + "," + textBox8.Text.Trim();
            int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度

            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("NDEF地图坐标写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else { disperrinf(status); }
                    }
                    else { MessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                    break;
                case 4:     //ForumType4标签
                    tagbuf_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);
                            MessageBox.Show("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;
            }
写入蓝牙连接
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码
            byte[] newpicckey = new byte[6];  //卡片新密码
            byte[] mypiccseriallen = new byte[1];

            byte[] macbuf = new byte[6];        //设备MAC地址

            string blenamestr = textBox19.Text.Trim();   //设备名称
            int blenamestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(blenamestr).Length; //设备名称长度

            string[] macstr = textBox18.Text.Split(new char[2] { ':', ':' });//设备MAC地址
            try
            {
                macbuf[0] = (byte)Convert.ToInt32(macstr[0], 16);
                macbuf[1] = (byte)Convert.ToInt32(macstr[1], 16);
                macbuf[2] = (byte)Convert.ToInt32(macstr[2], 16);
                macbuf[3] = (byte)Convert.ToInt32(macstr[3], 16);
                macbuf[4] = (byte)Convert.ToInt32(macstr[4], 16);
                macbuf[5] = (byte)Convert.ToInt32(macstr[5], 16);
            }
            catch
            {
                MessageBox.Show("蓝牙设备的MAC地址输入错误,请输入正确的MAC地址!" , "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox18.Select();
                return;
            }

            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:             //MifareClass标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0x80 + 0x10;
                        status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("NDEF蓝牙连接写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else { disperrinf(status); }
                    }
                    else { MessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                    break;
                case 4:     //ForumType4标签
                    tagbuf_clear(); //清空标签数据缓冲
                    status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
                    if (status == 0)
                    {
                        myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
                        status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);
                        if (status == 0)
                        {
                            pcdbeep(38);
                            MessageBox.Show("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;
            }
 读取NDEF标签信息
byte status;
            byte myctrlword;//控制字
            byte[] mypiccserial = new byte[8];//卡序列号
            byte[] oldpicckey = new byte[6];  //卡片旧密码    
            byte[] mypiccseriallen = new byte[1];
            byte[] revstrlen=new byte[1];
            byte[] recordnumber=new byte[1] ;
            byte[] mypiccdata=new byte[2048];
            
            textBox21.Text = "";
            status = 255;
            int cardtype = checkcardtype();
            switch (cardtype)
            {
                case 1:     //Ntag2x标签
                    break;
                case 2:     //15693标签
                    break;
                case 3:     //MifareClass标签
                    myctrlword = 0x80 + 0x10;
                    status = piccread_ndeftag(myctrlword, mypiccserial, oldpicckey);
                    break;
                case 4:     //ForumType4标签
                    myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
                    status = forumtype4_read_ndeftag(myctrlword, mypiccserial, mypiccseriallen, oldpicckey);
                    break;
                default:
                    MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }

            if (status == 0)
            {
                pcdbeep(38);
                tagbuf_read(mypiccdata, revstrlen, recordnumber);
                string ndefstr = Encoding.Default.GetString(mypiccdata);
                textBox21.Text = ndefstr;
            }