Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Android上的NFC向M1卡发送7位命令?_Android_Nfc_Rfid_Mifare - Fatal编程技术网

如何通过Android上的NFC向M1卡发送7位命令?

如何通过Android上的NFC向M1卡发送7位命令?,android,nfc,rfid,mifare,Android,Nfc,Rfid,Mifare,我在Android上使用NFC时遇到问题。我想向M1卡发送一些数据。我可以使用transceive()方法发送命令。但是transceive()方法有一个byte[]参数,一个字节是8位。我想将0b1000000(短帧)发送到我的M1卡。如何将其发送到M1卡 /** * Send raw NfcA data to a tag and receive the response. * * <p>This is equivalent to connecting to this ta

我在Android上使用NFC时遇到问题。我想向M1卡发送一些数据。我可以使用
transceive()
方法发送命令。但是
transceive()
方法有一个
byte[]
参数,一个字节是8位。我想将
0b1000000
(短帧)发送到我的M1卡。如何将其发送到M1卡

 /**
 * Send raw NfcA data to a tag and receive the response.
 *
 * <p>This is equivalent to connecting to this tag via {@link NfcA}
 * and calling {@link NfcA#transceive}. Note that all MIFARE Classic
 * tags are based on {@link NfcA} technology.
 *
 * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes
 * that can be sent with {@link #transceive}.
 *
 * <p>This is an I/O operation and will block until complete. It must
 * not be called from the main application thread. A blocked call will be canceled with
 * {@link IOException} if {@link #close} is called from another thread.
 *
 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
 *
 * @see NfcA#transceive
 */
public byte[] transceive(byte[] data) throws IOException {
    return transceive(data, true);
}
/**
*将原始NfcA数据发送到标记并接收响应。
*
*这相当于通过{@link NfcA}连接到此标记
*并调用{@link NfcA#transceive}。请注意,所有mif都是经典的
*标记基于{@link NfcA}技术。
*
*使用{@link#getMaxTransceiveLength}检索最大字节数
*可以通过{@link#transceive}发送。
*
*这是一个I/O操作,将一直阻塞直到完成。它必须
*不能从主应用程序线程调用。被阻止的呼叫将被取消
*{@link IOException}如果从另一个线程调用{@link#close}。
*
*

需要{@link android.Manifest.permission}权限。 * *@see NfcA#收发器 */ 公共字节[]收发器(字节[]数据)引发IOException{ 返回收发器(数据,真); }

我的代码是:

 /**
 * Write a block of 16 byte data to tag.
 * @param sectorIndex The sector to where the data should be written
 * @param blockIndex The block to where the data should be written
 * @param data 16 byte of data.
 * @param key The MIFARE Classic key for the given sector.
 * @param useAsKeyB If true, key will be treated as key B
 * for authentication.
 * @return The return codes are:<br />
 * <ul>
 * <li>0 - Everything went fine.</li>
 * <li>1 - Sector index is out of range.</li>
 * <li>2 - Block index is out of range.</li>
 * <li>3 - Data are not 16 bytes.</li>
 * <li>4 - Authentication went wrong.</li>
 * <li>-1 - Error while writing to tag.</li>
 * </ul>
 * @see #authenticate(int, byte[], boolean)
 */
public int writeBlock(int sectorIndex, int blockIndex, byte[] data,
        byte[] key, boolean useAsKeyB) {
    if (getSectorCount()-1 < sectorIndex) {
        return 1;
    }
    if (mMFC.getBlockCountInSector(sectorIndex)-1 < blockIndex) {
        return 2;
    }
    if (data.length != 16) {
        return 3;
    }
    int block = mMFC.sectorToBlock(sectorIndex) + blockIndex;

    // write chinese card
    if (block == 0) {
        // Write block.
        try {
            mMFC.transceive(new byte[]{(byte)0x50, (byte)0x00, (byte)0x57, (byte)0xCD});
            mMFC.transceive(new byte[]{(byte)0x40}); // TODO: here need send 0b1000000(7 bit) , not 0b01000000(8 bit)
            mMFC.transceive(new byte[]{(byte)0x43});
            mMFC.writeBlock(block, data);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error while writing block to tag.", e);
            return -1;
        }
        return 0;
    }

    if (!authenticate(sectorIndex, key, useAsKeyB)) {
        return 4;
    }
    // Write block.
    try {
        mMFC.writeBlock(block, data);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error while writing block to tag.", e);
        return -1;
    }
    return 0;
}
/**
*将一块16字节的数据写入标记。
*@param sector索引数据应写入的扇区
*@param blockIndex应将数据写入的块
*@param data 16字节的数据。
*@param key是给定扇区的经典密钥。
*@param useAsKeyB如果为true,则密钥将被视为密钥B
*用于身份验证。
*@return返回代码为:
*
    *
  • 0-一切顺利
  • *
  • 1-行业指数超出范围
  • *
  • 2-块索引超出范围
  • *
  • 3-数据不是16字节
  • *
  • 4-身份验证出错
  • *
  • -1-写入标记时出错
  • *
*@see#验证(int,字节[],布尔值) */ public int writeBlock(int sectorIndex,int blockIndex,byte[]数据, 字节[]键,布尔值useAsKeyB){ if(getSectorCount()-1
在Android上,您试图做的事情根本不可能实现。Android NFC API不提供任何直接发送短帧(ISO/IEC 14443-3 A类中定义的7位帧)的方法

事实上,
NfcA
transceive()
方法不仅发送作为其参数传递的字节数组。相反,它还将导致CRC校验和自动附加到帧。因此,您只能使用
transceive()
方法交换正常帧(如ISO/IEC 14443-3 a型中的定义)


由于您使用的是MIFARE Classic(或者更确切地说是一些可更改UID的克隆),您将在支持MIFARE Classic的Android设备(即使用NXP制造的NFC芯片组的设备)上遇到进一步的限制:如果检测到标签为MIFARE Classic,NFC控制器将切换到一种特殊模式,在该模式下,它解释MIFARE经典命令的高级版本,并自动将其转换为低级(可能加密)版本。这是必要的,因为MIFARE Classic没有完全遵循ISO/IEC 14443-3 A型的帧规则。不幸的是,这通常也会阻止您将任何原始帧直接发送到这些标签。

在Android上,您尝试的操作根本不可能。Android NFC API不提供任何直接发送短帧(ISO/IEC 14443-3 A类中定义的7位帧)的方法

事实上,
NfcA
transceive()
方法不仅发送作为其参数传递的字节数组。相反,它还将导致CRC校验和自动附加到帧。因此,您只能使用
transceive()
方法交换正常帧(如ISO/IEC 14443-3 a型中的定义)


由于您使用的是MIFARE Classic(或者更确切地说是一些可更改UID的克隆),您将在支持MIFARE Classic的Android设备(即使用NXP制造的NFC芯片组的设备)上遇到进一步的限制:如果检测到标签为MIFARE Classic,NFC控制器将切换到一种特殊模式,在该模式下,它解释MIFARE经典命令的高级版本,并自动将其转换为低级(可能加密)版本。这是必要的,因为MIFARE Classic没有完全遵循ISO/IEC 14443-3 A型的框架规则。不幸的是,这通常也会阻止您将任何原始帧直接发送到这些标记。

您可以将数据传递到收发器的代码段放在这里吗?您可以将数据传递到收发器的代码段放在这里吗?您真了不起