From 007e0292bc1b7172c27b0a588613738d02c3c809 Mon Sep 17 00:00:00 2001
From: Martijn Coenen HostNfcFService is a convenience {@link Service} class that can be
* extended to emulate an NFC-F card inside an Android service component.
+ *
+ * Cards emulated by this class are based on the NFC-Forum NFC-F
+ * protocol (based on the JIS-X 6319-4 specification.) A {@link HostNfcFService HostNfcFService service} can register
+ * exactly one System Code and one NFCID2. For details about the use of
+ * System Code and NFCID2, see the NFC Forum Digital specification. To statically register a System Code and NFCID2 with the service, a {@link #SERVICE_META_DATA}
+ * entry must be included in the declaration of the service. An example of a HostNfcFService
+ * manifest declaration is shown below:
+ *
+ * The {@link android.R.styleable#HostNfcFService <host-nfcf-service>} is required
+ * to contain a
+ * {@link android.R.styleable#HostApduService_description <android:description>}
+ * attribute that contains a user-friendly description of the service that may be shown in UI.
+ * The {@link android.R.styleable#HostNfcFService <host-nfcf-service>} must
+ * contain:
+ * NFC Protocols
+ * System Code and NFCID2 registration
+ * <service android:name=".MyHostNfcFService" android:exported="true" android:permission="android.permission.BIND_NFC_SERVICE">
+ * <intent-filter>
+ * <action android:name="android.nfc.cardemulation.action.HOST_NFCF_SERVICE"/>
+ * </intent-filter>
+ * <meta-data android:name="android.nfc.cardemulation.host_nfcf_service" android:resource="@xml/nfcfservice"/>
+ * </service>
+ *
+ * This meta-data tag points to an nfcfservice.xml file.
+ * An example of this file with a System Code and NFCID2 declaration is shown below:
+ *
+ * <host-nfcf-service xmlns:android="http://schemas.android.com/apk/res/android"
+ * android:description="@string/servicedesc">
+ * <system-code-filter android:name="4000"/>
+ * <nfcid2-filter android:name="02FE000000000000"/>
+ * </host-nfcf-service>
+ *
+ *
+ *
+ *
+ *
Alternatively, the System Code and NFCID2 can be dynamically registererd for a service + * by using the {@link NfcFCardEmulation#registerSystemCodeForService(ComponentName, String)} and + * {@link NfcFCardEmulation#setNfcid2ForService(ComponentName, String)} methods. + *
+ * + *When a remote NFC devices wants to communicate with your service, it + * sends a SENSF_REQ command to the NFC controller, requesting a System Code. + * If a {@link NfcFCardEmulation NfcFCardEmulation service} has registered + * this system code and has been enabled by the foreground application, the + * NFC controller will respond with the NFCID2 that is registered for this service. + * The reader can then continue data exchange with this service by using the NFCID2.
+ * + *After service selection, all frames addressed to the NFCID2 of this service will + * be sent through {@link #processNfcFPacket(byte[], Bundle)}, until the NFC link is + * broken.
+ * + *
When the NFC link is broken, {@link #onDeactivated(int)} will be called.
*/ public abstract class HostNfcFService extends Service { /** diff --git a/core/java/android/nfc/cardemulation/NfcFCardEmulation.java b/core/java/android/nfc/cardemulation/NfcFCardEmulation.java index a50650481a731..6bb5d8e6253c1 100644 --- a/core/java/android/nfc/cardemulation/NfcFCardEmulation.java +++ b/core/java/android/nfc/cardemulation/NfcFCardEmulation.java @@ -108,12 +108,12 @@ public final class NfcFCardEmulation { * the System Code contained in the Manifest file is returned. After calling * {@link #registerSystemCodeForService(ComponentName, String)}, the System Code * registered there is returned. After calling - * {@link #removeSystemCodeForService(ComponentName)}, "null" is returned. + * {@link #unregisterSystemCodeForService(ComponentName)}, "null" is returned. * * @param service The component name of the service * @return the current System Code */ - public String getSystemCodeForService(ComponentName service) { + public String getSystemCodeForService(ComponentName service) throws RuntimeException { if (service == null) { throw new NullPointerException("service is null"); } @@ -130,6 +130,7 @@ public final class NfcFCardEmulation { return sService.getSystemCodeForService(UserHandle.myUserId(), service); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return null; } } @@ -157,7 +158,8 @@ public final class NfcFCardEmulation { * @param systemCode The System Code to be registered * @return whether the registration was successful. */ - public boolean registerSystemCodeForService(ComponentName service, String systemCode) { + public boolean registerSystemCodeForService(ComponentName service, String systemCode) + throws RuntimeException { if (service == null || systemCode == null) { throw new NullPointerException("service or systemCode is null"); } @@ -176,6 +178,7 @@ public final class NfcFCardEmulation { service, systemCode); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return false; } } @@ -187,7 +190,7 @@ public final class NfcFCardEmulation { * @param service The component name of the service * @return whether the System Code was successfully removed. */ - public boolean removeSystemCodeForService(ComponentName service) { + public boolean unregisterSystemCodeForService(ComponentName service) throws RuntimeException { if (service == null) { throw new NullPointerException("service is null"); } @@ -204,6 +207,7 @@ public final class NfcFCardEmulation { return sService.removeSystemCodeForService(UserHandle.myUserId(), service); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return false; } } @@ -221,7 +225,7 @@ public final class NfcFCardEmulation { * @param service The component name of the service * @return the current NFCID2 */ - public String getNfcid2ForService(ComponentName service) { + public String getNfcid2ForService(ComponentName service) throws RuntimeException { if (service == null) { throw new NullPointerException("service is null"); } @@ -238,6 +242,7 @@ public final class NfcFCardEmulation { return sService.getNfcid2ForService(UserHandle.myUserId(), service); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return null; } } @@ -262,7 +267,8 @@ public final class NfcFCardEmulation { * @param nfcid2 The NFCID2 to be registered * @return whether the setting was successful. */ - public boolean setNfcid2ForService(ComponentName service, String nfcid2) { + public boolean setNfcid2ForService(ComponentName service, String nfcid2) + throws RuntimeException { if (service == null || nfcid2 == null) { throw new NullPointerException("service or nfcid2 is null"); } @@ -281,6 +287,7 @@ public final class NfcFCardEmulation { service, nfcid2); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return false; } } @@ -292,12 +299,12 @@ public final class NfcFCardEmulation { * *The specified HCE-F service is only enabled when the corresponding application is * in the foreground and this method has been called. When the application is moved to - * the background, {@link #disableNfcFForegroundService(Activity)} is called, or + * the background, {@link #disableService(Activity)} is called, or * NFCID2 or System Code is replaced, the HCE-F service is disabled. * *
The specified Activity must currently be in resumed state. A good * paradigm is to call this method in your {@link Activity#onResume}, and to call - * {@link #disableNfcFForegroundService(Activity)} in your {@link Activity#onPause}. + * {@link #disableService(Activity)} in your {@link Activity#onPause}. * *
Note that this preference is not persisted by the OS, and hence must be * called every time the Activity is resumed. @@ -306,7 +313,7 @@ public final class NfcFCardEmulation { * @param service The service to be preferred while this activity is in the foreground * @return whether the registration was successful */ - public boolean enableNfcFForegroundService(Activity activity, ComponentName service) { + public boolean enableService(Activity activity, ComponentName service) throws RuntimeException { if (activity == null || service == null) { throw new NullPointerException("activity or service is null"); } @@ -327,6 +334,7 @@ public final class NfcFCardEmulation { return sService.enableNfcFForegroundService(service); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return false; } } @@ -342,7 +350,7 @@ public final class NfcFCardEmulation { * @param activity The activity which the service was registered for * @return true when successful */ - public boolean disableNfcFForegroundService(Activity activity) { + public boolean disableService(Activity activity) throws RuntimeException { if (activity == null) { throw new NullPointerException("activity is null"); } @@ -362,6 +370,7 @@ public final class NfcFCardEmulation { return sService.disableNfcFForegroundService(); } catch (RemoteException ee) { Log.e(TAG, "Failed to reach CardEmulationService."); + ee.rethrowAsRuntimeException(); return false; } }