有没有一种方法来创建从code的ACTION_NDEF_DISCOVERED意图 [英] Is there a way to create an ACTION_NDEF_DISCOVERED intent from code

查看:354
本文介绍了有没有一种方法来创建从code的ACTION_NDEF_DISCOVERED意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的应用程序,我正在写我想知道是否有可能创建一个从code的ACTION_NDEF_DISCOVERED意图。通常这种意图是由系统读取NDEF格式化标签时创建的。它包含了类型标签的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.

意图的产生可能是容易的,但你能还创建code一个标签或作为我假设并不是由类的支持。

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

这里的问题是,你将无法连接到该标签。因此, NDEF 对象的所有方法(可以从模拟获得标签实例)需要与IO操作与该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()

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

将工作。

所以pretty很多相同的功能将可如果你没有传递标签对象作为 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.

这篇关于有没有一种方法来创建从code的ACTION_NDEF_DISCOVERED意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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