示例#1
0
        public NFCMessage MifareClassic_AuthenticateSectorWithKeyB(MifareClassic mfc, int Sector, List <byte[]> Keys)
        {
            if (mfc == null)
            {
                return(NFCMessage.NFC_NULL_MIFARECLASSIC);
            }
            if (!mfc.IsConnected)
            {
                mfc.Connect();
            }
            bool auth = false;
            int  i    = 0;

            if (Keys == null || Keys.Count == 0)
            {
                auth = mfc.AuthenticateSectorWithKeyB(Sector, MifareClassic.KeyNfcForum.ToArray());
                if (!auth)
                {
                    auth = mfc.AuthenticateSectorWithKeyB(Sector, MifareClassic.KeyDefault.ToArray());
                }
            }
            else
            {
                while (!auth || i != Keys.Count)
                {
                    auth = mfc.AuthenticateSectorWithKeyB(Sector, Keys[i]);
                    i++;
                }
            }
            return(auth ? NFCMessage.NFC_AUTH_OK : NFCMessage.NFC_AUTH_FAIELD);
        }
示例#2
0
        public void ConnectToCard()
        {
            try
            {
                acr1281UC1.connect(readerList[0].ToString());


                //Initialize Mifare classic class
                mifareClassic = new MifareClassic(acr1281UC1.pcscConnection);

                currentChipType = acr1281UC1.getChipType();


                if (currentChipType != CHIP_TYPE.MIFARE_1K && currentChipType != CHIP_TYPE.MIFARE_4K)
                {
                    // MessageBox.Show("Card is not supported.\r\nPlease present Mifare Classic card");
                    return;
                }
            }
            catch (PcscException pcscException)
            {
                //MessageBox.Show(pcscException.Message, "PCSC Exception");
            }
            catch (Exception generalException)
            {
                // MessageBox.Show(generalException.Message, "Exception");
            }
        }
示例#3
0
 // Faz a leitura do ID do cartão
 protected void LerCartaoNfc()
 {
     mifareClassic = MifareClassic.Get(tag);
     if (mifareClassic == null)
     {
         isoDep = IsoDep.Get(tag);
     }
     Contador       += 1;
     txtLeitura.Text = "Leitura: " + Contador.ToString() + "\nId do Cartão: " + idCartao();
 }
示例#4
0
        public void StopByteBlockOperations()
        {
            innitialCardId = null;

            currentMifareClassic?.Close();
            currentMifareClassic?.Dispose();
            currentMifareClassic = null;

            currentTag    = null;
            currentIntent = null;
        }
示例#5
0
        private bool TryAndWriteToTag(Tag tag, byte[] data)
        {
            var  mfc      = MifareClassic.Get(tag);
            bool auth     = false;
            int  secCount = 1;// mfc.SectorCount;
            int  bCount   = 3;
            int  bIndex   = 0;

            byte[] toWrite = new byte[MifareClassic.BlockSize];

            try
            {
                mfc.Connect();
                String UID = byte2hex(mfc.Tag.GetId());
                for (int j = 1; j <= secCount; j++)
                {
                    auth = mfc.AuthenticateSectorWithKeyB(j, KEY_DEFAULT);
                    if (auth)
                    {
                        //bCount = mfc.GetBlockCountInSector(j);
                        bIndex = mfc.SectorToBlock(j);
                        for (int i = 0; i < bCount; i++)
                        {
                            for (int k = 0; k < 16; k++)
                            {
                                if (k + (i * 16) < data.Length)
                                {
                                    toWrite[k] = data[k + (i * 16)];
                                }
                                else
                                {
                                    toWrite[k] = 0;
                                }
                            }
                            mfc.WriteBlock(bIndex, toWrite);
                            bIndex++;
                        }
                        DisplayMessage("Card write success");
                        mfc.Close();
                        return(true);
                    }
                    else
                    {
                        Log.Error(Tag, "Sector write authentication failed");
                        return(false);
                    }
                }
            }
            catch (IOException e)
            {
                Log.Debug(Tag, e.LocalizedMessage);
            }
            return(false);
        }
        private bool isMIFAREClassicTag(Tag tag)
        {
            bool          returnVar     = false;
            MifareClassic mifareClassic = MifareClassic.Get(tag); //Get MIFARE CLASSIC tag

            if (mifareClassic != null)
            {
                returnVar = true;
            }
            return(returnVar);
        }
示例#7
0
        public List <byte[]> MifareClassic_ReadSector(MifareClassic mfc, int Sector)
        {
            int blockCount = mfc.GetBlockCountInSector(Sector);
            var lst        = new List <byte[]>(blockCount);
            int readIndex  = mfc.SectorToBlock(1);

            for (int i = 0; i < blockCount; i++)
            {
                lst[i] = mfc.ReadBlock(readIndex);
                readIndex++;
            }
            return(lst);
        }
