Host Card Emulation (HCE) Plugin for Capacitor / Ionic on Android. This project is ported and reuse code based from https://github.com/agmo-ios-team/flutter_nfc_hce that forked for Flutter HCE Plugin https://pub.dev/packages/flutter_nfc_hce you can follow the introcuction for thier project
npm install capacitor-hce-plugin-int
npx cap sync
- This plugin only support NFC Type-4 Tag with NDEF message for now
-
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.thanmgan22.plugins.nfchce.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>
- Vajag Pieteikties Apple Ios HCE entitelment. Kad piesakās norādīt AID kā D2760000850101
- Pievienot Apple HCE pie sava projekta Xcode
- Importēt Galveno Module
import {NfcEmulation} from 'capacitor-hce-plugin-int';
- Izmanto kā parastu component, data parametrā ievada vērtību ko grib emulēt kā nfc tag.
<NfcEmulation data="Hello World!"></NfcEmulation>
- January pats grib piekļūt visiem API, to var izdarīt importējot HCECapacitorPlugin
import { HCECapacitorPlugin } from 'capacitor-hce-plugin-int';
StartIosEmulation(...)
addListener('sessionInvalidated' | 'nfcDataComplete', ...)
removeListener(...)
startNfcHce(...)
stopNfcHce()
isNfcSupported()
isNfcEnabled()
isNfcHceSupported()
isSecureNfcEnabled()
enableApduService(...)
addListener('onStatusChanged', ...)
- Interfaces
- Enums
StartIosEmulation(options: { Data: string; }) => Promise<{ Data: string; }>
Param | Type |
---|---|
options |
{ Data: string; } |
Returns: Promise<{ Data: string; }>
addListener(eventName: 'sessionInvalidated' | 'nfcDataComplete', listener: (event: any) => void) => Promise<void>
Param | Type |
---|---|
eventName |
'sessionInvalidated' | 'nfcDataComplete' |
listener |
(event: any) => void |
removeListener(eventName: 'sessionInvalidated' | 'nfcDataComplete', listener: (event: any) => void) => Promise<void>
Param | Type |
---|---|
eventName |
'sessionInvalidated' | 'nfcDataComplete' |
listener |
(event: any) => void |
startNfcHce(options: { content: string; mimeType?: string; persistMessage?: boolean; }) => Promise<{ success: boolean; }>
Param | Type |
---|---|
options |
{ content: string; mimeType?: string; persistMessage?: boolean; } |
Returns: Promise<{ success: boolean; }>
stopNfcHce() => Promise<{ success: boolean; }>
Returns: Promise<{ success: boolean; }>
isNfcSupported() => Promise<{ supported: boolean; }>
Returns: Promise<{ supported: boolean; }>
isNfcEnabled() => Promise<{ enabled: boolean; }>
Returns: Promise<{ enabled: boolean; }>
isNfcHceSupported() => Promise<{ supported: boolean; }>
Returns: Promise<{ supported: boolean; }>
isSecureNfcEnabled() => Promise<{ enabled: boolean; }>
Returns: Promise<{ enabled: boolean; }>
enableApduService(options: { enable: boolean; }) => Promise<{ enabled: boolean; }>
Param | Type |
---|---|
options |
{ enable: boolean; } |
Returns: Promise<{ enabled: boolean; }>
addListener(eventName: 'onStatusChanged', listenerFunc: (response: { eventName: ReaderStatusType; }) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
Param | Type |
---|---|
eventName |
'onStatusChanged' |
listenerFunc |
(response: { eventName: ReaderStatusType; }) => void |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Prop | Type |
---|---|
remove |
() => Promise<void> |
Members | Value |
---|---|
CardEmulatorStarted |
'card-emulator-started' |
ScanError |
'scan-error' |
ScanCompleted |
'scan-completed' |
CardEmulatorStopped |
'card-emulator-stopped' |