当前位置: 首页 > 知识库问答 >
问题:

NFC堆栈停止寻找新标签

洪光霁
2023-03-14

实际上,我正在开发一个应用程序,它需要处理一个NFC标签,使用户能够使用这个应用程序。

这个活动是在应用程序启动和简历时调用的。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);

    if (nfcAdapter == null || !nfcAdapter.isEnabled())
    {
        finish();
        return;
    }

    final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
    final PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);

    //method to handle your intent
    handleTag(getIntent());
}

@Override
public void onResume()
{
    super.onResume();

    final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
    final PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

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

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    handleTag(intent);

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}

private void handleTag(final Intent intent)
{
    String action = intent.getAction();
    final Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action))
    {
        try
        {
            Class isoDep = Class.forName("android.nfc.tech.IsoDep");
            Method isoDep_get = isoDep.getDeclaredMethod("get", Tag.class);

            final IsoDep techIsoDep = (IsoDep) isoDep_get.invoke(null, tag);

            if (techIsoDep != null)
            {
                // --- Tag detected
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, "Exception while processing IsoPcdA object", e);
        }
    }
}

编辑:我注意到这是因为,即使屏幕没有变黑,cpu或NFC阅读器进入睡眠模式,我也必须锁定和解锁手机,使它再次工作,我现在正在寻找一种方法,使cpu一直运行,我已经尝试过:

仅适用于Galaxy Xcover 3(Android 6.0.1)

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();

我仍然无法使它在Galaxy Xcover 4(Android 8.1)上运行

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

共有1个答案

澹台华晖
2023-03-14

当你在Android Studio中使用调试模式,而手机丢失标签时,就会发生这种情况。当您关闭NFC并再次打开时,手机将无法检测到其他标签。我没有读你的代码,因为你说同样的情况发生在另一个应用程序上。只需尝试关闭和打开手机中的NFC。

 类似资料:
  • 我正在寻找一种方法来停止/杀死线程,我看到Thread.stop()已被弃用。所以我开始寻找另一个解决方案,并看到多个帖子建议这样做: 但这不会阻止我的线程,我的线程看起来像这样: 有人得到我可以用来停止/杀死我的线程的解决方案吗?非常感谢任何帮助。

  • 我正在尝试流式传输从FFmpeg编码的Webm视频文件。 然而,当我寻找到一个未被缓冲的位置时,视频播放停止。 我已验证是否下载了正确的字节范围。 知道是什么引起的吗?在x264中编码的mp4文件中,查找工作完美无瑕。Webm似乎是个问题。可能是视频编码方式有问题。 ffmpeg-i输入。mp4-编解码器:v libvpx-质量最佳-b:v 4000k-qmin 10-qmax 42-最大速率40

  • 嗨,我有个大问题, 我一直在创建一个自动化脚本,并在构建过程中对其进行测试。我已经完成了第109行,现在第64行已经开始失败。 这条线路之前已经工作了很多次,在没有任何真实信息帮助解决的情况下突然停止。 这是我使用xpath从中提取元素的代码 我使用以下代码使用xpath选择列表中的第一项 这行代码一整天都运行得很好,现在每次执行脚本都会失败。我现在得到一个错误,没有找到元素。 当我导航到页面并使

  • 问题内容: 在Java日志中,很多时候我会得到类似以下内容的信息: 有谁知道如何显示完整的堆栈跟踪(即显示其他113行)? Throwable 的JavaDocs(用于Java 7) 对正在发生的事情有非常详细的解释。 问题答案: 当您看到“ … 113更多”时,表示“由…引起的”异常的其余行与父异常的那一点起的其余行相同。 例如,您将拥有 这两个堆栈跟踪在AbstractBatcher.exec

  • 下面的代码有一个TaskMonitor类,它是Stephen Cleary的NotifyTask类的修改版本。Worker类使用TaskMonitor类异步运行Worker.CheckStatus。 Worker.CheckStatus经常会引发异常(例如数据库连接问题)。我希望TaskMonitor.Monitor阻止异常到达Program.Main并通过Exception和InnerExcep

  • 有没有办法弄清楚Android设备上的NFC堆栈(或芯片组)?我正在为NfcV类(ISO15693标签)创建一个带有自定义实现的应用程序。在Nexus S、Galaxy S2、Nexus 7等较旧的设备上,NFC芯片组来自恩智浦,唯一支持的ISO15693标签是恩智浦ICODE标签。我有一个依赖于收发器函数的自定义实现。它与TI和ST微标签一起工作很漂亮。但问题是,在Nexus10、Nexus4和