我想通过NFC将一个简单的字符串(text / plain)从一个Android设备发送到另一个。 我能够以简单的字符串格式发送消息,并通过恩智浦和其他提供商阅读一些已经制作的应用程序。 我在NDEFMessage中发送字符串。
public class MainActivity extends Activity
{
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private TextView mText,txtRead;
private int mCount = 0;
Tag detectedTag;
ReadWrite rd;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.activity_main);
mText = (TextView) findViewById(R.id.text);
txtRead = (TextView) findViewById(R.id.txtRead);
mText.setText("Scan a tag");
rd=new ReadWrite();
mAdapter = NfcAdapter.getDefaultAdapter(this);
detectedTag =getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
// Create a generic PendingIntent that will be deliver to this activity. The NFC stack
// will fill in the intent with the details of the discovered tag before delivering to
// this activity.
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("text/plain");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {
ndef,
};
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { MifareClassic.class.getName(),
MifareUltralight.class.getName(),Ndef.class.getName()} };
//onNewIntent(getIntent());
}
@Override
public void onResume() {
super.onResume();
if (mAdapter != null) mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}
@Override
public void onNewIntent(Intent intent) {
txtRead.setText("in on NewIntent");
if(getIntent().getAction().equalsIgnoreCase(NfcAdapter.ACTION_NDEF_DISCOVERED) ||
getIntent().getAction().equalsIgnoreCase(NfcAdapter.ACTION_TAG_DISCOVERED)){
txtRead.setText("inside if condition in on NewIntent");
detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//String str=rd.readTag(detectedTag);
//txtRead.setText(str);
readFromTag(getIntent());
}
Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
}
@Override
public void onPause() {
super.onPause();
if (mAdapter != null) mAdapter.disableForegroundDispatch(this);
}
public void readFromTag(Intent intent){
txtRead.setText("-1-1-1-1");
Ndef ndef = Ndef.get(detectedTag);
if(ndef==null){
txtRead.setText("ndef is null");
}
try{
txtRead.setText("0000"+"\\n");
ndef.connect();
txtRead.setText("11111");
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
txtRead.setText("2222");
if (messages != null) {
txtRead.setText("3333");
NdefMessage[] ndefMessages = new NdefMessage[messages.length];
txtRead.setText("4444444");
for (int i = 0; i < messages.length; i++) {
ndefMessages[i] = (NdefMessage) messages[i];
}
txtRead.setText("555555");
NdefRecord record = ndefMessages[0].getRecords()[0];
txtRead.setText("6666666");
byte[] payload = record.getPayload();
txtRead.setText("777777");
String text = new String(payload);
txtRead.setText("888888");
txtRead.setText("");
txtRead.setText(text);
ndef.close();
}
}
catch (Exception e) {
//txtRead.setText(e.getMessage());
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}