Android Beam(P2P接続)で動作するMIMEタイプ "application/com.sticknotes.android"用のインテントフィルタが既にアプリに含まれている場合、同じタグを含むタグでも動作しますMIMEタイプ。 Android Beamとタグ検出の両方で受信/読み取りデバイスにACTION_NDEF_DISCOVERED
インテントが生成されます。
このようなNDEFメッセージをMIFARE Classic 1Kタグに書き込むには、それを行う簡単なアプリケーションを作成できます。このアプリのマニフェストファイルプット:
<activity>
...
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
...
</activity>
し、プロジェクトのres/xml
フォルダに以下の内容のファイルnfc_tech_filter.xml
置く:アプリのActivity
プットで
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
:
onCreate(Bundle savedInstanceState) {
// put code here to set up your app
...
// create NDEF message
String mime = "application/com.sticknotes.android";
byte[] payload = ... ; // put your payload here
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mime.toBytes(), null, payload);
NdefMessage ndef = new NdefMessage(new NdefRecord[] {ndef});
// write NDEF message
Intent intent = getIntent();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefFormatable nf = NdefFormatable.get(tag);
if (nf != null) {
// tag not yet formatted with NDEF
try {
nf.connect();
nf.format(ndef);
nf.close();
} catch (IOException e) {
// tag communication error occurred
}
} else {
Ndef n = Ndef.get(tag);
if (n != null && n.isWritable()) {
// can write NDEF
try {
n.connect();
n.writeNdefMessage(ndef);
n.close();
} catch (IOException e) {
// tag communication error occurred
}
}
}
}
}
このフォーマットをフォーマットされていない(空の)MIFAREクラシックタグにNDEFメッセージを書き込むか、NDEFですでにフォーマットされているタグを上書きします。 MIFARE Classic以外のタグタイプを書きたい場合は、それに応じてnfc_tech_filter.xml
を調整してください。
Mifareカードで反応しますか?あなたはMifareカードで何を持っていますか?カスタムタイプのNDEFメッセージですか? –