NFC数据写入
1. 介绍
NFC(Near Field Communication)是一种近场通信技术,可以实现简单的数据交换和连接。在许多现代智能设备中,如手机和平板电脑中都有NFC芯片。NFC数据写入是指将数据写入到NFC标签(或卡片)中的过程。本文将介绍NFC数据写入的原理、代码示例和应用场景。
2. 原理
NFC数据写入的原理是通过NFC芯片和NFC标签之间的通信来实现。NFC标签可以是可读写的,也可以是只读的。在写入数据之前,需要先将NFC芯片与NFC标签建立连接。然后,将要写入的数据发送给NFC芯片,再由NFC芯片将数据写入到NFC标签中。
3. 示例代码
下面是一个使用Android开发平台的代码示例,演示了如何使用NFC技术将数据写入到NFC标签中。
public class MainActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;
private PendingIntent pendingIntent;
private IntentFilter[] intentFilters;
private String[][] techLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// NFC不可用
// 处理NFC不可用的情况
}
pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("Failed to add data type.", e);
}
intentFilters = new IntentFilter[]{ndef};
techLists = new String[][]{{NfcF.class.getName()}};
}
@Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, techLists);
}
}
@Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] {
createTextRecord("Hello, World!")
});
writeNdefMessageToTag(ndefMessage, detectedTag);
}
}
private NdefRecord createTextRecord(String message) {
byte[] language = Locale.getDefault().getLanguage().getBytes(Charset.forName("US-ASCII"));
final byte[] text = message.getBytes(Charset.forName("UTF-8"));
final int languageSize = language.length;
final int textLength = text.length;
final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textLength);
payload.write((byte) (languageSize & 0x1F));
payload.write(language, 0, languageSize);
payload.write(text, 0, textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0],
payload.toByteArray());
}
private void writeNdefMessageToTag(NdefMessage message, Tag tag) {
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
// 标签只读,无法写入数据
// 处理标签只读的情况
return;
}
ndef.writeNdefMessage(message);
ndef.close();
}
} catch (Exception e) {
// 写入标签失败
// 处理写入标签失败的情况
}
}
}
上述代码使用了Android的NFC API来实现NFC数据写入。首先,在onCreate()
方法中,我们获取了默认的NfcAdapter实例,并创建了一个PendingIntent、IntentFilter和techLists。然后,在onResume()
和onPause()
方法中,我们启用和禁用了前台调度。最后,在onNewIntent()
方法中,我们处理了接收到的NDEF_DISCOVERED意图,并调用`write