使用支持NFC的Android智能手机作为NFC标签,并使用nfc阅读器接收数据

时间:2017-04-12 10:23:49

标签: android nfc

我是NFC的新手我想将支持NFC的Android手机用作NFC标签,并使用NFC阅读器读取其数据。如果您可以建议我已经构建的用于发送数据的NFC应用程序或者我应该使用哪种NFC阅读器硬件,那将是非常好的。

2 个答案:

答案 0 :(得分:2)

这是我的NFC发送和接收代码。

MainActivity发送文件

public class MainActivity extends AppCompatActivity {

NfcAdapter nfcAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mIntentFilters;
private String[][] mNFCTechLists;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);

    mTextView = (TextView)findViewById(R.id.recive_text);
    PackageManager pm = this.getPackageManager();
    // Check whether NFC is available on device
    if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
        // NFC is not available on the device.
        Toast.makeText(this, "The device does not has NFC hardware.",
                Toast.LENGTH_SHORT).show();
    }
    // Check whether device is running Android 4.1 or higher
    else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        // Android Beam feature is not supported.
        Toast.makeText(this, "Android Beam is not supported.",
                Toast.LENGTH_SHORT).show();
    }
    else {
        // NFC and Android Beam file transfer is supported.
        Toast.makeText(this, "Android Beam is supported on your device.",
                Toast.LENGTH_SHORT).show();
    }


    // create an intent with tag data and deliver to this activity
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // set an intent filter for all MIME data
    IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndefIntent.addDataType("*/*");
        mIntentFilters = new IntentFilter[] { ndefIntent };
    } catch (Exception e) {
        Log.e("TagDispatch", e.toString());
    }

    mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}

public void sendFile(View view) {


    // Check whether NFC is enabled on device
    if(!nfcAdapter.isEnabled()){
        // NFC is disabled, show the settings UI
        // to enable NFC
        Toast.makeText(this, "Please enable NFC.",
                Toast.LENGTH_SHORT).show();
        startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
    }
    // Check whether Android Beam feature is enabled on device
    else if(!nfcAdapter.isNdefPushEnabled()) {
        // Android Beam is disabled, show the settings UI
        // to enable Android Beam
        Toast.makeText(this, "Please enable Android Beam.",
                Toast.LENGTH_SHORT).show();
        startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
    }
    else {


        Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
        chooseFile.setType("*/*");
        chooseFile = Intent.createChooser(chooseFile, "Choose a file");
        startActivityForResult(chooseFile, 1);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data!=null) {
        Uri uri = data.getData();
        nfcAdapter.setBeamPushUris(new Uri[]{uri}, this);
    } else {
        Toast.makeText(this,"Please choose a file",Toast.LENGTH_SHORT).show();
    }
}


public void nextScreen(View view){
    startActivity(new Intent(MainActivity.this,SecondActivity.class));
}

public void reciveScreen(View view){
    startActivity(new Intent(MainActivity.this,RecieveScreen.class));
}


@Override
public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    String s = action + "\n\n" + tag.toString();

    // parse through all NDEF messages and their records and pick text type only
    Parcelable[] data = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

    if (data != null) {
        try {
            for (int i = 0; i < data.length; i++) {
                NdefRecord[] recs = ((NdefMessage)data[i]).getRecords();
                for (int j = 0; j < recs.length; j++) {
                    if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN &&
                            Arrays.equals(recs[j].getType(), NdefRecord.RTD_TEXT)) {

                        byte[] payload = recs[j].getPayload();
                        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
                        int langCodeLen = payload[0] & 0077;

                        s += ("\n\nNdefMessage[" + i + "], NdefRecord[" + j + "]:\n\"" +
                                new String(payload, langCodeLen + 1,
                                        payload.length - langCodeLen - 1, textEncoding) +
                                "\"");
                    }
                }
            }
        } catch (Exception e) {
            Log.e("TagDispatch", e.toString());
        }

    }

    mTextView.setText(s);
}

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

    if (nfcAdapter != null)
        nfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists);
}

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

    if (nfcAdapter != null)
        nfcAdapter.disableForegroundDispatch(this);
}

}

这是我的接收屏幕,可能有一些额外的代码但你可以删除它

public class RecieveScreen extends AppCompatActivity {

private TextView mTextView;
private NfcAdapter mNfcAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mIntentFilters;
private String[][] mNFCTechLists;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recieve_screen);
    mTextView = (TextView)findViewById(R.id.recieve_tv);

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    if (mNfcAdapter != null) {
        mTextView.setText("Read an NFC tag");
    } else {
        mTextView.setText("This phone is not NFC enabled.");
    }

    // create an intent with tag data and deliver to this activity
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // set an intent filter for all MIME data
    IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndefIntent.addDataType("*/*");
        mIntentFilters = new IntentFilter[] { ndefIntent };
    } catch (Exception e) {
        Log.e("TagDispatch", e.toString());
    }

    mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}


@Override
public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    String s = action + "\n\n" + tag.toString();

    // parse through all NDEF messages and their records and pick text type only
    Parcelable[] data = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

    if (data != null) {
        try {
            for (int i = 0; i < data.length; i++) {
                NdefRecord[] recs = ((NdefMessage)data[i]).getRecords();
                for (int j = 0; j < recs.length; j++) {
                    if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN &&
                            Arrays.equals(recs[j].getType(), NdefRecord.RTD_TEXT)) {

                        byte[] payload = recs[j].getPayload();
                        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
                        int langCodeLen = payload[0] & 0077;

                        s += ("\n\nNdefMessage[" + i + "], NdefRecord[" + j + "]:\n\"" +
                                new String(payload, langCodeLen + 1,
                                        payload.length - langCodeLen - 1, textEncoding) +
                                "\"");
                    }
                }
            }
        } catch (Exception e) {
            Log.e("TagDispatch", e.toString());
        }

    }

    mTextView.setText(s);
}

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

    if (mNfcAdapter != null)
        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists);
}

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

    if (mNfcAdapter != null)
        mNfcAdapter.disableForegroundDispatch(this);
}
}

如果您遇到任何错误,请告诉我。

答案 1 :(得分:0)

是的。您可以使用基于Android NFC主机的卡仿真(HCE)。

https://developer.android.com/guide/topics/connectivity/nfc/hce

您可以使用自己的HostApduService。 但是NFC读取器应在第一时间发送APDU命令以选择您的应用程序ID(AID)。 APDU也在ISO / IEC 7816-4规范中定义。

  

如前所述,Android使用AID来确定哪个HCE   读者想要交谈的服务。通常,第一个APDU是NFC   阅读器向您的设备发送的是“ SELECT AID” APDU;该APDU包含   读者想要与之交谈的AID。 Android提取该AID   从APDU,将其解析为HCE服务,然后转发该APDU   到已解决的服务。

我正在使用ACR 122 NFC读卡器,可以将Android设备用作NFC卡。

请参考此git repo。

https://github.com/darryldecode/card-emulation

谢谢。