I am trying to write NDEF message to NFC tag. I have the code to write NDEF message in onNewIntent(). But the control is not going to onNewIntent(). After onResume(), it is hanging. pls find below the code.
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private NfcAdapter mNfcAdapter;
static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob".
getBytes(Charset.forName("US_ASCII"));
private NdefMessage mNdefMessage;
String mLocalBluetoothAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show();
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
return;
}
}
public void onResume() {
super.onResume();
Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
public void onNewIntent(Intent intent) {
Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
ndef.writeNdefMessage(createHandoverRequestMessage());
} catch (Exception e) {
Log.e("TagWriting", e.toString());
} finally {
try {
ndef.close();
} catch (Exception e) {
}
}
}
}
}
}
public void onPause() {
super.onPause();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
I appreciate any help to get this sorted out.
Check your manifest file to see
<activity
android:name=".MainActivity"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="nosensor" >
android:alwaysRetainTaskState="true"
(I use the following method and it works) If it is fine then try catching the pending intent at onCreate().
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private NfcAdapter mNfcAdapter;
static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob".
getBytes(Charset.forName("US_ASCII"));
private NdefMessage mNdefMessage;
private PendingIntent pendingIntent;
String mLocalBluetoothAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show();
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
return;
} else {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
onTagFound(getIntent());
}
}
public void onResume() {
super.onResume();
Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter != null && pendingIntent != null) {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
}
public void onNewIntent(Intent intent) {
Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show();
onTagFound(intent);
}
public void onTagFound(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
ndef.connect();
ndef.writeNdefMessage(createHandoverRequestMessage());
} catch (Exception e) {
Log.e("TagWriting", e.toString());
} finally {
try {
ndef.close();
} catch (Exception e) {
}
}
}
}
}
}
public void onPause() {
super.onPause();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.