Last active
July 17, 2019 05:54
-
-
Save meco300/4561607 to your computer and use it in GitHub Desktop.
NFCタグをタッチしたときにIntentが起こるようにします。反応するタグの種類を設定できます。
This file contains 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 com.test.nfc_test; | |
import java.io.UnsupportedEncodingException; | |
import android.net.Uri; | |
import android.nfc.*; | |
import android.os.Bundle; | |
import android.os.Parcelable; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.app.PendingIntent; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.text.Editable; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
public static final String LOG_TAG = MainActivity.class.getSimpleName(); | |
//▼▼▼▼ここから | |
private NfcAdapter mNfcAdapter; | |
//▲▲▲▲▲ここまで | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
@Override | |
protected void onResume(){ | |
super.onResume(); | |
//▼▼▼▼ここから | |
mNfcAdapter = NfcAdapter.getDefaultAdapter(this); | |
//▼NFCの機能判定 | |
//NFC機能なし機種 | |
if(mNfcAdapter == null){ | |
Toast.makeText(getApplicationContext(), "no Nfc feature", Toast.LENGTH_SHORT).show(); | |
finish(); | |
return; | |
} | |
//NFC通信OFFモード | |
if(!mNfcAdapter.isEnabled()){ | |
Toast.makeText(getApplicationContext(), "off Nfc feature", Toast.LENGTH_SHORT).show(); | |
finish(); | |
return; | |
} | |
//▲NFCの機能判定 | |
//NFCを見つけたときに反応させる | |
//PendingIntent→タイミング(イベント発生)を指定してIntentを発生させる | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0); | |
//タイミングは、タグ発見時とする。 | |
IntentFilter[] intentFilter = new IntentFilter[]{ | |
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) | |
}; | |
//反応するタグの種類を指定。 | |
String[][] techList = new String[][]{ | |
{ | |
android.nfc.tech.NfcA.class.getName(), | |
android.nfc.tech.NfcB.class.getName(), | |
android.nfc.tech.IsoDep.class.getName(), | |
android.nfc.tech.MifareClassic.class.getName(), | |
android.nfc.tech.MifareUltralight.class.getName(), | |
android.nfc.tech.NdefFormatable.class.getName(), | |
android.nfc.tech.NfcV.class.getName(), | |
android.nfc.tech.NfcF.class.getName(), | |
} | |
}; | |
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, techList); | |
//▲▲▲▲▲ここまで | |
} | |
@Override | |
public void onPause(){ | |
super.onPause(); | |
//▼▼▼▼ここから | |
//アプリが表示されてない時は、NFCに反応しなくてもいいようにする | |
mNfcAdapter.disableForegroundDispatch(this); | |
//▲▲▲▲▲ここまで | |
} | |
//NFCをタッチした後の処理 | |
@Override | |
protected void onNewIntent(Intent intent){ | |
super.onNewIntent(intent); | |
//▼▼▼▼ここから | |
String action = intent.getAction(); | |
if(TextUtils.isEmpty(action)){ | |
return; | |
} | |
if(!action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)){ | |
return; | |
} | |
//成功!と表示してみる | |
Toast.makeText(getApplicationContext(), "成功!", Toast.LENGTH_SHORT).show(); | |
//▲▲▲▲▲ここまで | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment