android原生开发nfc

NFC(Near Field Communication)是一种短距离无线通信技术,广泛应用于移动支付、身份认证、门禁等场景。在Android原生开发中,我们可以利用NFC技术实现与设备之间的数据传输和交互,本文将介绍Android原生开发的NFC原理和详细实现方法。

一、NFC技术原理

NFC技术是一种基于射频识别(RFID)技术的近距离无线通信技术,它基于13.56MHz的射频信号进行通信。NFC设备分为读卡器和标签两种类型,其中读卡器可以主动向标签发送命令,而标签只能在读卡器的激励下被动响应。

NFC标签包含一个芯片和一个天线,芯片用于存储数据和处理命令,天线用于接收和发送射频信号。NFC标签可以存储一定量的数据,包括文本、URL、联系人信息、支付信息等等。当读卡器靠近标签时,它会向标签发送一些命令,标签会根据接收到的命令执行相应的操作,并将响应数据返回给读卡器。

二、NFC开发环境搭建

在进行NFC开发之前,需要在Android Studio中配置相关环境。首先,在build.gradle文件中添加以下依赖:

```

dependencies {

implementation 'com.android.support:support-v4:28.0.0'

implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.android.support:design:28.0.0'

implementation 'com.android.support:cardview-v7:28.0.0'

implementation 'com.android.support:recyclerview-v7:28.0.0'

implementation 'com.android.support:gridlayout-v7:28.0.0'

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.android.support:support-v13:28.0.0'

implementation 'com.android.support:multidex:1.0.3'

implementation 'com.android.support:preference-v7:28.0.0'

implementation 'com.android.support:preference-v14:28.0.0'

implementation 'com.android.support:support-vector-drawable:28.0.0'

implementation 'com.android.support:animated-vector-drawable:28.0.0'

implementation 'com.android.support:mediarouter-v7:28.0.0'

implementation 'com.android.support:palette-v7:28.0.0'

implementation 'com.android.support:slices-builders:1.0.0'

implementation 'com.android.support:slices-core:1.0.0'

implementation 'com.android.support:slices-view:1.0.0'

implementation 'com.android.support:textclassifier:1.0.0'

implementation 'com.android.support:wear:28.0.0'

implementation 'com.android.support:wear-watchface:1.0.0'

implementation 'com.android.support:wear-input:1.0.0'

implementation 'com.android.support:wear-remote-interactions:1.0.0'

implementation 'com.android.support:wear-phone-interactions:1.0.0'

implementation 'com.android.support:wearable:2.4.0'

implementation 'com.google.android.gms:play-services-wearable:16.0.1'

implementation 'com.google.android.gms:play-services-auth:16.0.1'

implementation 'com.google.android.gms:play-services-nfc:16.0.1'

}

```

然后,在AndroidManifest.xml文件中添加以下权限和特性:

```

```

三、NFC读取标签数据

在Android中,我们可以使用NfcAdapter类来控制NFC设备的读写操作。首先,需要获取NfcAdapter实例并检查设备是否支持NFC功能:

```

NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

if (mNfcAdapter == null) {

// 设备不支持NFC功能

return;

}

```

接着,在onResume()方法中启动NFC读取功能:

```

@Override

protected void onResume() {

super.onResume();

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

}

```

在onPause()方法中停止NFC读取功能:

```

@Override

protected void onPause() {

super.onPause();

mNfcAdapter.disableForegroundDispatch(this);

}

```

当设备靠近NFC标签时,系统会自动触发onNewIntent()方法,我们可以在该方法中获取标签数据:

```

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {

// 获取标签数据

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

byte[] id = tag.getId();

String idStr = ByteArrayToHexString(id);

Toast.makeText(this, "标签ID:" + idStr, Toast.LENGTH_SHORT).show();

}

}

```

其中,ByteArrayToHexString()方法用于将字节数组转换为十六进制字符串:

```

private String ByteArrayToHexString(byte[] data) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < data.length; i++) {

int j = data[i] & 0xFF;

if (j < 16) {

sb.append("0");

}

sb.append(Integer.toHexString(j));

}

return sb.toString();

}

```

四、NFC写入标签数据

除了读取标签数据外,我们还可以利用NFC技术将数据写入标签。在Android中,我们可以使用NdefRecord类来创建NDEF格式的数据,并使用NdefMessage类将多个NDEF记录组合成一个NDEF消息。首先,需要获取Ndef实例并检查标签是否支持NDEF格式:

```

Ndef ndef = Ndef.get(tag);

if (ndef == null) {

// 标签不支持NDEF格式

return;

}

```

接着,需要将NDEF消息写入标签:

```

NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] {

NdefRecord.createMime("text/plain", "Hello, NFC!".getBytes())

});

if (ndef.isWritable()) {

ndef.connect();

ndef.writeNdefMessage(ndefMessage);

ndef.close();

Toast.makeText(this, "写入成功!", Toast.LENGTH_SHORT).show();

} else {

// 标签只读

Toast.makeText(this, "标签只读!", Toast.LENGTH_SHORT).show();

}

```

以上代码将创建一个包含文本“Hello, NFC!”的NDEF消息,并将其写入标签。如果标签只读,则无法写入数据。

五、总结

本文介绍了Android原生开发中NFC技术的原理和详细实现方法,包括读取标签数据和写入标签数据。NFC技术在移动支付、身份认证、门禁等场景中有着广泛应用,掌握NFC技术的开发方法对于开发者来说是非常重要的。