1

我正在尝试从 ACR1252 向 android 应用程序发送一条长 NDEF 消息。我正在使用 C# 开发应用程序。可以发送长度小于 256 字节的消息,并且 android 应用程序也完全接收消息,但对于较大的消息,我使用 PFB 字段中的 MI(多信息)位以片段形式发送消息。我收到每条消息的确认,但 android 应用程序进入 NFC 接收页面,但最后没有收到任何内容。

这是我用来发送消息的代码:

    sentIndex = 0;
do
{
    int tempLen = Min(474, sSNEPMessage_.Length - sentIndex);

    int index = 0;

    bool firstPacket = sentIndex == 0 ? true : false;
    bool lastPacket = (sSNEPMessage_.Length == sentIndex + tempLen) ? true : false;

    int iByteLength = 6 + 6 + tempLen / 2;

    uSendBuffer = new byte[iByteLength];
    uSendBuffer[index++] = 0x11;
    if (lastPacket == true)
        uSendBuffer[index++] = (byte)(0x00 | (uPNI_ & 0x03)); // PFB
    else
        uSendBuffer[index++] = (byte)(0x10 | (uPNI_ & 0x03)); // PFB

    uSendBuffer[index++] = (byte)(tempLen / 2 + 3 + 6);
    uSendBuffer[index++] = (byte)(((uDSAP_ << 2) & 0xFC) | 0x03); // I1
    uSendBuffer[index++] = (byte)(uSSAP_ & 0x3F);                 // I2
    uSendBuffer[index++] = (byte)(((uSendSeq_++ << 4) & 0xF0) | (uRecSeq_ & 0x0F)); // Seq
    uSendBuffer[index++] = 0x10;
    uSendBuffer[index++] = 0x02;
    uSendBuffer[index++] = 0x00;
    uSendBuffer[index++] = 0x00;
    uSendBuffer[index++] = 0x00;

    uSendBuffer[index++] = (byte)(tempLen / 2);

    for (int i = 0; i < (tempLen / 2); i++)
        uSendBuffer[index + i] = Convert.ToByte(sSNEPMessage_.Substring(sentIndex + i * 2, 2), 16);

    iSendCnt = 6 + 6 + tempLen / 2;

    iConnectComplete_ = 0x00;

    uApduResponse = peerToPeer_.depExchange(uSendBuffer, iSendCnt);

    sentIndex += tempLen;

    uPNI_++;

} while (sentIndex != sSNEPMessage_.Length);

和 depExchange 是:

public byte[] depExchange(byte[] uDep, int iSendDep)
    {
        Apdu apdu = new Apdu();
        apdu.lengthExpected = 20;
        apdu.data = new byte[5];

        apdu.data[0] = 0xE0;
        apdu.data[1] = 0x00;
        apdu.data[2] = 0x00;
        apdu.data[3] = 0x43;
        apdu.data[4] = (byte)(iSendDep);

        apdu.data = Helper.appendArrays(apdu.data, uDep);

        apduCommand = apdu;
        sendCardControl(ref apdu, operationControlCode);

        return apduCommand.response;
    }

sendCardControl 使用 SCardControl 发送 apdu。

我使用以下代码创建 NDEF 消息:

void encodeToNdefMessage(String msgToSend)
    {
        NdefMessage ndefMessage;
        NdefRecord tmpRecord;
        byte uStatusByte = 0;
        byte[] uBuffer;

        if (RadioButtonUtf16.Checked)
            uStatusByte = 0x80;

        uStatusByte |= (byte)msgToSend.Length;

        tmpRecord = new NdefRecord(new NdefRecordType(TypeNameFormat.NfcFormWellKnownType, "T"));
        tmpRecord.messageBegin = true;
        tmpRecord.messageEnd = true;

        tmpRecord.payLoad = new byte[] { uStatusByte };
        tmpRecord.payLoad = Helper.appendArrays(tmpRecord.payLoad, ASCIIEncoding.ASCII.GetBytes(ComboBoxTextLanguage.Text));
        tmpRecord.payLoad = Helper.appendArrays(tmpRecord.payLoad, ASCIIEncoding.ASCII.GetBytes(msgToSend));

        ndefMessage = new NdefMessage();
        ndefMessage.appendRecord(tmpRecord);

        uBuffer = ndefMessage.toByteArray();

        sSNEPMessage_ = Helper.byteAsString(uBuffer, false);
    }

public byte[] toByteArray()
    {
        byte[] buffer = new byte[0];
        byte[] tmpArray;
        int indx = 0;

        for (int i = 0; i < ndefRecords.Count; i++)
        {
            indx = buffer.Length;

            if (i == 0)
                ndefRecords[i].messageBegin = true;

            if ((i + 1) == ndefRecords.Count)
                ndefRecords[i].messageEnd = true;


            tmpArray = ndefRecords[i].encodeToNDEF();

            //Resize destination array to acoomodate new record
            Array.Resize(ref buffer, buffer.Length + tmpArray.Length);

            //Copy new  ndef record to byte array
            Array.Copy(tmpArray, 0, buffer, indx, tmpArray.Length);
        }

        return buffer;
    }

这也是日志: 在此处输入图像描述

4

0 回答 0