【问题标题】:Getting the NFC TagID of the NTAG and MIFARE smart card获取 NTAG 和 MIFARE 智能卡的 NFC TagID
【发布时间】:2015-04-21 11:13:35
【问题描述】:

我是 NFC 技术的新手,我想从读写标签开始。出于阅读目的,我使用的是 MIRFARE 1K 智能卡,而对于写作,我使用的是 NTAG203。我想要的只是在读取过程中获取 TagID 并将 TagID 和日期时间戳发送到 NTAG203。

问题:

  1. 我的应用程序在编译时遇到困难。它一直给我错误生成最终存档:无法创建'C:\Android WS\TabletApp\bin\TabletApp.apks':访问被拒绝错误。我清理了项目并重新启动 Eclipse,但徒劳无功。只有当我重新启动笔记本电脑时它才会消失。对此有何建议?

  2. 当应用程序工作时,它仍然没有给我 ID。我扫描我的卡/标签,MainActivity(此处为 ScanningActivity)一次又一次地加载。控制不会比这更进一步。关于如何处理它有什么建议吗?

public class ScanningActivity extends Activity {
    private NfcAdapter sNfcAdapter;
    Locale locale = new Locale("en", "US");
    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
    boolean encodeInUtf8 = false;
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    char status = (char)(utfBit + langBytes.length);

    static String bin2hex(byte[] data) {
        return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
    }
    /* write to tag */

    boolean writeNdefMessageToTag(NdefMessage message, Tag detectedTag) throws FormatException {
        int size = message.toByteArray().length;
        try {
            Ndef ndef = Ndef.get(detectedTag);
            if (ndef != null) {
                ndef.connect();
                if (!ndef.isWritable()) {
                    Toast.makeText(this, "Tag is read-only.", Toast.LENGTH_SHORT).show();
                    return false;
                }
                if (ndef.getMaxSize() < size) {
                    Toast.makeText(this,
                                   "The data cannot be written since the tag capacity is" + ndef.getMaxSize() +
                                   " bytes and the data to be transferred is " + size + " bytes",
                                   Toast.LENGTH_LONG).show();
                    return false;
                }

                ndef.writeNdefMessage(message);
                ndef.close();
                Toast.makeText(this, "Data sent to the Tag", Toast.LENGTH_LONG).show();
                return true;

            } else {
                NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);
                if (ndefFormat != null) {
                    try {
                        ndefFormat.connect();
                        ndefFormat.format(message);
                        ndefFormat.close();
                        Toast.makeText(this, "Data sent", Toast.LENGTH_LONG).show();
                        return true;
                    } catch (IOException e) {
                        Toast.makeText(this, "Unable to format tag", Toast.LENGTH_LONG).show();
                        return false;
                    }
                } else {
                    Toast.makeText(this, "Not supported tag", Toast.LENGTH_LONG).show();
                    return false;
                }

            }
        } catch (IOException e) {
            Toast.makeText(this, "Sending failed", Toast.LENGTH_LONG).show();
        }
        return false;

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanning);

        TextView tg = (TextView)findViewById(R.id.tagid);
        final TextView tvtime = (TextView)findViewById(R.id.datetime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
        String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
        tvtime.setText(currentTimeStamp);

        sNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
        if (sNfcAdapter == null) {
            Toast.makeText(this, "This device does not support NFC or you have not scanned the card properly",
                           Toast.LENGTH_LONG).show();
            return;
        }

        if (!sNfcAdapter.isEnabled()) {
            Toast.makeText(this, "Looks like the NFC is not enabled on your device", Toast.LENGTH_LONG).show();
            new AlertDialog.Builder(this).setTitle("NFC disabled").setMessage(
                    "Looks like the NFC is not enabled on your device. Please enable it.").setPositiveButton(
                    "Ok", null).show();
        }
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
            Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
            try {
                String TagID = tag.getId().toString();
                tg.setText(bin2hex(tag.getId()));
                Toast.makeText(getApplicationContext(), bin2hex(tag.getId()), Toast.LENGTH_LONG).show();
                Toast.makeText(this, TagID, Toast.LENGTH_LONG).show();
            } catch (Exception ex) {
                new AlertDialog.Builder(this).setTitle("Error!").setPositiveButton("Ok",
                                                                                   new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(ScanningActivity.this, ScanningActivity.class);
                        startActivity(i);
                    }
                }).setMessage(ex.getMessage() + "\n\nPlease click Ok. This will restart the application").create().show();

                new AlertDialog.Builder(this).setTitle("Transferring the data").setPositiveButton("Ok",
                                                                                                  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        TextView tvData = (TextView)findViewById(R.id.Data);
                        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
                            Tag tag1 = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
                            String DataToSend = tvtime.getText().toString() + tag1.getId().toString();
                            //String DataToSend = "Data";
                            //tvData.setText(tvtime.getText().toString()+tag.getId().toString());
                            byte[] textBytes = DataToSend.getBytes(utfEncoding);
                            byte[] dataa = new byte[1 + langBytes.length + textBytes.length];
                            dataa[0] = (byte)status;
                            System.arraycopy(langBytes, 0, dataa, 1, langBytes.length);
                            System.arraycopy(textBytes, 0, dataa, 1 + langBytes.length, textBytes.length);;
                            NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                                                                   NdefRecord.RTD_TEXT, new byte[0], dataa);
                            NdefMessage newMessage = new NdefMessage(new NdefRecord[] { textRecord });

                            try {
                                writeNdefMessageToTag(newMessage, tag1);
                            } catch (FormatException e) {

                                e.printStackTrace();
                            }
                        }
                    }
                }).setMessage(
                        "Place this tablet on the RFID Tag on the Microbiology Form. Keep it there until you get to the next screen").create().show();

            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.scanning, menu);
        return true;
    }
}

