简体   繁体   English

Android NFC:将卡复制到设备中

[英]Android NFC: copy card into device

I created an app that reads the ID of an NFC card. 我创建了一个读取NFC卡ID的应用。 How I want to emulate that card with my Android device, so that an NFC reader can read it as if it was that previously read NFC card. 我想如何用我的Android设备模拟该卡,以便NFC读取器可以像以前读取的NFC卡一样读取它。 So eventually, I want to replace the NFC card with the Android device. 所以最终,我想用Android设备替换NFC卡。

This is my code for reading the ID from the card: 这是我从卡中读取ID的代码:

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

    TextView txt;
    NfcAdapter nfcAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.textView);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Toast.makeText(this,"Hello, I'm NFCTESTER", Toast.LENGTH_LONG).show();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        txt.setText(tag.getId().toString());
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        IntentFilter[] intentFilter = new IntentFilter[]{};

        nfcAdapter.enableForegroundDispatch(this,pendingIntent,intentFilter,null);
    }

    @Override
    protected void onPause() {
        super.onPause();

        nfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The card (school card) is MIFARE Classic and contains an 8 digit hex code (we got this information from IT Manager of school): 该卡(学校卡)是MIFARE Classic,包含一个8位数的十六进制代码(我们从学校的IT经理那里获得了此信息):

  • Tag ID (hex): d3 72 f5 24 标签ID(十六进制):d3 72 f5 24
  • Tag ID (dec): 3547526436 标记ID(十进制):3547526436
  • Tag ID (reserved): 620065491 标记ID(保留):620065491
  • Tag ID (string): 24:f5:72:d3 标记ID(字符串):24:f5:72:d3

If you can emulate such a card with your Android phone depends on what information is contained in the card/used by the NFC reader: 如果您可以用Android手机模拟这样的卡,则取决于卡中包含的信息/ NFC阅读器使用的信息:

  • The reader uses only the anti-collision identifier (UID). 读取器仅使用防冲突标识符(UID)。 You can read that ID with Tag.getId() and that's what your app currently does. 您可以使用Tag.getId()读取该ID,这就是您的应用当前正在执行的操作。 The Android NFC/HCE API does not allow you to set an arbitrary anti-collision identifer for the phone. Android NFC / HCE API不允许您为手机设置任意的防冲突标识符。 Thus, you cannot emulate the ID with your phone . 因此, 您无法使用手机模拟ID However, the NFC hardware in some Android devices would allow you to modify the ID with a customized ROM (or a modification of a configuration file on a rooted device). 但是,某些Android设备中的NFC硬件将允许您使用自定义的ROM修改ID(或在根设备上修改配置文件)。 See Editing Functionality of Host Card Emulation in Android and Host-based Card Emulation with Fixed Card ID . 请参阅在Android中编辑主机卡模拟功能使用固定卡ID的基于主机的卡模拟

  • The reader reads and/or writes to sectors of the MIFARE Classic card. 读取器读取和/或写入MIFARE Classic卡的扇区。 Android HCE does not support emulation of MIFARE Classic. Android HCE不支持MIFARE Classic的仿真。 Thus, you cannot emulate such a card with your phone . 因此, 您无法用手机模拟这样的卡

  • The reader reads an NDEF message from the card and supports just any NFC tag containing a properly formatted NDEF message. 读取器从卡中读取NDEF消息,并且仅支持任何包含格式正确的NDEF消息的NFC标签。 (Note that this is very unlikely!) You could read the NDEF message with Android and use Android HCE to emulate an NFC Forum type 4 tag containing that NDEF message. (请注意,这极不可能!)您可以使用Android阅读NDEF消息,并使用Android HCE模拟包含该NDEF消息的NFC论坛4类标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM