NfcAdapter.getDefaultAdapter(this)返回null,但NFC可以工作 [英] NfcAdapter.getDefaultAdapter(this) returns null but NFC works

查看:340
本文介绍了NfcAdapter.getDefaultAdapter(this)返回null,但NFC可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在背景和前景上使用NFC读取.对于用户信息,我在我的活动和onTick(long l)方法中使用CountDownTimer(120 * 1000,5 * 1000)每5秒检查一次NFC状态.有时(我的客户说,在Android 4.2.2上,这从来没有发生过)NfcAdapter.getDefaultAdapter(BaseActivity.this)返回null,但背景和前景NFC读取仍然有效!关闭然后再打开并没有帮助.重新安装帮助.

my app uses NFC reading on background and on foreground. For user info I use CountDownTimer(120 * 1000, 5 * 1000) in my activity and method onTick(long l) to check NFC status each 5 seconds. Sometimes (on Android 4.2.2, my client says, it never happend to me) NfcAdapter.getDefaultAdapter(BaseActivity.this) returns null but background and foreground NFC reading still works! Turning off and on doesn't help. Re-install helps.

通过清单读取BG:

<activity
            android:name=".activity.NfcReaderActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>

通过意图阅读FG:

IntentFilter ndef = new IntentFilter();
ndef.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
ndef.addCategory(Intent.CATEGORY_DEFAULT);

try {
    ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
    throw new RuntimeException("fail", e);
}

mNfcAdapter = NfcAdapter.getDefaultAdapter(this.mActivity);
mNfcPendingIntent = PendingIntent.getActivity(
        this.mActivity, 0,
        new Intent(this.mActivity, this.mActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
        0
);
mNfcFilters = new IntentFilter[] {
        ndef
};
mNfcTechLists = new String[][] { new String[] {
        // White solid cards
        NfcA.class.getName()
} };

if (mNfcAdapter != null) {
    mNfcAdapter.enableForegroundDispatch(
            mActivity,
            mNfcPendingIntent,
            mNfcFilters,
            mNfcTechLists
    );
}

NfcAdapter似乎已存储或冻结.有人有同样的经历吗?问题出在哪里?

It looks like NfcAdapter is stocked or freezed. Does anybody has the same experience? Where can be the problem?

经过一些测试,我有了新的发现.仅在重新启动后才会发生这种情况.尽管我的应用程序启动了两次,并且出现了一些线程死锁,但事实并非如此.如果我以一定的延迟(3秒钟或更长时间)启动CountDownTimer(在onCreate方法中调用),则它将正常工作,并且getDefaultAdapter不为null.如果启动延迟太短(2秒或更短),我在日志中发现以下消息:"E/NFC:无法检索NFC服务",然后getDefaultAdapter返回null,直到我重新安装我的应用程序.

After some testing I have new observations. This happen only after reboot. I though my app started twice and some thread deadlocks are present but it didn't. If I start CountDownTimer (called in onCreate method) with some delay (3 or more seconds) it works and getDefaultAdapter is NOT null. If starting delay is too low (2 or less secs) i found this message in logs: "E/NFC: could not retrieve NFC service" and then getDefaultAdapter returns null until I reinstall my app.

因此执行CountDownTimer之前的短暂延迟(也许最好是Timer.schedule(...,delay,interval))是临时解决方案,但是如果有人知道什么是最佳解决方案,请告诉我.

So short delay before executing CountDownTimer (maybe better will be Timer.schedule(..., delay, interval)) is temporary solution, but if somebody knows what is the best soluttion let me know.

推荐答案

可能是在模拟器上使用的,在模拟器上您无法对NFC进行任何操作.

Probably it was used on an emulator and on the emulator you can not do anything with NFC.

这篇关于NfcAdapter.getDefaultAdapter(this)返回null,但NFC可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