i have a problem using NFC in 2 activities , the first one A is a Sender activity and the Second one B is a Receiver ..
My Problem is that the Second Activity is prompting to send beam so both application can send and receive ( conflict).
I Have tryed this solution : Disable beam touch mode
i have 2 phones Samsung galaxy S5 4.4.2 and Galaxy S3 4.3 the solution works for the S5 but when i reverse the order the Galaxy S3 Still prompting to send beam ... I don't know whats the problem :/ any help ?
ok here is the code
A :
public class NFCActivity extends Activity implements NfcAdapter.CreateNdefMessageCallback {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
mEditText = (EditText) findViewById(R.id.edit_text_field);
NfcAdapter mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
mEditText.setText("Sorry this device does not have NFC.");
return;
}
if (!mAdapter.isEnabled()) {
mEditText.setText("Please activate your NFC !");
Toast.makeText(this, "Please enable NFC via Settings.", Toast.LENGTH_LONG).show();
}
mAdapter.setNdefPushMessageCallback(this, this);
}
@Override
public void onResume(){
super.onResume();
setContentView(R.layout.activity_nfc);
mEditText = (EditText) findViewById(R.id.edit_text_field);
NfcAdapter mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
mEditText.setText("Sorry this device does not have NFC.");
return;
}
if (!mAdapter.isEnabled()) {
mEditText.setText("Please activate your NFC !");
Toast.makeText(this, "Please enable NFC via Settings.", Toast.LENGTH_LONG).show();
}
mAdapter.setNdefPushMessageCallback(this, this);
}
@Override
public void onPause(){
super.onPause();
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mNfcAdapter.setNdefPushMessage(null, this);
}
}
@Override
public void onStop(){
super.onStop();
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mNfcAdapter.setNdefPushMessage(null, this);
}
}
@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
String message = mEditText.getText().toString();
NdefRecord ndefRecord = NdefRecord.createMime("text/plain", message.getBytes());
NdefMessage ndefMessage = new NdefMessage(ndefRecord);
return ndefMessage;
}
}
B:
public class NFCDisplayActivity extends Activity {
private TextView mTextView;
public NfcAdapter nfcAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc_display);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
// Disable This activity from Sending Beams
nfcAdapter.setNdefPushMessage(null, this);
//NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
ScanNewTicket();
mTextView = (TextView) findViewById(R.id.text_view);
}
@Override
protected void onResume(){
super.onResume();
Intent intent = getIntent();
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcAdapter.setNdefPushMessage(null, this);
mNfcAdapter.setNdefPushMessageCallback(null, this);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage message = (NdefMessage) rawMessages[0]; // only one message transferred
String Resultcode=new String(message.getRecords()[0].getPayload());
mTextView.setText(Resultcode);
if (Resultcode.equals("seif"))
{
findViewById(R.id.imageOK).setVisibility(View.VISIBLE);
}
else
{
findViewById(R.id.imageKO).setVisibility(View.VISIBLE);
}
} else
mTextView.setText("Waiting for Next Ticket Scan");
Intent intent2 = new Intent(getApplicationContext(), getClass());
intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
nfcAdapter.enableForegroundDispatch(this, pIntent, null, null);
}
@Override
public void onPause(){
super.onPause();
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mNfcAdapter.setNdefPushMessage(null, this);
}
nfcAdapter.disableForegroundDispatch(this);
}
protected void onNewIntent(Intent intent) {
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
return; // ignore NFC intents
}
}
public void ScanNewTicket() {
Button nextScan=(Button)findViewById(R.id.nextscan);
final Context context = this;
nextScan.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
/* Intent intent = new Intent(context, NFCActivity.class);
startActivity(intent);
finish();*/
Intent i = new Intent(context,NFCDisplayActivity.class);
startActivity(i);
finish();
}
});
}
}