清单是

<uses-permission android:name="android.permission.NFC"/>

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.tabletapp.ScanningActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

更新:

  1. 应用程序不显示任何 toast。当我在设备上放置卡片/标签时,系统会询问我喜欢使用哪个应用程序。我选择了我们正在谈论的那个,然后再次加载“ScanningActivity”(MainActivity)。既不向我显示 ID,也不通过扫描活动。

  2. 我想从卡上读取 ID,然后将其连同时间戳一起写入另一个标签。

【问题讨论】:

  • 我不明白为什么有人反对这个问题。如果您觉得这不是一个合适的问题或缺少什么,也请发表评论。
  • 您遇到的实际问题是什么?您的应用程序是否显示任何吐司(带有标签 ID 或任何错误消息的吐司)?您想从标签中读取 ID,然后将其写入同一个标签还是另一个标签?您的应用程序是否将 ID 读入 R.id.tagid,而您的问题是您无法写入第二个标签,或者它甚至无法写入?
  • @MichaelRoland 这里是答案。很抱歉之前没有说清楚。 1.该应用程序没有显示任何吐司。当我在设备上放置卡片/标签时,系统会询问我喜欢使用哪个应用程序。我选择了我们正在谈论的那个,然后再次加载“ScanningActivity”(MainActivity)。既不向我显示 ID,也不通过扫描活动。 2. 我想从卡片中读取 ID,然后将其与时间戳一起写入另一个标签。

标签: android eclipse android-intent nfc mifare


【解决方案1】:

你的代码有很多问题,所以我只关注最明显的问题:

  1. 您在 AndroidManifest.xml 中注册了 NDEF_DISCOVERED。但是,在onCreate() 中,只有在获得TAG_DISCOVERED 意图时才会读取ID。您必须更改它以匹配 NDEF_DISCOVERED 意图。 (我在此假设您的标签包含与您的意图过滤器匹配的文本记录。)

  2. tag.getId().toString() 不会给你太多有用的信息,因为tag.getId() 返回一个字节数组。您可能总是希望使用 bin2hex() 方法将 ID 转换为人类可读的字符串。

  3. 您应该避免在单击按钮时与 NFC 标签进行任何交互。 (即您当前在onClick(DialogInterface dialog, int which) 中所做的事情。)按钮单击和 NFC 交互都应视为人工输入,您应避免同时需要两个人工输入。 (此外,bott 是基于事件的,您不能直接组合它们。)您可以改为在onClick() 方法中设置一个标志,指示应在下一次 NFC 发现事件时将数据写入标签。

    李> 1234563这样,您的前台活动可以处理标签的重复扫描,而无需重新启动(并且无需控制任何其他可能由标签触发的活动)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多