Created
August 3, 2012 10:37
-
-
Save daichan4649/3246599 to your computer and use it in GitHub Desktop.
NFC sample(read IDm)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="test.nfc" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-permission android:name="android.permission.NFC" /> | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="15" /> | |
<uses-feature | |
android:name="android.hardware.nfc" | |
android:required="true" /> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:theme="@android:style/Theme.Holo.NoActionBar" | |
android:name=".MainActivity" | |
android:label="@string/title_activity_main" | |
android:launchMode="singleInstance" > | |
<intent-filter> | |
<action android:name="android.nfc.action.TAG_DISCOVERED" /> | |
<category android:name="android.intent.category.DEFAULT" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.nfc; | |
import android.app.Activity; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.nfc.NfcAdapter; | |
import android.nfc.tech.IsoDep; | |
import android.nfc.tech.MifareClassic; | |
import android.nfc.tech.MifareUltralight; | |
import android.nfc.tech.NdefFormatable; | |
import android.nfc.tech.NfcA; | |
import android.nfc.tech.NfcB; | |
import android.nfc.tech.NfcF; | |
import android.nfc.tech.NfcV; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private NfcAdapter adapter; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
adapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); | |
analyzeIntent(getIntent()); | |
} | |
@Override | |
protected void onNewIntent(Intent intent) { | |
analyzeIntent(intent); | |
} | |
private void analyzeIntent(Intent intent) { | |
// IDm取得 | |
String text = getIdm(intent); | |
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
registerNfcFilter(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
unregisterNfcFilter(); | |
} | |
private void registerNfcFilter() { | |
Intent intent = new Intent(this, getClass()); | |
PendingIntent pendingIntent = PendingIntent.getActivity( | |
getApplicationContext(), 0, intent, 0); | |
adapter.enableForegroundDispatch(this, pendingIntent, filters, techLists); | |
} | |
private void unregisterNfcFilter() { | |
adapter.disableForegroundDispatch(this); | |
} | |
private static final IntentFilter[] filters = new IntentFilter[] { | |
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED), | |
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED), | |
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) | |
}; | |
private static final String[][] techLists = new String[][] {{ | |
NfcA.class.getName(), | |
NfcB.class.getName(), | |
IsoDep.class.getName(), | |
MifareClassic.class.getName(), | |
MifareUltralight.class.getName(), | |
NdefFormatable.class.getName(), | |
NfcV.class.getName(), | |
NfcF.class.getName() | |
}}; | |
private static String getIdm(Intent intent) { | |
StringBuilder idm = new StringBuilder(); | |
byte[] rawId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); | |
for (int i = 0; i < rawId.length; i++) { | |
idm.append(String.format("%02x", rawId[i] & 0xff)); | |
} | |
return idm.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment