当前位置: 首页 > 知识库问答 >
问题:

如何读到nfc标签与Android应用程序

洪光霁
2023-03-14

大家好,我正在开发android应用程序,需要扫描设备的nfc标签。我对nfc一无所知,在阅读了大量教程后,我找到了一些方法来检查手机中是否启用了nfc,但我不知道如何读取nfc标签。

这是我的promise

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this);
        pref = getApplicationContext().getSharedPreferences("Cordinate", 0);
        editor = pref.edit();
        nfc1 = (ImageView) findViewById(R.id.checknfc1);
        mDisplay = (TextView) findViewById(R.id.display);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password1);
        login = (TextView) findViewById(R.id.btnRegister);
        context = getApplicationContext();

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        imei = telephonyManager.getDeviceId();


        gcm = GoogleCloudMessaging.getInstance(this);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Toast.makeText(MainActivity.this, "i  am workin",
                // Toast.LENGTH_LONG).show();

                GPSService gpstest =  new GPSService(imei,MainActivity.this);
                Thread data2 = new Thread(gpstest);
                data2.start();
                gps = new GPSTracker(MainActivity.this);
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // \n is for new line
                    //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   
                }
                else
                {

                    Toast.makeText(getApplicationContext(), "GPS is not working", Toast.LENGTH_LONG).show(); 
                }
                username1 = username.getText().toString();
                password2 = password.getText().toString();
                username.setText("");
                password.setText("");
                new RegisterBackground().execute();

            }
        });


        nfc1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                if (mNfcAdapter != null) {
                   // "Read an NFC tag"
                    Toast.makeText(MainActivity.this, "Read an NFC tag", Toast.LENGTH_LONG).show();
                     // create an intent with tag data and deliver to this activity
                    mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
                        new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

                    // set an intent filter for all MIME data
                    IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

                    onNewIntent(getIntent());
                    try {
                        ndefIntent.addDataType("*/*");
                        mIntentFilters = new IntentFilter[] { ndefIntent };
                    } catch (Exception e) {
                        Log.e("TagDispatch", e.toString());
                    }
                } else {
                    Toast.makeText(MainActivity.this, "not able to read NFC tag", Toast.LENGTH_LONG).show();
                }

                //Toast.makeText(MainActivity.this, datanfc, Toast.LENGTH_LONG).show();


            }
        });

    }

我的帐篷

public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

   String nfctag =tag.toString();

   Toast.makeText(MainActivity.this, nfctag, Toast.LENGTH_LONG).show();

}

共有1个答案

司马宏邈
2023-03-14

有关更多信息,请参阅本文档http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

基本上,首先你应该在ACTION\u TECH\u DISCOVERED

然后相应地更新清单,

<activity>
...
<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>

然后从扫描的标签中获取数据

public void onResume() {
    super.onResume();
    ...
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
    }
    //process the msgs array
}
 类似资料:
  • 类似的问题 - 如何在Android中读取检测到的NFC标签(NDEF内容)详细信息? 我希望我的android应用程序能够读取和解析检测到的NDEF消息。 我已经编辑了AndroidManifest.xml来检测nfc标签,并添加了意图过滤器,如下所示 我相信这很好,因为当我使用SDK附带的NFCDemo示例应用程序创建MockNDEF标签时,当我可以选择处理这些生成的标签的应用程序列表出现时,

  • 我正在尝试使用nfc android库读取ISO15693 RFID标签: 有关该标签的更多信息如下:http://img42.com/gw07d+

  • 我正在使用Mifare Ultralight C标记,并以NDEF格式向它们写入数据。我使用NDEF-NFC库为我的NDEF消息创建原始字节。我的NDEF消息是 我得到的输出是D1 01 04 54 02 65 6E 31(十六进制)。如果我按原样将此字节数组写入标记: 从标签读取并使用相同的NDEF NFC库后,我能够转换回所需的NDEF消息。 Android应用程序不识别NDEF消息。我尝试了

  • 我有一个应用程序可以读取一个NDEF标签,没什么大不了的:-) 我找到了这个链接:如何发现NFC标签是否还在Android的范围内?什么是一个开始,但我不知道如何更新标签。 我很挣扎,我甚至不知道我尝试做的事情在技术上是否可行。 有没有人知道怎么做的?干杯 对不起,我试着看了一些教程和例子,但我还是不明白。 这是我的全部密码。读取标签需要长得多的时间,有时并不需要。我不知道如何和在哪里更新标签,以

  • 我在这里找到了一些关于使用Android阅读NFC标签的最近帖子。我得到的结论是,执行NFC读取动作会触发一个分离的意图。 那么第一个问题:在我的清单中列出意图过滤器是否必要? 我认为这是没有必要的,因为我不想推出我的应用程序通过NFC标签事件,对吗? 第二个问题:我如何保持我的NFC阅读逻辑/功能与我的app/活动相关? 例如,在iOS中,在VC中需要时有一个简单的NFC会话。

  • 我有一个小项目要将数据写入Ntag 213、215和216。 我成功地使用MifareUltraLight方法写入数据。问题是当我使用 NfcTools 扫描时,数据格式与我预期的不同。 这是我期望的格式。 这是我得到的格式。 我希望当Ntag扫描时,如果没有安装应用程序,它会打开浏览器。 我使用Mi的方法,因为N标签将受密码保护。我尝试了两种不同的方式来写入数据。 > 我使用命令N瘤手动处理写入