NfcAdapter.getDefaultAdapter(this) returns null but NFC works

心已入冬 提交于 2019-12-24 17:46:04

问题


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 reading via manifest:

<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 reading via intent:

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
    );
}

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


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.

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.


回答1:


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



来源:https://stackoverflow.com/questions/37660861/nfcadapter-getdefaultadapterthis-returns-null-but-nfc-works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!