写MifareClassic卡用我们自己的Andr​​oid应用程序上 [英] Write on a MifareClassic card with our own android application

查看:264
本文介绍了写MifareClassic卡用我们自己的Andr​​oid应用程序上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Android应用程序,这将写入Mifare卡上设有一个小型的文本。
我想申请,但code doest't工作方案。有人知道我有什么对Android清单写?
这里是我的code,如果有人可以帮助我,这将是很亲切。非常感谢你。

公共静态最后的标签CREATOR = NULL;

 公共无效ecrirenfc(标签标签){    MifareClassic MFC = MifareClassic.get(标签);    尝试{
        mfc.connect();
        布尔AUTH = FALSE;        AUTH = mfc.authenticateSectorWithKeyA(1,MifareClassic.KEY_DEFAULT);        如果(AUTH){
            字符串文本=你好,世界!
            字节[]值= text.getBytes();
            字节[] = toWrite新的字节[MifareClassic.BLOCK_SIZE]            的for(int i = 0; I< MifareClassic.BLOCK_SIZE;我++){
                如果(ⅰ&下; value.length)
                    toWrite [I] =值[I]
                其他
                    toWrite [I] = 0;
            }            mfc.writeBlock(1,toWrite);
        }    }赶上(IOException异常五){
    }
}公共无效的onClick(查看视图){
    如果(view.getId()== R.id.button1){
        ecrirenfc(CREATOR);
    }
}


解决方案

您需要使用

  sectorToBlock(INT sectorIndex)

的方法来确定该扇区的第一个块。然后使用

  getBlockCountInSector(INT sectorIndex)

要确定你能有多少块(-1)写入到该部门。

有关您的上述code,你在试图写0扇区,当你已经验证部门1。

I am currently trying to develop an android application which will write a smal text on a Mifare card. I tried to program an application but the code doest't work. Someone knows what do I have to write on the Android Manifest? Here is my code, if somebody could help me it would be very kind. Thank you very much

public static final Tag CREATOR = null;

public void ecrirenfc(Tag tag) {

    MifareClassic mfc = MifareClassic.get(tag);

    try {
        mfc.connect();
        boolean auth = false;

        auth = mfc.authenticateSectorWithKeyA(1, MifareClassic.KEY_DEFAULT);

        if (auth) {
            String text = "Hello, World!";
            byte[] value = text.getBytes();
            byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];

            for (int i = 0; i < MifareClassic.BLOCK_SIZE; i++) {
                if (i < value.length)
                    toWrite[i] = value[i];
                else
                    toWrite[i] = 0;
            }

            mfc.writeBlock(1, toWrite);
        }

    } catch (IOException e) {
    }
}

public void onClick(View view) {
    if (view.getId() == R.id.button1) {
        ecrirenfc(CREATOR);
    }
}

解决方案

You need to use the

sectorToBlock(int sectorIndex)

method to determine the first block of the sector. Then use

getBlockCountInSector(int sectorIndex)

to determine how many blocks (-1) you can write to in that sector.

For your above code, you are trying to write to sector 0 when you have authenticated sector 1.

这篇关于写MifareClassic卡用我们自己的Andr​​oid应用程序上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