示例#8
0
        private bool TryAndReadFromTag(Tag tag)
        {
            MifareClassic mfc = MifareClassic.Get(tag);

            byte[] data;
            bool   auth = false;

            try
            {
                mfc.Connect();
                String UID      = byte2hex(mfc.Tag.GetId());
                String cardData = null;
                int    secCount = 1;
                int    bCount   = 3; //Just read 2 block for now
                int    bIndex   = 0;
                for (int j = 1; j <= secCount; j++)
                {
                    auth = mfc.AuthenticateSectorWithKeyB(j, KEY_DEFAULT);
                    if (auth)
                    {
                        //bCount = mfc.GetBlockCountInSector(j);
                        bIndex = mfc.SectorToBlock(j);
                        for (int i = 0; i < bCount; i++)
                        {
                            data      = mfc.ReadBlock(bIndex);
                            cardData += Encoding.ASCII.GetString(data);
                            bIndex++;
                        }
                        DisplayMessage(cardData);
                        mfc.Close();
                        return(true);
                    }
                    else
                    {
                        Log.Error(Tag, "Sector read authentication failed");
                        mfc.Close();
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(Tag, e.Message);
            }
            return(false);
        }
示例#9
0
 public static void MifareTest()
 {
     using (var context = new NfcContext())
         using (var device = context.OpenDevice()) // Try to open the NFC reader
         {
             MifareClassic mfc = new MifareClassic(device);
             mfc.InitialDevice();
             mfc.SelectCard();
             mfc.Authentication(0, KeyType.KeyA, 0xFFFFFFFFFFFFu);
             var block0 = mfc.ReadBlock(0);
             Program.PrintHex(block0, 16);
             mfc.Authentication(1, KeyType.KeyA, 0xFFFFFFFFFFFFu);
             mfc.WriteBlock(4, new byte[16] {
                 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
             });
             var block4 = mfc.ReadBlock(4);
             Program.PrintHex(block4, 16);
             Console.ReadLine();
         }
 }
示例#10
0
        private bool LoadCurrentMifareClassic()
        {
            if (currentMifareClassic != null)
            {
                ConnectCurrentMifareClassic();
                return(true);
            }

            if (currentTag == null)
            {
                return(false);
            }

            currentMifareClassic = MifareClassic.Get(currentTag);
            if (currentMifareClassic == null)
            {
                return(false);
            }

            ConnectCurrentMifareClassic();
            return(true);
        }
示例#11
0
 public (TagInfo?, NFCMessage) ReadInfo(Tag tag)
 {
     if (tag == null)
     {
         return(null, NFCMessage.NFC_NULL_TAG);
     }
     return(new TagInfo
     {
         Uid = tag.GetId(),
         Tag = tag,
         IsoDep = IsoDep.Get(tag),
         MifareClassic = MifareClassic.Get(tag),
         MifareUltralight = MifareUltralight.Get(tag),
         NfcV = NfcV.Get(tag),
         Ndef = Ndef.Get(tag),
         NdefFormatable = NdefFormatable.Get(tag),
         NfcA = NfcA.Get(tag),
         NfcB = NfcB.Get(tag),
         NfcF = NfcF.Get(tag),
         NfcBarcode = NfcBarcode.Get(tag),
         TechList = tag.GetTechList()
     }, NFCMessage.NFC_NO_ERROR);
 }
示例#12
0
        public void MifareClassic_WriteBlock(MifareClassic mfc, int Block, byte[] Data)
        {
            var tmp = new byte[16];

            if (Data.Length >= 16)
            {
                for (int i = 0; i < 16; i++)
                {
                    tmp[i] = Data[i];
                }
            }
            else
            {
                for (int i = 0; i < Data.Length; i++)
                {
                    tmp[i] = Data[i];
                }
                for (int i = Data.Length; i < 16; i++)
                {
                    tmp[i] = (byte)' ';
                }
            }
            mfc.WriteBlock(Block, tmp);
        }
示例#13
0
        private void ReadIcData(Intent intent)
        {
            m_intent    = intent;
            ic.icserial = ic.FindIC(m_intent);

            Car = new CarInfo();    //初始化一个CarInfo类

            //取出封装在intent中的TAG
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            #region 清空文本
            carNumber      = FindViewById <EditText>(Resource.Id.carNumber);
            carNumber.Text = "";
            #endregion

            MifareClassic mfc = MifareClassic.Get(m_tag);
            if (mfc != null)
            {
                #region 获取到tag
                try
                {
                    mfc.Connect();
                    var type = mfc.GetType();
                    if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                    {
                        #region 循环读取5个扇区数据
                        for (int i = 0; i < ic.sectors.Length; i++)
                        {
                            for (int k = 0; k < 3; k++)
                            {
                                int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k);
                                if (sec < mfc.SectorCount)
                                {
                                    if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb)))
                                    {
                                        int c = mfc.SectorCount;
                                        c = mfc.BlockCount;

                                        byte[] data = new byte[16];
                                        data = mfc.ReadBlock(ic.sectors[i] * 4 + k);
                                        if (data != null)
                                        {
                                            ic.IcDatas[i, k] = ic.ToHexString(data);
                                            ic.isReadIcOK    = true;
                                        }
                                        else
                                        {
                                            CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true);
                                            ic.isReadIcOK = false;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        CommonFunction.ShowMessage("密码验证失败!", this, true);
                                        ic.isReadIcOK = false;
                                        return;
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    CommonFunction.ShowMessage(ex.Message, this, true);
                }
                finally
                {
                    mfc.Close();
                }

                if (ic.isReadIcOK)
                {
                    try
                    {
                        //解析扇区数据
                        Car = ic.GetCarInfo(ic.IcDatas);

                        if (Car != null)
                        {
                            CommonFunction.ShowMessage("", this, false);

                            #region 读卡正确,显示在窗体文本内,按钮复活

                            carNumber.Text = Car.CarPlate;
                            // carNumber.Text = "GT030108";//暂用

                            //根据车号查询 相关收发货路线信息
                            goodsInfo();
                            #endregion
                        }
                        else
                        {
                            #region 读卡异常,按钮不可用
                            btSender.Enabled = false;

                            btGet.Enabled = false;
                            CommonFunction.ShowMessage("卡内信息错误!", this, true);
                            #endregion
                        }
                    }
                    catch (System.ArgumentOutOfRangeException ax)
                    {
                        CommonFunction.ShowMessage("读卡信息错误!", this, true);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.ShowMessage(ex.Message, this, true);
                    }
                }

                #endregion
            }
            else
            {
                #region 判断卡类型
                btGet.Enabled    = false;
                btSender.Enabled = false;

                btGet.Checked    = false;
                btSender.Checked = false;
                CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                ic.isReadIcOK = false;
                return;

                #endregion
            }
        }
示例#14
0
        /// <summary>
        /// 卸货写卡函数
        /// </summary>
        private void UnloadFunction(Intent intent)
        {
            //业务类型置"1"
            //计量类型置"1"
            //计重方式置"1"
            //退货标识置"1"
            //写入卸货时间

            bool result = true;

            if (ic.icserial != ic.FindIC(intent))
            {
                result = false;
                CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true);
                return;
            }
            //取出封装在intent中的TAG
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            if (CommonFunction.mode == "NFC")
            {
                MifareClassic mfc = MifareClassic.Get(m_tag);
                if (mfc != null)
                {
                    try
                    {
                        mfc.Connect();
                        var type = mfc.GetType();
                        if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                        {
                            try
                            {
                                #region  点击按钮的写卡方法
                                try
                                {
                                    string pkidd = Convert.ToString(pkid);
                                    isOK = 1;
                                    string writedata = "";
                                    if (pkidd.Length == 1)
                                    {
                                        writedata = Convert.ToString(isOK) + pkidd + "000000000000000000000000000000";
                                    }
                                    else if (pkidd.Length == 2)
                                    {
                                        writedata = Convert.ToString(isOK) + pkidd + "00000000000000000000000000000";
                                    }
                                    else if (pkidd.Length == 3)
                                    {
                                        writedata = Convert.ToString(isOK) + pkidd + "0000000000000000000000000000";
                                    }


                                    bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                    if (auth)
                                    {
                                        mfc.WriteBlock(10, ic.ToDigitsBytes(writedata));
                                    }
                                }
                                catch (Java.IO.IOException ex)
                                {
                                    result = false;
                                    CommonFunction.ShowMessage(ex.Message, this, true);
                                }
                                #endregion

                                #region 点击按钮的写卡方法
                                //0业务类型 1计量类型 2计重方式 3 退货标识
                                //    if (btGet.Checked) //收货
                                //    {
                                //    #region 1-写入收发货标识 位于2区10块 [1,2] 第一位
                                //    try
                                //    {
                                //        //string tmp = ic.IcDatas[0, 1].Remove(0, 1);
                                //        //string writedata = tmp.Insert(0, "1");

                                //        // string writedata = ic.IcDatas[1, 2].Substring(0, 1) ;
                                //        string pkidd = Convert.ToString(pkid);
                                //        string writedata = "";
                                //        if (pkidd.Length == 1)
                                //        {
                                //            writedata = Convert.ToString(isOK) + pkidd + "000000000000000000000000000000";
                                //        }
                                //        else if (pkidd.Length == 2)
                                //        {
                                //            writedata = Convert.ToString(isOK) + pkidd + "00000000000000000000000000000";
                                //        }
                                //        else if (pkidd.Length == 3)
                                //        {
                                //            writedata = Convert.ToString(isOK) + pkidd + "0000000000000000000000000000";
                                //        }


                                //        bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                //        if (auth)
                                //        {
                                //            mfc.WriteBlock(10, ic.ToDigitsBytes(writedata));
                                //        }
                                //    }
                                //    catch (Java.IO.IOException ex)
                                //    {
                                //        result = false;
                                //        CommonFunction.ShowMessage(ex.Message, this, true);
                                //    }
                                //    #endregion
                                //}
                                #endregion
                            }
                            finally
                            {
                                mfc.Close();
                            }
                        }
                    }
                    catch (Java.IO.IOException ex)
                    {
                        CommonFunction.ShowMessage("请放卡!", this, true);
                        return;
                    }
                }
                else
                {
                    #region 判断卡类型

                    CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                    return;

                    #endregion
                }
            }
            else if (CommonFunction.mode == "SerialPort")
            {
                //
            }

            WriteDataOK(result);
        }
示例#15
0
        /// <summary>
        /// 卸货写卡函数
        /// </summary>
        private void UnloadFunction(Intent intent)
        {
            //业务类型置"1"
            //计量类型置"1"
            //计重方式置"1"
            //退货标识置"1"
            //写入卸货时间

            bool result = true;

            if (ic.icserial != ic.FindIC(intent))
            {
                result = false;
                CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true);
                return;
            }
            //取出封装在intent中的TAG
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            if (CommonFunction.mode == "NFC")
            {
                MifareClassic mfc = MifareClassic.Get(m_tag);
                if (mfc != null)
                {
                    try
                    {
                        mfc.Connect();
                        var type = mfc.GetType();
                        if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                        {
                            try
                            {
                                //0业务类型 1计量类型 2计重方式 3 退货标识
                                if (bunload.Checked) //确认卸货
                                {
                                    #region 1-更改计量类型为1,位于1扇区5块第2位
                                    try
                                    {
                                        //string tmp = ic.IcDatas[0, 1].Remove(0, 1);
                                        //string writedata = tmp.Insert(0, "1");

                                        string writedata = ic.IcDatas[0, 1].Substring(0, 1) + "1" + ic.IcDatas[0, 1].Substring(2);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位
                                    try
                                    {
                                        string xhdt = string.Format("{0:yyMMddhhmmss}", DateTime.Now);

                                        EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight);
                                        string   kd           = buckleweight.Text.Trim().PadLeft(6, '0');

                                        string writedata = xhdt + kd + ic.IcDatas[0, 2].Substring(18);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 3-写入卸货人名字,位于2扇区9块后16位
                                    try
                                    {
                                        string tmp       = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位
                                        string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 4-取样类型位于3扇区12块前两位,如果取样类型为13,写入取样时间和取样人员,3扇区12块,2位取样代码,12位取样时间,8位取样人员名字
                                    try
                                    {
                                        string quyangbaozhi = ic.IcDatas[2, 0].ToString().Substring(0, 2);
                                        if (quyangbaozhi == "13")
                                        {
                                            //判断取样代码。为"13"时,写入IC卡取样时间和取样人员(即货场操作人员);不为"13"时,不对IC卡操作。
                                            string qydt      = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now);
                                            string writedata = quyangbaozhi + qydt + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);

                                            bool auth = mfc.AuthenticateSectorWithKeyB(3, ic.ToDigitsBytes(ic.mb));
                                            if (auth)
                                            {
                                                mfc.WriteBlock(12, ic.ToDigitsBytes(writedata));//12位取样时间和16位卸货人员名字写入IC卡3扇区12块
                                            }
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                }
                                else if (bhalfreturn.Checked) //半车退货
                                {
                                    #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"1",位于1扇区5块第1位;计重方式置"1",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位;
                                    try
                                    {
                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            string writedata = "1111" + ic.IcDatas[0, 1].Substring(4);
                                            mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位
                                    try
                                    {
                                        string   xhdt         = string.Format("{0:yyMMddhhmmss}", DateTime.Now);
                                        EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight);
                                        string   kd           = buckleweight.Text.Trim().PadLeft(6, '0');
                                        string   writedata    = xhdt + kd + ic.IcDatas[0, 2].Substring(18);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 3-写入卸货人名字,位于2扇区9块后16位
                                    try
                                    {
                                        string tmp       = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位
                                        string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                }
                                else if (ballreturn.Checked) //全车退货
                                {
                                    #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"1",位于1扇区5块第1位;计重方式置"0",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位;
                                    try
                                    {
                                        string writedata = "1101" + ic.IcDatas[0, 1].Substring(4);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位
                                    try
                                    {
                                        string   xhdt         = string.Format("{0:yyMMddhhmmss}", DateTime.Now);
                                        EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight);
                                        string   kd           = buckleweight.Text.Trim().PadLeft(6, '0');
                                        string   writedata    = xhdt + kd + ic.IcDatas[0, 2].Substring(18);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 3-写入卸货人名字,位于2扇区9块后16位
                                    try
                                    {
                                        string tmp       = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位
                                        string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                }
                                else if (bunloadandload.Checked) //卸后退装
                                {
                                    #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"0",位于1扇区5块第1位;计重方式置"0",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位;
                                    try
                                    {
                                        string writedata = "0101" + ic.IcDatas[0, 1].Substring(4);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位
                                    try
                                    {
                                        string   xhdt         = string.Format("{0:yyMMddhhmmss}", DateTime.Now);
                                        EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight);
                                        string   kd           = buckleweight.Text.Trim().PadLeft(6, '0');
                                        string   writedata    = xhdt + kd + ic.IcDatas[0, 2].Substring(18);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                    #region 3-写入卸货人名字,位于2扇区9块后16位
                                    try
                                    {
                                        string tmp       = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位
                                        string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                }
                            }
                            finally
                            {
                                mfc.Close();
                            }
                        }
                    }
                    catch (Java.IO.IOException ex)
                    {
                        CommonFunction.ShowMessage("请放卡!", this, true);
                        return;
                    }
                }
                else
                {
                    #region 判断卡类型

                    CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                    return;

                    #endregion
                }
            }
            else if (CommonFunction.mode == "SerialPort")
            {
                //
            }

            WriteDataOK(result);
        }
示例#16
0
        private void ReadIcData(Intent intent)
        {
            m_intent    = intent;
            ic.icserial = ic.FindIC(m_intent);

            Car = new CarInfo();    //初始化一个CarInfo类

            //取出封装在intent中的TAG
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            #region 清空文本
            EditText carinfo = FindViewById <EditText>(Resource.Id.tuCarInfo);
            carinfo.Text = "";

            EditText bussnise = FindViewById <EditText>(Resource.Id.tuBussnise);
            bussnise.Text = "";

            EditText measuretype = FindViewById <EditText>(Resource.Id.tuMeasureType);
            measuretype.Text = "";

            EditText measuremode = FindViewById <EditText>(Resource.Id.tuMeasureMode);
            measuremode.Text = "";

            EditText measureid = FindViewById <EditText>(Resource.Id.tuMeasureID);
            measureid.Text = "";

            EditText goodweight = FindViewById <EditText>(Resource.Id.tuGoodWeight);
            goodweight.Text = "";

            EditText measuredt = FindViewById <EditText>(Resource.Id.tuMeasureDT);
            measuredt.Text = "";

            EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight);
            buckleweight.Text = "0";

            #endregion

            MifareClassic mfc = MifareClassic.Get(m_tag);
            if (mfc != null)
            {
                #region 获取到tag
                try
                {
                    mfc.Connect();
                    var type = mfc.GetType();
                    if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                    {
                        #region 循环读取5个扇区数据
                        for (int i = 0; i < ic.sectors.Length; i++)
                        {
                            for (int k = 0; k < 3; k++)
                            {
                                int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k);
                                if (sec < mfc.SectorCount)
                                {
                                    if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb)))
                                    {
                                        int c = mfc.SectorCount;
                                        c = mfc.BlockCount;

                                        byte[] data = new byte[16];
                                        data = mfc.ReadBlock(ic.sectors[i] * 4 + k);
                                        if (data != null)
                                        {
                                            ic.IcDatas[i, k] = ic.ToHexString(data);
                                            ic.isReadIcOK    = true;
                                        }
                                        else
                                        {
                                            CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true);
                                            ic.isReadIcOK = false;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        CommonFunction.ShowMessage("密码验证失败!", this, true);
                                        ic.isReadIcOK = false;
                                        return;
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    CommonFunction.ShowMessage(ex.Message, this, true);
                }
                finally
                {
                    mfc.Close();
                }

                if (ic.isReadIcOK)
                {
                    try
                    {
                        //解析扇区数据
                        Car = ic.GetCarInfo(ic.IcDatas);

                        if (Car != null)
                        {
                            CommonFunction.ShowMessage("", this, false);

                            #region 读卡正确,显示在窗体文本内,按钮复活

                            carinfo.Text = Car.CarPlate;

                            bussnise.Text = Car.BusinessType;

                            measuretype.Text = Car.MeasurementType;

                            measuremode.Text = Car.MeasureMode;

                            measureid.Text = Car.CardID;

                            goodweight.Text = Car.Gross.ToString();

                            measuredt.Text = Car.GrossDateTime;

                            Button unload = FindViewById <Button>(Resource.Id.buUnLoad);
                            unload.Enabled = true;
                            unload.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32"));
                            unload.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff"));

                            Button halfreturn = FindViewById <Button>(Resource.Id.buHalfReturn);
                            halfreturn.Enabled = true;
                            halfreturn.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32"));
                            halfreturn.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff"));

                            Button allreturn = FindViewById <Button>(Resource.Id.buAllReturn);
                            allreturn.Enabled = true;
                            allreturn.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32"));
                            allreturn.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff"));

                            Button unloadandload = FindViewById <Button>(Resource.Id.buUnloadAndLoad);
                            unloadandload.Enabled = true;
                            unloadandload.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32"));
                            unloadandload.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff"));

                            #endregion
                        }
                        else
                        {
                            #region 读卡异常,按钮不可用
                            Button unload = FindViewById <Button>(Resource.Id.buUnLoad);
                            unload.Enabled = false;

                            Button halfreturn = FindViewById <Button>(Resource.Id.buHalfReturn);
                            halfreturn.Enabled = false;

                            Button allreturn = FindViewById <Button>(Resource.Id.buAllReturn);
                            allreturn.Enabled = false;

                            Button unloadandload = FindViewById <Button>(Resource.Id.buUnloadAndLoad);
                            unloadandload.Enabled = false;

                            CommonFunction.ShowMessage("卡内信息错误!", this, true);
                            #endregion
                        }
                    }
                    catch (System.ArgumentOutOfRangeException ax)
                    {
                        CommonFunction.ShowMessage("读卡信息错误!", this, true);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.ShowMessage(ex.Message, this, true);
                    }
                }

                #endregion
            }
            else
            {
                #region 判断卡类型
                bunload.Enabled        = false;
                bhalfreturn.Enabled    = false;
                ballreturn.Enabled     = false;
                bunloadandload.Enabled = false;

                bunload.Checked        = false;
                bhalfreturn.Checked    = false;
                ballreturn.Checked     = false;
                bunloadandload.Checked = false;
                CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                ic.isReadIcOK = false;
                return;

                #endregion
            }
        }
示例#17
0
        public static string GetManufacturer(Tag tag)
        {
            string mifareInfo = "";

            string[] techtags = tag.GetTechList();
            foreach (string s in techtags)
            {
                if (s.ToLower().IndexOf("mifareultralight") != -1)
                {
                    return("MifareUltralight");
                }
                else if (s.ToLower().IndexOf("mifareclassic") != -1)
                {
                    mifareInfo = "Mifare Classic";
                    MifareClassic mifareClassicTag = MifareClassic.Get(tag); //Get MIFARE CLASSIC tag
                    if (mifareClassicTag != null)
                    {
                        if (mifareClassicTag.Type == MifareClassic.TypePlus)
                        {
                            mifareInfo = "Mifare  Plus";
                        }
                        else if (mifareClassicTag.Type == MifareClassic.TypePro)
                        {
                            mifareInfo = "Mifare  Pro";
                        }
                    }

                    return(mifareInfo);
                }
                else if (s.ToLower().IndexOf("nfca") != -1)
                {
                    NfcA nfc_A = NfcA.Get(tag);
                    if (nfc_A != null)
                    {
                        byte[] atqa = nfc_A.GetAtqa();
                        if (atqa.Length >= 2)
                        {
                            Int32.TryParse(nfc_A.Sak.ToString(), out int Sak);
                            switch (atqa[1])
                            {
                            case 0x00:
                                if (atqa[0] == 0x44 || atqa[0] == 0x42 || atqa[0] == 0x02 || atqa[0] == 0x04)
                                {
                                    if (Sak == 0x20)
                                    {
                                        mifareInfo = "Mifare Plus SL3";
                                    }
                                    else if (Sak == 0x10)
                                    {
                                        mifareInfo = "Mifare Plus SL2";
                                    }

                                    else if (Sak == 0x00)
                                    {
                                        mifareInfo = "Mifare EM";
                                    }
                                }
                                break;

                            case 0x03:
                                if (atqa[0] == 0x44 || atqa[0] == 0x4)
                                {
                                    mifareInfo = "Mifare Desfire";
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
                else if (IsFelicaTag(tag))
                {
                    mifareInfo = "Felica";
                }
            }

            return(mifareInfo);
        }
示例#18
0
        private void ReadIcData(Intent intent)
        {
            m_intent    = intent;
            ic.icserial = ic.FindIC(m_intent);

            Car = new CarInfo();    //初始化一个CarInfo类

            //取出封装在intent中的TAG
            tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            MifareClassic mfc = MifareClassic.Get(m_tag);

            if (mfc != null)
            {
                #region 获取到tag
                try
                {
                    mfc.Connect();
                    var type = mfc.GetType();
                    if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                    {
                        #region 循环读取5个扇区数据
                        for (int i = 0; i < ic.sectors.Length; i++)
                        {
                            for (int k = 0; k < 3; k++)
                            {
                                int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k);
                                if (sec < mfc.SectorCount)
                                {
                                    if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb)))
                                    {
                                        int c = mfc.SectorCount;
                                        c = mfc.BlockCount;

                                        byte[] data = new byte[16];
                                        data = mfc.ReadBlock(ic.sectors[i] * 4 + k);
                                        if (data != null)
                                        {
                                            ic.IcDatas[i, k] = ic.ToHexString(data);
                                            ic.isReadIcOK    = true;
                                        }
                                        else
                                        {
                                            CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true);
                                            ic.isReadIcOK = false;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        CommonFunction.ShowMessage("密码验证失败!", this, true);
                                        ic.isReadIcOK = false;
                                        return;
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    CommonFunction.ShowMessage(ex.Message, this, true);
                }
                finally
                {
                    mfc.Close();
                }
                if (ic.isReadIcOK)
                {
                    ToggleButton samp = FindViewById <ToggleButton>(Resource.Id.btSamp);
                    //Button samp = FindViewById<Button>(Resource.Id.btSamp);
                    samp.Enabled = true;
                    btSamp.SetBackgroundColor(Android.Graphics.Color.ParseColor("#DB3E3E"));
                }
                else
                {
                    #region 读卡异常,按钮不可用
                    Button samp = FindViewById <Button>(Resource.Id.btSamp);
                    samp.Enabled = false;
                    CommonFunction.ShowMessage("卡内信息错误!", this, true);
                    #endregion
                }
                #endregion
            }
            else
            {
                #region 判断卡类型
                btSamp.Enabled = false;

                btSamp.Checked = false;
                CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                ic.isReadIcOK = false;
                return;

                #endregion
            }
        }
 /**
  * Método construtor da classe.
  *
  * @param tag = contém as tag do cartão que foi lido.
  *
  * */
 public NfcLeituraGravacao(Tag tag)
 {
     this.mifareClassic = MifareClassic.Get(tag);
     this.GravaTempoInicia();
 }
示例#20
0
        /// <summary>
        /// 卸货写卡函数
        /// </summary>
        private void UnloadFunction(Intent intent)
        {
            //业务类型置"1"
            //计量类型置"1"
            //计重方式置"1"
            //退货标识置"1"
            //写入卸货时间

            bool result = true;

            if (ic.icserial != ic.FindIC(intent))
            {
                result = false;
                CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true);
                return;
            }
            //取出封装在intent中的TAG
            //var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            if (CommonFunction.mode == "NFC")
            {
                MifareClassic mfc = MifareClassic.Get(m_tag);
                if (mfc != null)
                {
                    try
                    {
                        mfc.Connect();
                        var type = mfc.GetType();
                        if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                        {
                            try
                            {
                                if (btSamp.Checked) //取样
                                {
                                    #region  写入取样编号 IcDatas[0,0] - 1扇区 4块
                                    try
                                    {
                                        //当前时间
                                        string xhdt = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now);

                                        //写入取样编号  YYYYMMDD+0001
                                        //sampCode = xhdt + serialNumber;
                                        // string writedata = xhdt + serialNumber + ic.IcDatas[0, 2].Substring(18);

                                        //写入取样卡标识
                                        string writedata = "12018041100100000000000000000000";
                                        //serialNumber = Convert.ToString( Convert.ToInt32(serialNumber)+1);

                                        bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb));
                                        if (auth)
                                        {
                                            mfc.WriteBlock(4, ic.ToDigitsBytes(writedata));
                                        }
                                    }
                                    catch (Java.IO.IOException ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                    }
                                    #endregion
                                }
                            }
                            finally
                            {
                                mfc.Close();
                            }
                        }
                    }
                    catch (Java.IO.IOException ex)
                    {
                        CommonFunction.ShowMessage("请放卡!", this, true);
                        return;
                    }
                }
                else
                {
                    #region 判断卡类型

                    CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                    return;

                    #endregion
                }
            }
            else if (CommonFunction.mode == "SerialPort")
            {
                //
            }

            WriteDataOK(result);
        }
示例#21
0
        /// <summary>
        /// 取样写入函数
        /// </summary>
        private void QualityFunction(Intent intent)
        {
            bool result = true;

            if (ic.icserial != ic.FindIC(intent))
            {
                result = false;
                CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true);
                return;
            }

            //取出封装在intent中的TAG
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            if (tag == null)
            {
                return;
            }
            m_tag = tag;
            if (CommonFunction.mode == "NFC")
            {
                //写卡
                MifareClassic mfc = MifareClassic.Get(m_tag);
                if (mfc != null)
                {
                    try
                    {
                        mfc.Connect();
                        var type = mfc.GetType();
                        if (type.Equals(typeof(MifareClassic)))
                        {
                            try
                            {
                                if (afterquality.Checked)
                                {
                                    #region 1-先写入操作人员名字,位于2扇区9块后16位
                                    try
                                    {
                                        string tmp       = ic.IcDatas[1, 1].Substring(0, 16); //获取2扇区9块前16位
                                        string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);
                                        bool   auth      = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb));

                                        if (auth)
                                        {
                                            mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块
                                        }
                                        else
                                        {
                                            result = false;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                        return;
                                    }
                                    #endregion
                                    #region 2-取样类型位于3扇区12块前两位,如果取样类型为12,写入取样时间和取样人员 3扇区12块  2位取样代码,12位取样时间,16位取样人员名字
                                    try
                                    {
                                        //string quyangbiaozhi = ic.IcDatas[2, 0].ToString().Substring(0, 2);
                                        string quyangbiaozhi = ic.IcDatas[0, 1].ToString().Substring(4, 1);
                                        if (quyangbiaozhi == "2")
                                        {
                                            //判断取样代码。为"12"时,磅后取样,写入IC卡取样时间和取样人员(即货场操作人员);不为"12"时,不对IC卡操作,并且提示。
                                            string qydt      = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now);
                                            string writedata = quyangbiaozhi + qydt + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16);
                                            bool   auth      = mfc.AuthenticateSectorWithKeyB(3, ic.ToDigitsBytes(ic.mb));

                                            if (auth)
                                            {
                                                mfc.WriteBlock(12, ic.ToDigitsBytes(writedata));//12位取样时间和16位卸货人员名字写入IC卡3扇区12块
                                            }
                                            else
                                            {
                                                result = false;
                                            }
                                        }
                                        else
                                        {
                                            result = false;
                                            CommonFunction.ShowMessage("取样类型非'磅后取样'", this, true);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        result = false;
                                        CommonFunction.ShowMessage(ex.Message, this, true);
                                        return;
                                    }
                                    #endregion
                                }
                                else if (beforequality.Checked)
                                {
                                }
                            }
                            finally
                            {
                                mfc.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        result = false;
                        CommonFunction.ShowMessage(ex.Message, this, true);
                    }
                    finally
                    {
                        mfc.Close();
                    }
                }
                else
                {
                    #region 判断卡类型
                    result = false;
                    CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                    return;

                    #endregion
                }
            }
            else if (CommonFunction.mode == "SerialPort")
            {
                CommonFunction.ShowMessage("磅后取样", this, true);
            }
            if (result)
            {
                beforequality.Checked = false;
                beforequality.Enabled = false;

                afterquality.Checked = false;
                afterquality.Enabled = false;

                CommonFunction.ShowMessage("操作成功!", this, true);
            }
        }
示例#22
0
        private void ReadIcData(Intent intent)
        {
            m_intent    = intent;
            ic.icserial = ic.FindIC(m_intent);

            Car = new CarInfo();    //初始化一个CarInfo类

            //取出封装在intent中的TAG
            tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
            if (tag == null)
            {
                return;
            }
            m_tag = tag;

            //清空文本
            extra_bags.Text     = "";
            samp_bags.Text      = "";
            samp_factory.Text   = "";
            samp_goodsname.Text = "";
            samp_carNumber.Text = "";
            cmeasure_Id.Text    = "";
            MifareClassic mfc = MifareClassic.Get(m_tag);

            if (mfc != null)
            {
                #region 获取到tag
                try
                {
                    mfc.Connect();
                    var type = mfc.GetType();
                    if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic)))
                    {
                        #region 循环读取5个扇区数据
                        for (int i = 0; i < ic.sectors.Length; i++)
                        {
                            for (int k = 0; k < 3; k++)
                            {
                                int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k);
                                if (sec < mfc.SectorCount)
                                {
                                    if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb)))
                                    {
                                        int c = mfc.SectorCount;
                                        c = mfc.BlockCount;

                                        byte[] data = new byte[16];
                                        data = mfc.ReadBlock(ic.sectors[i] * 4 + k);
                                        if (data != null)
                                        {
                                            ic.IcDatas[i, k] = ic.ToHexString(data);
                                            ic.isReadIcOK    = true;
                                        }
                                        else
                                        {
                                            CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true);
                                            ic.isReadIcOK = false;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        CommonFunction.ShowMessage("密码验证失败!", this, true);
                                        ic.isReadIcOK = false;
                                        return;
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    CommonFunction.ShowMessage(ex.Message, this, true);
                }
                finally
                {
                    mfc.Close();
                }

                if (ic.isReadIcOK)
                {
                    try
                    {
                        //解析扇区数据
                        Car = ic.GetCarInfo(ic.IcDatas);

                        if (Car != null)
                        {
                            // CommonFunction.ShowMessage("", this, false);

                            //将摇号信息写入本地数据表
                            // isEqualFactory();

                            #region 读卡正确,显示在窗体文本内,按钮复活
                            //是否为同一张卡 重复卡不得重复刷
                            if (!isEqualFactory())
                            {
                                samp_carNumber.Text = Car.CarPlate;

                                // bussnise.Text = Car.BusinessType;

                                // measuremode.Text = Car.MeasureMode;

                                cmeasure_Id.Text = Car.MeasureID;

                                //根据车号和计量单号 从计量系统中查询 采购订单号 供应商编号 供应商名称 货物编号 货物名称等信息
                                bsif.selectCmInfo(Car.CarPlate, Car.MeasureID, out cMeasureID, out purchaseOrderID, out car_factoryCode, out car_factoryName, out car_goodsCode, out car_goodsName);
                                samp_factory.Text   = car_factoryName;
                                samp_goodsname.Text = car_goodsName;
                            }
                            #endregion
                        }
                        else
                        {
                            #region 读卡异常,按钮不可用
                            ToggleButton samp = FindViewById <ToggleButton>(Resource.Id.btExtract);
                            samp.Enabled = false;
                            CommonFunction.ShowMessage("卡内信息错误!", this, true);
                            #endregion
                        }
                    }
                    catch (System.ArgumentOutOfRangeException ax)
                    {
                        CommonFunction.ShowMessage("读卡信息错误!", this, true);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.ShowMessage(ex.Message, this, true);
                    }
                }

                #endregion
            }
            else
            {
                #region 判断卡类型
                btExtra.Enabled = false;

                btExtra.Checked = false;
                CommonFunction.ShowMessage("不支持该类型的卡", this, true);
                ic.isReadIcOK = false;
                return;

                #endregion
            }
        }
示例#23
0
 public byte[] MifareClassic_ReadBlock(MifareClassic mfc, int Block)
 {
     return(mfc.ReadBlock(Block));
 }