带 NFC 的 Android 应用程序

标签 android nfc

我正在尝试构建一个 Android 应用程序,我需要在其中执行以下操作: 它加载然后等待,直到检测到 NFC 标签。我不太关心标签的解析方式(无论是智能海报还是 URI 等)。我唯一感兴趣的是那个标签的 ID。 一旦检测到标签及其 ID,我想执行一些计算,然后返回等待状态(应用程序等待检测 NFC 标签的状态)。

我的问题是我不知道如何让我的所有代码都被标记检测触发。 (请注意,应用程序正在运行,所以这不是应用程序优先级的问题。相反,我希望我的代码通过检测到标签来触发,然后回到等待状态)。

非常感谢

最佳答案

我们开始吧,代码如下。诀窍是注册前台标签分派(dispatch),以便您的 Activity 获得所有新标签。还要指定标志 SINGLE_TOP,以便使用 onNewIntent 调用 Activity 的一个 Activity 实例。也会发布 ForegroundUtil。

public class DashboardActivity extends Activity {

NFCForegroundUtil nfcForegroundUtil = null;

private TextView info;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    info = (TextView)findViewById(R.id.info);

    nfcForegroundUtil = new NFCForegroundUtil(this);


}

public void onPause() {
    super.onPause();
    nfcForegroundUtil.disableForeground();
}   

public void onResume() {
    super.onResume();
    nfcForegroundUtil.enableForeground();

    if (!nfcForegroundUtil.getNfc().isEnabled())
    {
        Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }

}

public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    info.setText(NFCUtil.printTagDetails(tag));    

}


}

Foreground-Util(您应该修改 intent 过滤器以满足您的需要)

public class NFCForegroundUtil {

private NfcAdapter nfc;


private Activity activity;
private IntentFilter intentFiltersArray[];
private PendingIntent intent;
private String techListsArray[][];

public NFCForegroundUtil(Activity activity) {
    super();
    this.activity = activity; 
    nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());

    intent = PendingIntent.getActivity(activity, 0, new Intent(activity,
            activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Unable to speciy */* Mime Type", e);
    }
    intentFiltersArray = new IntentFilter[] { ndef };

    techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
    //techListsArray = new String[][] { new String[] { NfcA.class.getName(), NfcB.class.getName() }, new String[] {NfcV.class.getName()} };
}

public void enableForeground()
{
    Log.d("demo", "Foreground NFC dispatch enabled");
    nfc.enableForegroundDispatch(activity, intent, intentFiltersArray, techListsArray);     
}

public void disableForeground()
{
    Log.d("demo", "Foreground NFC dispatch disabled");
    nfc.disableForegroundDispatch(activity);
}

public NfcAdapter getNfc() {
    return nfc;
}   

}

关于带 NFC 的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5126982/

相关文章:

android - 从 HAL 到应用框架层的 AOSP NFC 设备调用流程

java - 如何在android中格式化和打印文本文件?

android - Android Google Fit示例不起作用

ios - 用于卡模拟的 CoreNFC?

android - 如何在 onNewIntent 执行之前拦截 NFC 标签

android - 使用 Nexus S 编写 NFC 标签

android - 我的 AsyncTask 类中有其他线程。在执行以下方法之前,我如何等待它及其所有线程完成?

android - SQLite在Kotlin和Anko中的实现

android - 如何在 Android WebView 中播放 YouTube 视频?

android - NFC标签打开应用到特定屏幕