Read NDEF message from tag two times with one single touch (without moving phone)
I got everything successfully by reading the NDEF message from NFC tag. And after I read, I move the phone and it can read the tag again.
I'm using the onNewIntent and foregroundDispatch to handle the message.
The problem is:
I want to read the same NFC tag twice (for security reasons) without moving the phone (without touching the tag a second time). So for a single touch, I want to read twice.
I try to see the life cycle but it seems if you do not move the phone, it will not issue a new intent again.
private NfcAdapter nfc = null;
private boolean inReadMode = false;
private boolean isNFC_support = false;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ignore the unrelevent part of layout
isNFC_support = true;
nfc = NfcAdapter.getDefaultAdapter(this);
if(nfc == null) {
Toast.makeText(this, "Not support NFC device.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if(!nfc.isEnabled()) {
Toast.makeText(this, "Please go the setting and enable NFC first.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if (isNFC_support == true) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("NFC", "---- onNewIntent called ---- ");
if (this.inReadMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.i("NFC", "---- onNewIntent called ---- AND read nfc success!");
try {
readFromTag(intent);
} catch (Exception e) {
Log.e("NFC", "nfc cmac validate: ", e);
}
}
}
@Override
public void onResume() {
super.onResume();
Log.i("NFC", "---- onResume called ---- ");
nfc.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
public void onPause() {
Log.i("NFC", "---- onPause called ---- ");
if (nfc != null) {
nfc.disableForegroundDispatch(this);
}
if (isFinishing()) {
cleanupReadingFromTag();
}
super.onPause();
}
private void readFromTag(Intent intent) throws RuntimeException, NoSuchAlgorithmException, IOException {
Parcelable msgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// code I handle the message as I get. Not important for read twice I think?
}
So how can I read the tag again a second time?
android android-intent tags nfc ndef
add a comment |
I got everything successfully by reading the NDEF message from NFC tag. And after I read, I move the phone and it can read the tag again.
I'm using the onNewIntent and foregroundDispatch to handle the message.
The problem is:
I want to read the same NFC tag twice (for security reasons) without moving the phone (without touching the tag a second time). So for a single touch, I want to read twice.
I try to see the life cycle but it seems if you do not move the phone, it will not issue a new intent again.
private NfcAdapter nfc = null;
private boolean inReadMode = false;
private boolean isNFC_support = false;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ignore the unrelevent part of layout
isNFC_support = true;
nfc = NfcAdapter.getDefaultAdapter(this);
if(nfc == null) {
Toast.makeText(this, "Not support NFC device.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if(!nfc.isEnabled()) {
Toast.makeText(this, "Please go the setting and enable NFC first.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if (isNFC_support == true) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("NFC", "---- onNewIntent called ---- ");
if (this.inReadMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.i("NFC", "---- onNewIntent called ---- AND read nfc success!");
try {
readFromTag(intent);
} catch (Exception e) {
Log.e("NFC", "nfc cmac validate: ", e);
}
}
}
@Override
public void onResume() {
super.onResume();
Log.i("NFC", "---- onResume called ---- ");
nfc.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
public void onPause() {
Log.i("NFC", "---- onPause called ---- ");
if (nfc != null) {
nfc.disableForegroundDispatch(this);
}
if (isFinishing()) {
cleanupReadingFromTag();
}
super.onPause();
}
private void readFromTag(Intent intent) throws RuntimeException, NoSuchAlgorithmException, IOException {
Parcelable msgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// code I handle the message as I get. Not important for read twice I think?
}
So how can I read the tag again a second time?
android android-intent tags nfc ndef
add a comment |
I got everything successfully by reading the NDEF message from NFC tag. And after I read, I move the phone and it can read the tag again.
I'm using the onNewIntent and foregroundDispatch to handle the message.
The problem is:
I want to read the same NFC tag twice (for security reasons) without moving the phone (without touching the tag a second time). So for a single touch, I want to read twice.
I try to see the life cycle but it seems if you do not move the phone, it will not issue a new intent again.
private NfcAdapter nfc = null;
private boolean inReadMode = false;
private boolean isNFC_support = false;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ignore the unrelevent part of layout
isNFC_support = true;
nfc = NfcAdapter.getDefaultAdapter(this);
if(nfc == null) {
Toast.makeText(this, "Not support NFC device.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if(!nfc.isEnabled()) {
Toast.makeText(this, "Please go the setting and enable NFC first.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if (isNFC_support == true) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("NFC", "---- onNewIntent called ---- ");
if (this.inReadMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.i("NFC", "---- onNewIntent called ---- AND read nfc success!");
try {
readFromTag(intent);
} catch (Exception e) {
Log.e("NFC", "nfc cmac validate: ", e);
}
}
}
@Override
public void onResume() {
super.onResume();
Log.i("NFC", "---- onResume called ---- ");
nfc.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
public void onPause() {
Log.i("NFC", "---- onPause called ---- ");
if (nfc != null) {
nfc.disableForegroundDispatch(this);
}
if (isFinishing()) {
cleanupReadingFromTag();
}
super.onPause();
}
private void readFromTag(Intent intent) throws RuntimeException, NoSuchAlgorithmException, IOException {
Parcelable msgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// code I handle the message as I get. Not important for read twice I think?
}
So how can I read the tag again a second time?
android android-intent tags nfc ndef
I got everything successfully by reading the NDEF message from NFC tag. And after I read, I move the phone and it can read the tag again.
I'm using the onNewIntent and foregroundDispatch to handle the message.
The problem is:
I want to read the same NFC tag twice (for security reasons) without moving the phone (without touching the tag a second time). So for a single touch, I want to read twice.
I try to see the life cycle but it seems if you do not move the phone, it will not issue a new intent again.
private NfcAdapter nfc = null;
private boolean inReadMode = false;
private boolean isNFC_support = false;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ignore the unrelevent part of layout
isNFC_support = true;
nfc = NfcAdapter.getDefaultAdapter(this);
if(nfc == null) {
Toast.makeText(this, "Not support NFC device.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if(!nfc.isEnabled()) {
Toast.makeText(this, "Please go the setting and enable NFC first.", Toast.LENGTH_LONG).show();
isNFC_support = false;
}
if (isNFC_support == true) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("NFC", "---- onNewIntent called ---- ");
if (this.inReadMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.i("NFC", "---- onNewIntent called ---- AND read nfc success!");
try {
readFromTag(intent);
} catch (Exception e) {
Log.e("NFC", "nfc cmac validate: ", e);
}
}
}
@Override
public void onResume() {
super.onResume();
Log.i("NFC", "---- onResume called ---- ");
nfc.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
public void onPause() {
Log.i("NFC", "---- onPause called ---- ");
if (nfc != null) {
nfc.disableForegroundDispatch(this);
}
if (isFinishing()) {
cleanupReadingFromTag();
}
super.onPause();
}
private void readFromTag(Intent intent) throws RuntimeException, NoSuchAlgorithmException, IOException {
Parcelable msgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// code I handle the message as I get. Not important for read twice I think?
}
So how can I read the tag again a second time?
android android-intent tags nfc ndef
android android-intent tags nfc ndef
edited Nov 16 at 12:53
Michael Roland
29.4k851107
29.4k851107
asked Nov 15 at 22:40
o1xhack
375
375
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The NFC intents are only dispatched upon detection of NFC tags. If a tag contains an NDEF message, that message is automatically processed and shared with your app in an intent extra (NfcAdapter.EXTRA_NDEF_MESSAGES
).
If you want to read from the tag again at a later stage (and managed to keep the NFC tag constantly connected), you will need to communicate with the tag directly. You can do this through the tag handle object. This object (class Tag
) is also passed to your app upon tag detection (as an intent extra of the NFC intent):
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
while the tag is connected, you can use that object at any time to initiate communication with your tag. E.g. to freshly read the current NDEF message on the tag, you could use:
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
// do something with the NDEF message
} catch (IOException e) {
} finally {
try {
ndef.close();
} catch (Exception e) {}
}
}
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328885%2fread-ndef-message-from-tag-two-times-with-one-single-touch-without-moving-phone%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The NFC intents are only dispatched upon detection of NFC tags. If a tag contains an NDEF message, that message is automatically processed and shared with your app in an intent extra (NfcAdapter.EXTRA_NDEF_MESSAGES
).
If you want to read from the tag again at a later stage (and managed to keep the NFC tag constantly connected), you will need to communicate with the tag directly. You can do this through the tag handle object. This object (class Tag
) is also passed to your app upon tag detection (as an intent extra of the NFC intent):
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
while the tag is connected, you can use that object at any time to initiate communication with your tag. E.g. to freshly read the current NDEF message on the tag, you could use:
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
// do something with the NDEF message
} catch (IOException e) {
} finally {
try {
ndef.close();
} catch (Exception e) {}
}
}
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
add a comment |
The NFC intents are only dispatched upon detection of NFC tags. If a tag contains an NDEF message, that message is automatically processed and shared with your app in an intent extra (NfcAdapter.EXTRA_NDEF_MESSAGES
).
If you want to read from the tag again at a later stage (and managed to keep the NFC tag constantly connected), you will need to communicate with the tag directly. You can do this through the tag handle object. This object (class Tag
) is also passed to your app upon tag detection (as an intent extra of the NFC intent):
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
while the tag is connected, you can use that object at any time to initiate communication with your tag. E.g. to freshly read the current NDEF message on the tag, you could use:
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
// do something with the NDEF message
} catch (IOException e) {
} finally {
try {
ndef.close();
} catch (Exception e) {}
}
}
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
add a comment |
The NFC intents are only dispatched upon detection of NFC tags. If a tag contains an NDEF message, that message is automatically processed and shared with your app in an intent extra (NfcAdapter.EXTRA_NDEF_MESSAGES
).
If you want to read from the tag again at a later stage (and managed to keep the NFC tag constantly connected), you will need to communicate with the tag directly. You can do this through the tag handle object. This object (class Tag
) is also passed to your app upon tag detection (as an intent extra of the NFC intent):
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
while the tag is connected, you can use that object at any time to initiate communication with your tag. E.g. to freshly read the current NDEF message on the tag, you could use:
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
// do something with the NDEF message
} catch (IOException e) {
} finally {
try {
ndef.close();
} catch (Exception e) {}
}
}
The NFC intents are only dispatched upon detection of NFC tags. If a tag contains an NDEF message, that message is automatically processed and shared with your app in an intent extra (NfcAdapter.EXTRA_NDEF_MESSAGES
).
If you want to read from the tag again at a later stage (and managed to keep the NFC tag constantly connected), you will need to communicate with the tag directly. You can do this through the tag handle object. This object (class Tag
) is also passed to your app upon tag detection (as an intent extra of the NFC intent):
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
while the tag is connected, you can use that object at any time to initiate communication with your tag. E.g. to freshly read the current NDEF message on the tag, you could use:
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
// do something with the NDEF message
} catch (IOException e) {
} finally {
try {
ndef.close();
} catch (Exception e) {}
}
}
answered Nov 16 at 12:50
Michael Roland
29.4k851107
29.4k851107
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
add a comment |
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
thanks. you are day saver. I already tried out that yesterday. But my method got some wrong and not working. Slight different make a lot change!
– o1xhack
Nov 16 at 23:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328885%2fread-ndef-message-from-tag-two-times-with-one-single-touch-without-moving-phone%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown