繁体   English   中英

使用NFC监听信用卡信息

[英]Listening for credit card info with NFC

关于如何创建示例应用程序来监听信用卡信息,是否有任何有效的示例?

我从Android示例中尝试了这段代码,但是当我扫描卡时没有任何反应( NFC正在工作,我听到哔声)

 public class MainActivity extends Activity {

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private TextView mText;
private int mCount = 0;

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    setContentView(R.layout.activity_main);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {
            ndef,
    };

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

@Override
public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

    String action = getIntent().getAction(); //allways MAIN, even after scanning a card
    Parcelable[] msg1 = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); //allways null
    Parcelable[] msg2 = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_TAG); //allways null
}

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);

}
@Override
public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

}

我的Manifest文件可能有问题:

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.NFC" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="pl.aprilapps.nfcreader.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

看一下这篇博客文章 我概述了您需要听以对非接触式EMV卡的存在做出反应的NFC标签的类型。 该代码使用Triangle.io的API一旦检测到卡就提取卡信息,但是您可以根据需要相当容易地更改该部分。

暂无
暂无

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

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