C#轻松读写NDEF智能海报

news2025/2/24 6:14:57

       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)

 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;
            }    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/743524.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python算法笔记(1)-时间复杂度、空间复杂度

Python算法笔记(1)-时间复杂度 1.时间复杂度 时间复杂度是一个描述算法的运行时间的一个函数,它描述了算法的运行时间和输入数据的规模之间的关系,时间复杂度的表示方法用O表示,时间复杂度也用来考察输入值无限趋近无…

【嵌入式Qt开发入门】Qt如何使用多线程——继承QObject的线程

QObject 在上篇已经说过,继承 QThread 类是创建线程的一种方法,另一种就是继承 QObject 类。继承 QObject 类更加灵活。它通过 QObject::moveToThread()方法,将一个 QObeject 的类转移到一个线程里执行,可以通过下图理解。 通过…

注解和反射02(Java)

反射机制 首先需了解静态语言和动态语言。 动态语言是一类在运行时可以改变其结构的语言:例如新的函数、对象、甚至代码可以被引进,已有的函数可以被删除或是其他结构上的变化。通俗点说就是在运行时代码可以根据某些条件改变自身结构。主要动态语言&a…

一文解析Arm64 栈回溯

AArch64栈的结构 Arm64有4种栈,分别是空增栈(Empty Ascendant Stack,EA)、空减栈(Empty Descendant Stack,ED)、满增栈(Full Ascendant Stack,FA)、满减栈(Full Descendant Stack,FD)。常用的是满减栈,Linux内核也使用满减栈。 下图是一个满减栈的示意…

AppSpider Pro 7.4.053 for Windows - Web 应用程序安全测试

AppSpider Pro 7.4.053 for Windows - Web 应用程序安全测试 Rapid7 Dynamic Application Security Testing (DAST) 请访问原文链接:https://sysin.org/blog/appspider/,查看最新版。原创作品,转载请保留出处。 作者主页:sysin…

分板机视觉定位切割软硬件方案

【检测目的】 定位切割 【拍摄效果图一】 【拍摄效果图二】 【拍摄效果图三】 【方案评估】 以目前样品进行实验来看,图像效果明显,可以找到中线位置。 视野:44mm*33mm 视觉精度:44mm/2448pixel0.018mm/pixel。 【硬件配置】…

抖音seo源码.视频剪辑功能开发(一)

一、短视频抖音seo账号矩阵系统 批量剪辑功能的开发一般有以下几种方式 1. 前端实现:通过前端技术,利用vue jquery layui JavaScript,等语言,实现一个可视化的编辑器,用户可以批量上传视频文件,设置剪…

uniapp-日历控件

第一步:打开uniapp的插件市场 网址:日历组件可选择周与月标记打卡支持左右切换 - DCloud 插件市场 第二步:导入相应的项目,会有相应的提示(路径) 第三步:引入对应的位置-例如我引入的位置 imp…

fastadmin视图渲染

基类app\common\controller\Backend会默认渲染以下几个对象到视图中 //渲染站点配置 $this->assign(site, $site); //渲染配置信息 $this->assign(config, $config); //渲染权限对象 $this->assign(auth, $this->auth); //渲染管理员对象 $this->assign(admin,…

ArcGISPro加载在线底图和影像

经常用ArcGIS都知道,在工作中配合在线地图有点多爽。无论是制图还是数据校核都非常方便。之前已经讲过如何在ArcGIS地图里利用simplegis插件加载多种在线地图,那换成pro咋办嘞 今天我们就来说说如何在ArcGIS Pro里加载在线地图 ArcGISPro本身就自带了两种影像,均是源自谷歌…

《Redis 核心技术与实战》课程学习笔记(八)

String 类型为什么不好用了? String 类型可以保存二进制字节流,只要把数据转成二进制字节数组,就可以保存了。String 类型并不是适用于所有场合的,它有一个明显的短板,就是它保存数据时所消耗的内存空间较多。 为什么…

不平衡电网条件下基于变频器DG操作的多目标优化研究(Matlab代码Simulink实现)

💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…

贪心算法、贪心搜索/采样(greedy search/sampling)、集束搜索(beam search)、随机采样(random sample)

首先需要了解贪心算法: 贪心算法,又名贪婪法,是寻找最优解问题的常用方法,这种方法模式一般将求解过程分成若干个步骤,但每个步骤都应用贪心原则,选取当前状态下最好/最优的选择(局部最有利的选…

Tenable Nessus 10.5.3 (Unix, Linux, Windows) - #1 漏洞评估解决方案

Tenable Nessus 10.5.3 (Unix, Linux, Windows) - #1 漏洞评估解决方案 发布 Nessus 试用版自动化安装程序,支持 macOS Ventura、RHEL 9 和 Ubuntu 22.04 请访问原文链接:https://sysin.org/blog/nessus-10/,查看最新版。原创作品&#xff…

开源堡垒机Guacamole二次开发记录之二

这篇主要记录录屏和SFTP的实现。 录屏及视频播放 对于录屏及录屏的播放,因为我们的项目中需要把guacd和java后端分开两台服务器部署,而guacamole的录屏是通过guacd程序录制的。我的要求是在Java后端直接把录好的视频文件通过http前端播放,因…

手机外壳缺陷视觉检测软硬件方案

单独使用一种光源效果图 同轴光会出现亮度不够的情况;回形面光因为光源中间的圆孔会使图像有阴影,造成图像效果不均衡,所以不采用单独光源打光 使用同轴回形面光源效果图 回形光源照亮产品要寻找的边缘,同轴光源起到补光的作用&a…

裁剪opencv库到2Mb

摘要:本文描述了如何对opencv进行裁剪已达到最小化,不限于使用模块编译,去除第三方库依赖,改变编译选项,限制导出符号等。   关键字:opencv、导出符号 opencv库大小优化的文章网络上很少,大部…

【C++ 学习 ⑩】- 详解 string 类(下):string 类的模拟实现和写时拷贝

目录 一、string 类的模拟实现 1.1 - string.h 1.2 - test.cpp 二、string 类的写时拷贝 2.1 - 示例 2.2 - 原理 一、string 类的模拟实现 1.1 - string.h #pragma once#include <assert.h> #include <string.h> #include <iostream>namespace yzz {…

mac版android studio设置字体避坑总结

1.整体主题字体设置: setting->Appearance & Behavior->Appearance->Theme: 设置主题 Use custom font:右边的数字是设置除了编辑代码去之外的字体大小 ,推荐使用AppleSystemUIFont 注意这个字体有个bug,就是如果用在终端横向会有空格: 2.设置终端字体: setting-…

PyTorch深度学习实战(5)——计算机视觉

PyTorch深度学习实战&#xff08;5&#xff09;——计算机视觉 0. 前言1. 图像表示2. 将图像转换为结构化数组2.1 灰度图像表示2.2 彩色图像表示 3 利用神经网络进行图像分析的优势小结系列链接 0. 前言 计算机视觉是指通过计算机系统对图像和视频进行处理和分析&#xff0c;利…