0

私のアプリケーションは基本的に、任意のタグのタグ ID を取得するだけです

どのアプリケーションを使用するかをユーザーに尋ねずにアプリケーションを実行したい

同じアプリから直接

NFCライターとリーダーの開発方法に関するチュートリアルをインターネットで見つけました

しかし、タップするたびに、システムはユーザーにアプリケーションを選択するように求めます (アプリケーションを閉じた場合でも)

実装ごとに異なるアクティビティを作成するため、同じメソッドを使用して、Android システムはユーザーに多くのアプリケーションを選択するように求めます (それらはすべて私のアプリのアクティビティです)。

これは私のコードです:-

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.nfc.test"
    android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.NFC" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity android:name=".TapToRegisterTag" android:screenOrientation="portrait">
            <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" />
        </activity>

        <!-- Other Activities -->

    </application>

</manifest>

TapToRegisterTag.java

package com.nfc.test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TapToRegisterTag extends Activity{

    // Prepare NFC Tag variables
    Tag myTag;
    String tagID;
    NfcAdapter mNfcAdapter;
    public static final String MIME_TEXT_PLAIN = "text/plain";
    public static final String TAG = "NfcDemo";

    ImageView logo;
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register_tag_layout);

        logo = (ImageView)findViewById(R.id.imageView2);
        txt = (TextView)findViewById(R.id.textView);

        // prepare NFC
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNfcAdapter == null) {
            // Stop here, we definitely need NFC
            Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
            finish();
            return;

        }

        if (!mNfcAdapter.isEnabled()) {
            Toast.makeText(this, "NFC is disabled.", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "NFC is Enabled.", Toast.LENGTH_LONG).show();
        }

        handleIntent(getIntent());
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        /**
         * It's important, that the activity is in the foreground (resumed). Otherwise
         * an IllegalStateException is thrown. 
         */
        setupForegroundDispatch(this, mNfcAdapter);
        try{
            this.myTag = (Tag) getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
            this.tagID = OnetapActivity.bytesToHex(myTag.getId());
            Toast.makeText(this, "Tag ID: \n" + tagID, Toast.LENGTH_LONG).show();
        }catch (Exception ex){
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


    @Override
    protected void onPause() {

        stopForegroundDispatch(this, mNfcAdapter);

        super.onPause();
    }

    @Override
    protected void onNewIntent(Intent intent) { 

        handleIntent(intent);

    }

    private void handleIntent(Intent intent) {
        // TODO: handle Intent

        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

            String type = intent.getType();
            if (MIME_TEXT_PLAIN.equals(type)) {

                //new NdefReaderTask().execute(tag);
                try{
                    this.myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    this.tagID = OnetapActivity.bytesToHex(this.myTag.getId());
                    Toast.makeText(this, "Tag ID: \n" + tagID, Toast.LENGTH_LONG).show();
                }catch (Exception ex){
                    Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
                }

            } else {
                Log.d(TAG, "Wrong mime type: " + type);
            }
        } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

            // In case we would still use the Tech Discovered Intent
            this.myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            String[] techList = this.myTag.getTechList();
            String searchedTech = Ndef.class.getName();

            for (String tech : techList) {
                if (searchedTech.equals(tech)) {
                    //new NdefReaderTask().execute(tag);

                     try{
                         myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                         //this.myTag = (Tag) getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
                         this.tagID = OnetapActivity.bytesToHex(myTag.getId());
                         Toast.makeText(this, "Tag ID: \n" + tagID, Toast.LENGTH_LONG).show();
                     }catch (Exception ex){
                         Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
                     }

                    break;
                }
            }

        }

    }

    public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][]{};

        // Notice that this is the same filter as in our manifest.
        filters[0] = new IntentFilter();
        filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filters[0].addCategory(Intent.CATEGORY_DEFAULT);
        try {
            filters[0].addDataType(MIME_TEXT_PLAIN);
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("Check your mime type.");
        }

        adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    }


    public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        adapter.disableForegroundDispatch(activity);
    }

}

@xml/nfc_tech_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <!-- class name -->
    </tech-list>
</resources>

<!-- 
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>
-->
4

3 に答える 3

0

実際、友人のおかげで解決策を見つけました

これが彼の答えです:

'filter' と 'techlist' を null に変更する必要があるかもしれません

あなたのコード:

adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);

への変更

adapter.enableForegroundDispatch(activity, pendingIntent, null, null);

参照:

このメソッドに IntentFilter が提供されている場合、ACTION_NDEF_DISCOVERED と ACTION_TAG_DISCOVERED の両方のディスパッチ インテントを照合するために使用されます。ACTION_TECH_DISCOVERED は、そのディスパッチに一致する IntentFilter の外部のメタデータに依存しているため、技術リストを個別に渡すことによってインテントが処理されます。技術リストの最初のレベルの各エントリは、一致するためにすべて存在しなければならない一連の技術を表しています。最初のレベル セットのいずれかが一致する場合、ディスパッチは指定された PendingIntent を介してルーティングされます。つまり、第 2 レベルは AND で結合され、第 1 レベルのエントリは OR で結合されます。

filter と techLists パラメーターの両方に null を渡すと、ワイルド カードとして機能し、フォアグラウンド アクティビティが ACTION_TAG_DISCOVERED インテントを介してすべてのタグを受け取るようになります。

ソース:

http://developer.android.com/reference/android/nfc/NfcAdapter.html#enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][ ]))

于 2014-04-04T10:48:03.623 に答える
0

私がこれまでに知っている唯一のことは、AAR (Android Application Records) の実装です。これらはタグ内の NDEF メッセージであり、AAR で指定されたアプリケーションのみを開始します。それはかなりうまくいきます。ただし、NDEF インデントを実装する必要があります。Android マーケットには、試用できる AAR ライターがいくつかあります。

于 2014-04-04T06:38:34.950 に答える