我在2个活动中使用NFC时遇到问题,第一个A是发件人活动而第二个B是接收器..
我的问题是第二个活动正在提示发送光束,因此应用程序都可以发送和接收(冲突)。
我尝试过这个解决方案:Disable beam touch mode
我有2部手机三星galaxy S5 4.4.2和Galaxy S3 4.3解决方案适用于S5但是当我逆转命令时Galaxy S3仍然提示发送光束......我不知道是什么问题:/有什么帮助吗?这里是代码
答:
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();
}
});
}
}