有没有办法从代码创建 ACTION_NDEF_DISCOVERED 意图

Is there a way to create an ACTION_NDEF_DISCOVERED intent from code(有没有办法从代码创建 ACTION_NDEF_DISCOVERED 意图)
本文介绍了有没有办法从代码创建 ACTION_NDEF_DISCOVERED 意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在编写的应用程序,我想知道是否可以从代码创建 ACTION_NDEF_DISCOVERED 意图.通常,此意图是由系统在读取 ndef 格式的标签时创建的.它包含一个类型为 Tag 的 parcableextra.

For an app i'm writing i am wondering if it is possible to create an ACTION_NDEF_DISCOVERED intent from code. Normally this intent is created by the system when reading an ndef formatted tag. It contains a parcableextra of the type Tag.

意图的创建可能很容易,但是您是否也可以从代码中创建一个标签,或者我认为该类不支持该标签.

The creation of the intent is probably easy but can you also create a Tag from code or is that not supported by the class as i suppose.

目标是将带有 ndef 记录的虚拟标签广播到系统,然后可由调用 ACTION_NDEF_DISCOVERED 意图的应用程序处理.

The goal is to broadcast a virtual tag with an ndef record to the system that then can be handled by an app that calls on the ACTION_NDEF_DISCOVERED intent.

推荐答案

你可以使用反射获得一个模拟标签对象实例.像这样的东西应该可以工作:

You could get a mock tag object instance using reflection. Something like this should work:

NdefMessage ndefMsg = ...;

Class tagClass = Tag.class;
Method createMockTagMethod = tagClass.getMethod("createMockTag", byte[].class, int[].class, Bundle[].class);

final int TECH_NFC_A = 1;
final int TECH_NDEF = 6;

final String EXTRA_NDEF_MSG = "ndefmsg";
final String EXTRA_NDEF_MAXLENGTH = "ndefmaxlength";
final String EXTRA_NDEF_CARDSTATE = "ndefcardstate";
final String EXTRA_NDEF_TYPE = "ndeftype";

Bundle ndefBundle = new Bundle();
ndefBundle.putInt(EXTRA_NDEF_MSG, 48); // result for getMaxSize()
ndefBundle.putInt(EXTRA_NDEF_CARDSTATE, 1); // 1: read-only, 2: read/write
ndefBundle.putInt(EXTRA_NDEF_TYPE, 2); // 1: T1T, 2: T2T, 3: T3T, 4: T4T, 101: MF Classic, 102: ICODE
ndefBundle.putParcelable(EXTRA_NDEF_MSG, ndefMsg);

Tag mockTag = (Tag)createMockTagMethod.invoke(null,
        new byte[] { (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78 },
        new int[] { TECH_NFC_A, TECH_NDEF },
        new Bundle[] { null, ndefBundle });

这样做的问题是您将无法连接到此标签.因此,所有需要使用真实标签或注册到NFC 服务将失败.具体来说,只有

The problem with this is that you will not be able to connect to this tag. Consequently, all methods of the Ndef object (that you can get from that mock Tag instance) that require IO operations with a real tag or a real tag that is registered with the NFC service will fail. Specifically, only

  • getCachedNdefMessage(),
  • getMaxSize(),
  • getType(),
  • isWritable(),和
  • getTag()

会起作用的.

如果您不将 Tag 对象作为 NDEF_DISCOVERED 意图的一部分传递,而只使用 EXTRA_NDEF_MESSAGES 额外的意图.

So pretty much the same functionality would be available if you do not pass a Tag object as part of the NDEF_DISCOVERED intent and instead just use the EXTRA_NDEF_MESSAGES intent extra.

这篇关于有没有办法从代码创建 ACTION_NDEF_DISCOVERED 意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)