【问题标题】:Start the NFC tag only from my specific application仅从我的特定应用程序启动 NFC 标签
【发布时间】:2014-02-24 02:14:45
【问题描述】:

我刚刚完成了基于 NFC 的应用程序。在这里,我只是简单地扫描 NFC 标签并获取 NFC 的序列号。

但是当 NFC 标签靠近设备时,它会显示所有可以扫描 NFC 标签的应用程序列表,我可以通过执行“始终”或“默认”进行设置,但我希望以某种方式以编程方式进行。

我正在寻找这个,因为它似乎在某些无法正常工作的设备中出现错误。

设备中的错误:当 NFC 标签靠近设备时,我有两台设备在对话框中甚至没有显示“始终”或“默认作为操作”。

看截图:

<activity
    android:name="net.livepatrols.thepartnerSA.NFCActivity"
    android:theme="@android:style/Theme.NoDisplay" >
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
    </intent-filter>

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

    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

NFCActivity.java

public class NFCActivity extends Activity {

    private NfcAdapter mNFCAdapter;
    private PendingIntent mNfcPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);

        // Create the OpenSqliteHelper object. It always best to create only
        // once instance of this OpenSqliteHelper
        DatabaseManager.init(this);

        mNFCAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNFCAdapter == null) {
            // Device does not support NFC
            Toast.makeText(this,
                    getString(R.string.device_does_not_support_nfc),
                    Toast.LENGTH_LONG).show();
        } else {
            if (!mNFCAdapter.isEnabled()) {
                // NFC is disabled
                Toast.makeText(this, getString(R.string.enable_nfc),
                        Toast.LENGTH_LONG).show();
            } else {
                mNfcPendingIntent = PendingIntent.getActivity(NFCActivity.this,
                        0, new Intent(NFCActivity.this, NFCActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mNFCAdapter.disableForegroundDispatch(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            mNFCAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
                    null);
            Tag mTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);

            byte byteId[] = mTag.getId();

            int size = byteId.length;
            // Convert the byte array to integer
            String str = "";
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    byte myByte = byteId[i];
                    int myInt = (int) (myByte & 0xFF);
                    str += myInt;
                }
            }

            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(
                    "yyyyMMddhhmmss");

            SharedPreferences mSharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            boolean mRegistrationStatus = mSharedPreferences.getBoolean(
                    "registration_status", false);
            boolean mExpiredStatus = mSharedPreferences.getBoolean(
                    "expire_status", true);

            Editor mEditor = mSharedPreferences.edit();

            if (mRegistrationStatus && !mExpiredStatus) {

                // Tag here started to scan the the NFC tags
                mEditor.putString("message", "Tag Read in");
                mEditor.commit();

                if (Util.isDeviceConnectedWithInternet(this)) {
                    // Upload the NFC details.

                    // Start the service and send the NFC Tag Info in the
                    // intent.
                    Intent serviceIntent = new Intent(this,
                            UploadNFCTagInfoService.class);

                    serviceIntent.putExtra("mNFCSerialNumber", str);
                    serviceIntent.putExtra("mNFCScannedTimeStamp",
                            mSimpleDateFormat.format(new Date()));
                    startService(serviceIntent);

                } else {

                    // Device is not connected with the Internet.
                    // Store the NFC Tag details in the SQLite database.
                    // When device connect with Internet later then
                    // these records will be uploaded on the database server.

                    Toast.makeText(this, getString(R.string.network_message), Toast.LENGTH_LONG).show();

                    mEditor.putString("message", "Tag has been saved in the application database to upload it later if your device is activated");
                    mEditor.commit();

                    NFCIItem mNFCItem = new NFCIItem();
                    mNFCItem.setSerialNumber(str);
                    mNFCItem.setScanTimeStamp(mSimpleDateFormat
                            .format(new Date()));

                    // Insert the record in the database.
                    DatabaseManager.getInstance().addNFCTag(mNFCItem);

                }
            } else if(!mRegistrationStatus) {
                mEditor.putString("message", "Device is not activated to scan NFC Tags");
                mEditor.commit();
            }
            finish();
        } catch (Exception e) {
            finish();
        }
    }
}

【问题讨论】:

    标签: android nfc


    【解决方案1】:

    我知道如何解决此问题的唯一方法是在您的标签上添加额外的AAR record(特殊 NDEF 记录)。随后,只有您的应用程序正在启动,没有人再询问。

    【讨论】:

    • 哦,我不想先把数据写入NFC标签
    • 那么,如果无法进行意图过滤,则无法避免选择应用程序,除非只有一个应用程序可用。
    • 任何选项,以便记住应用程序,如“始终”?
    • 没有。我也经历过这个问题。也许在未来的版本中,但我想这是一些安全功能。
    【解决方案2】:

    你不能。如果应用程序已打开,您可以使用前台调度使您的应用程序成为首选应用程序(您似乎已经在使用)。您可以做的最好的事情是调整您的 NFC 技术过滤器以尽可能匹配标签(技术 + NDEF 等)。对于不显示“始终”选项的设备,如果在对话框中双击应用会发生什么情况?

    【讨论】:

    • 如果我在对话框中双击应用程序,那么它再次要求进一步扫描
    • 但是为什么这个问题它没有任何选项可以记住,就像所有设备通常都有的一样,我在两个设备上测试了这个应用程序,我都没有找到那个选项
    • 这可能是由于一些供应商的定制,你只需要忍受它。
    • 那么有什么可以记住这个动作的替代品吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2015-02-27
    相关资源
    最近更新 更多