2013-10-30 61 views
1

是否有任何有關如何創建示例應用程序偵聽信用卡存在的工作示例?用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> 

+0

這裏有一個很好的答案:http://stackoverflow.com/questions/9648224/reading-visa-paywave-credit-card-details-via-nfc-on-android – artemiygrn

+1

不是所有的答案鏈接到sdks,並且我想創建自己的 –

+0

如果您引用的是符合EMV的信用卡,則可能希望使您的應用對IsoDep技術敏感,而不是NfcF(EMV信用卡肯定不使用)。您的信用卡通常也不會包含NDEF消息,因此請跳過這些意向過濾器。 –

回答

1

看看this blog post。我概括了您需要聆聽的NFC標籤的類型,以對非接觸式EMV卡的存在做出反應。代碼使用Triangle.io的API在檢測到卡片後提取卡片信息,但如果您認爲合適,則可以相當容易地更改該部件。