Android API 方法 CardEmulation.getInstance() 的 UnsupportedOperationException

UnsupportedOperationException for Android API method CardEmulation.getInstance()

我需要检查设备上是否启用了卡模拟。我找到了一种方法 here:

boolean isDefault = CardEmulation
                .getInstance(NfcAdapter.getDefaultAdapter(this))
                .isDefaultServiceForCategory(
                        new ComponentName(this, MyPaymentService.class),
                        CardEmulation.CATEGORY_PAYMENT);

这似乎适用于某些设备,但不适用于所有设备。

例如,三星GT-I9300I(Android 4.4)有NFC模块,但在设置中没有显示点击支付选项。

当我的应用程序在该设备上运行时出现以下错误:

E/CardEmulation: This device does not support card emulation
09-26 16:41:13.592 2625-2625/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.android.settings, PID: 2625
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.nfc.PaymentDefaultDialog}: java.lang.UnsupportedOperationException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
                                                     at android.app.ActivityThread.access0(ActivityThread.java:171)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:146)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5679)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.UnsupportedOperationException
                                                     at android.nfc.cardemulation.CardEmulation.getInstance(CardEmulation.java:159)
                                                     at com.android.settings.nfc.PaymentBackend.(PaymentBackend.java:53)
                                                     at com.android.settings.nfc.PaymentDefaultDialog.onCreate(PaymentDefaultDialog.java:57)
                                                     at android.app.Activity.performCreate(Activity.java:5582)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500) 
                                                     at android.app.ActivityThread.access0(ActivityThread.java:171) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:146) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5679) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
                                                     at dalvik.system.NativeStart.main(Native Method)

但是,根据 the documentationCardEmulation 自 API 级别 19 (Android 4.4) 起可用。

谁能解释一下为什么我会收到此异常?我该如何克服这个问题?

错误在日志输出中很清楚:

E/CardEmulation: This device does not support card emulation

此class只能在支持卡模拟和基于AID的路由配置的设备上使用。这也记录在 the documentation of the class CardEmulation:

Use of this class requires the FEATURE_NFC_HOST_CARD_EMULATION to be present on the device.

因此,您只能在实际支持 HCE 功能的设备上使用方法 getInstance()。请注意,host 卡模拟在这里有点误导,因为使用 OffHostApduService 声明管理路由配置和使用 CardEmulation [=31 也需要该功能=] 用于与 OffHostApduService 相关的任何操作。因此,此功能适用于 HCE 和基于安全元件的卡仿真的路由配置管理。然而,一些设备制造商开发了其他机制来测试基于安全元件的卡仿真的可用性(并且可能还用于管理路由)。

您可以在调用 CardEmulation.getInstance() 方法之前使用以下代码检查 HCE 功能:

boolean isDefault = false;
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
    isDefault  = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this))
                              .isDefaultServiceForCategory(
                                      new ComponentName(this, MyPaymentService.class),
                                      CardEmulation.CATEGORY_PAYMENT);
}