I am new to NFC. I tried to connect to NFC and share the text data from a device to another device.
I install my application on both devices and in one device I open my application and start tapping the device to the another device in order to transmit data over Beam.
On the other device, my application opens due to the Beam interaction. However, the activity is started with the default MAIN intent action and not with TAG_DISCOVERED (or similar NFC intent). Every time it does the same thing.
Also, it's not calling the onNewIntent()
method. I tried to call onNewIntent
from onCreate
, but the intent action is still MAIN in that case. I expected to receive an NFC intent for the Beam interaction. So can you please tell me where did I go wrong?
In this code I am not sharing the data. I just need the tag first.
Manifest:
<activity android:name="com.framentos.hellonfc.MainActivity"
android:clearTaskOnLaunch="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</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.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
Java code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
// startHandler();
_handler = new Handler();
Button btnWrite = (Button) findViewById(R.id.button);
message = (TextView) findViewById(R.id.nfcwriteTag);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
adapter = NfcAdapter.getDefaultAdapter(this);
if (adapter == null) {
message.setText("NFC is not supported on this device.");
}
if (adapter.isEnabled()) {
message.setText("NFC is Enabled on this device.");
} else {
message.setText("Please enable NFC to communicate.");
}
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(
NfcAdapter.ACTION_NDEF_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
IntentFilter tagTech = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
tagTech.addCategory(Intent.CATEGORY_DEFAULT);
IntentFilter tagDetect = new IntentFilter(
NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetect.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected, tagTech ,tagDetect};
// handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(getPackageName(), "on New Intent is called...!");
handleIntent(getIntent());
}
public void onPause() {
super.onPause();
WriteModeOff();
}
@Override
public void onResume() {
super.onResume();
WriteModeOn();
}
private void WriteModeOn() {
writeMode = true;
adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters,
null);
}
private void WriteModeOff() {
writeMode = false;
adapter.disableForegroundDispatch(this);
}