"flutter_nfc_hce plugin project"
-
This project was initiated during the process of exploring methods for exchanging data between cross-platforms using NFC.
-
The flutter_nfc_hce plugin project was created to utilize the functionality in flutter, using Qifan Yang's NFCAndroid's com.qifan.nfcbank.cardEmulation.KHostApduService. (reference: https://github.com/underwindfall/NFCAndroid)
-
Regarding the Android 13 version issues related to NFC HCE operation, modifications were made to the following section with reference to https://github.com/MichaelsPlayground/NfcHceNdefEmulator
// KHostApduService.kt private val READ_CAPABILITY_CONTAINER_RESPONSE = byteArrayOf( 0x00.toByte(), 0x0F.toByte(), // CCLEN length of the CC file 0x20.toByte(), // Mapping Version 2.0 0x00.toByte(), 0x3B.toByte(), // MLe maximum 0x00.toByte(), 0x34.toByte(), // MLc maximum 0x04.toByte(), // T field of the NDEF File Control TLV 0x06.toByte(), // L field of the NDEF File Control TLV 0xE1.toByte(), 0x04.toByte(), // File Identifier of NDEF file 0x00.toByte(), 0xFF.toByte(), // Maximum NDEF file size of 65534 bytes 0x00.toByte(), // Read access without any security 0xFF.toByte(), // Write access without any security 0x90.toByte(), 0x00.toByte(), // A_OKAY )
-
The NFC HCE operation works from Android 11 to 13, and we would like to inform you that in the current initial project, the supported AID is fixed to D2760000850101 for usage."
-
For NFC HCE Reader, you can refer to the example created using flutter_manager at https://github.com/deokgyuhan/flutter_nfc_hce_reader"
Main | Android NFC HCE |
---|---|
Android Setup
-
Add permission to your
AndroidManifest.xml
.<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <uses-feature android:name="android.hardware.nfc.hce" android:required="true" /> <uses-permission android:name="android.permission.VIBRATE" />
-
Register service to your
AndroidManifest.xml
.<service android:name="com.novice.flutter_nfc_hce.KHostApduService" android:exported="true" android:enabled="true" android:permission="android.permission.BIND_NFC_SERVICE"> <intent-filter> <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> <meta-data android:name="android.nfc.cardemulation.host_apdu_service" android:resource="@xml/apduservice"/> </service>
-
Add apduservice.xml to your res/xml
<?xml version="1.0" encoding="utf-8"?> <host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/servicedesc" android:requireDeviceScreenOn="false" android:requireDeviceUnlock="false"> <aid-group android:description="@string/aiddescription" android:category="other" > <aid-filter android:name="D2760000850101"/> </aid-group> </host-apdu-service>
-
Add strings.xml to your res/values
<resources> <string name="servicedesc">servicedesc</string> <string name="aiddescription">aiddescription</string> </resources>
import 'package:flutter_nfc_hce/flutter_nfc_hce.dart';
//plugin instance
final _flutterNfcHcePlugin = FlutterNfcHce();
//getPlatformVersion
var platformVersion = await _flutterNfcHcePlugin.getPlatformVersion();
//isNfcHceSupported
bool? isNfcHceSupported = await _flutterNfcHcePlugin.isNfcHceSupported();
//isSecureNfcEnabled
bool? isSecureNfcEnabled = await _flutterNfcHcePlugin.isSecureNfcEnabled();
//isNfcEnabled
bool? isNfcEnabled = await _flutterNfcHcePlugin.isNfcEnabled();
//nfc content
var content = 'flutter_nfc_hce';
//start nfc hce
var result = await _flutterNfcHcePlugin.startNfcHce(content);
//stop nfc hce
await _flutterNfcHcePlugin.stopNfcHce();
* Update Contributor:BugsOverBugs, atanasyordanov21
- Add the
persistMessage
option to thestartHce
function (with a default value of true).
Future<String?> startNfcHce(
String content, {
String mimeType = 'text/plain',
bool persistMessage = true,
}) {
return FlutterNfcHcePlatform.instance.startNfcHce(
content,
mimeType,
persistMessage,
);
}
-
Delete the internal storage file when executing the
stopNfcHce
function. -
Even if the
stopNfcHce
function is not executed and the app is closed, NFC HCE continues to operate. In this case, when you runstartNfcHce
after a device reboot, it reads the NDEF message from the internal storage file and initializes the message.
* Update Contributor:BugsOverBugs
-
Refactor the source code and function names.
-
Add a default argument 'mimeType' to the 'startNfcHce()' function.
-
Add 'stopNfcHce' API.
-
Write the initial NFC HCE NDEF message to internal storage and, upon app restart, read and initialize it from the file.
- Support operation in both foreground and background, but specifically enable NFC card reader access even when the device screen is fully locked on Android 12 and higher.
- When applying option 1, make sure to update the contents of 'Add apduservice.xml to your res/xml'.
- add isSupportSecureNfcSupported api