android - 如何从 Android 手机向 Pi 发送 NDEF 消息

标签 android linux nfc rfid

我正在尝试使用 nfcpy 将 NDEF 消息从我的手机发送到我的 Raspberry Pi。

我已经连接了 PN532 并且已经能够打印一些关于标签的信息。

使用我的 Android 应用程序,我可以将消息发送到另一部手机,但 Pi 没有收到。

import time
import nfc
import ndef
from threading import Thread
from nfc.clf import RemoteTarget

with nfc.ContactlessFrontend('tty:AMA0') as clf:
        tag = clf.connect(rdwr={'on-connect': lambda tag: False })
        print(tag)
        for record in tag.ndef.record:
                print(record)
        clf.close()
package com.example.t1000;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class NFCSender extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfcsender);

        Intent intent = getIntent();
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("Now Sending: ");
        stringBuilder.append(intent.getStringExtra(MainActivity.EXTRA_MYMAC));

        String displayedMessage = stringBuilder.toString();

        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setText(displayedMessage);

    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        Intent intent = getIntent();
        NdefRecord ndefRecord = NdefRecord.createMime("text/plain", intent.getStringExtra(MainActivity.EXTRA_MYMAC).getBytes());
        NdefMessage ndefMessage = new NdefMessage(ndefRecord);
        return ndefMessage;
    }
}

当拿着其中一个标签给阅读器时,我收到的错误是:

Traceback (most recent call last):
  File "readTag.py", line 11, in <module>
    for record in tag.ndef.record:
AttributeError: 'NoneType' object has no attribute 'record'

与此相反,用我的手机触摸阅读器根本不会出现错误,尽管它仍然给我 Type4ATag MIU=255 FWT=0.038664 作为输出。

之后只有拿走手机时才会出现错误:


Traceback (most recent call last):
  File "readTag.py", line 11, in <module>
    for record in tag.ndef.record:
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/__init__.py", line 278, in ndef
    if ndef.has_changed:
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/__init__.py", line 130, in has_changed
    ndef_data = self._read_ndef_data()
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 289, in _read_ndef_data
    if not (hasattr(self, "_ndef_file") or self._discover_ndef()):
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 231, in _discover_ndef
    if not self._select_ndef_application():
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 197, in _select_ndef_application
    self.tag.send_apdu(0, 0xA4, 0x04, 0x00, self._aid)
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 488, in send_apdu
    apdu = self.transceive(apdu)
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 437, in transceive
    data = self._dep.exchange(data, timeout)
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/tag/tt4.py", line 123, in exchange
    data = self.clf.exchange(data, (data[1] & 0x3F) * self.fwt)
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/clf/__init__.py", line 1051, in exchange
    rcvd_data = exchange(self.target, send_data, timeout)
  File "/home/pi/.local/lib/python2.7/site-packages/nfc/clf/pn53x.py", line 667, in send_cmd_recv_rsp
    raise nfc.clf.TimeoutError
nfc.clf.TimeoutError

最佳答案

所以,我终于找到了我的错误,就是我忘了放

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.setNdefPushMessageCallback(this, this);

到我的 onCreate 方法,之后它工作正常。

关于android - 如何从 Android 手机向 Pi 发送 NDEF 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648778/

相关文章:

android - 在 Android 上的 Cordova 应用程序中的弹出窗口后面模糊

android - Flash CS6 Android 应用程序测试 - 未找到 USB 设备

regex - 如果找到相同的单词,命令 egrep 提取一行

android - 使用 Android Beam(或 S-Beam)发送大文件

android - 带有 flatMapCompletable 的 RxJava 2 Observable 未完成

android - CSS 背景 (svg/base64) 在方向改变时被水平拉伸(stretch)

linux - sed操作需要在一些标签之间附加文本

linux - 启动 hadoop - 未找到命令

c - 在 C 项目中包含 json-c

permissions - Mifare 访问条件计算