2011-04-05 123 views

回答

15

NDEF Tools for Android公用事业项目有助于执行以下操作

  1. Detect,然后
  2. Readwrite,或
  3. Beam(推) NFC内容

该项目还包含所有标准化NDEF记录类型的数据绑定,与使用Android SDK中包含的(基于字节数组的)NDEF类相比,它真正简化了一些事情。

另请参见NFC Eclipse plugin图形NDEF编辑器 - 附带实用程序app,它可以读取和写入标签和光束,还具有NFC读卡器集成功能。

顺便说一句,你正在寻找Android应用程序记录启动应用程序。推出的'功能'不需要任何实际的实施;它内置于Android> = 4.0中,因此将该记录放置在标签上就足够了。

编辑:更新链接

+0

我在NFC阅读和写作读书了,我正要删除蚀去了ADT,将在NFC -eclipse-plugin兼容/可用? YouTube教程看起来很干净 – alex 2015-04-15 09:54:33

+0

对不起,我的意思是Android Studio – alex 2015-04-15 10:06:02

+0

这看起来很有前途。但是,我们是否可以在同一活动中阅读和书写? – 2015-04-28 18:07:49

11

首先,你必须获得针对NFC AndroidManifest.xml文件的权限。权限是:

  <intent-filter> 
      <action android:name="android.nfc.action.TAG_DISCOVERED" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

在您的活动onCreate()方法你:

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

将执行NFC读/写操作的行为,该活动在AndroidManifest.xml文件中添加此意图过滤器必须初始化NFC适配器和定义待定意图:

NfcAdapter mAdapter; 
PendingIntent mPendingIntent; 
mAdapter = NfcAdapter.getDefaultAdapter(this); 
if (mAdapter == null) { 
    //nfc not support your device. 
    return; 
} 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

在的onResume()回拨使前景调度来检测NFC意图。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 

在的onPause()回调您必须禁用于地面调度:

if (mAdapter != null) { 
     mAdapter.disableForegroundDispatch(this); 
    } 

在onNewIntent()回调方法,你会得到新的NFC意向。得到意向后,你必须解析检测卡的意图:

@Override 
protected void onNewIntent(Intent intent){  
    getTagInfo(intent) 
} 

private void getTagInfo(Intent intent) { 
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
} 

现在你有标签。然后,您可以检查标签技术列表以检测该标签。标签检测技术是在这里My Another Answer 全面完整的项目是在这里My github profile

+0

卡里姆先生,我想创建自己的数字名片,请让我知道我需要购买哪张nfc贴纸? – Sun 2015-03-10 11:40:29

+0

您可以使用Mifare Classic 1k。它有巨大的内存(1024字节),每个块可以一次写入16个字节。您也可以锁定数据以防止进一步写入。 – 2015-03-10 15:35:59

2

首先是把这些在你的清单:

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

,然后把它放进你想读NFC你的活动:

<intent-filter> 
       <action android:name="android.nfc.action.TAG_DISCOVERED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

末增加前进喜欢我的活动:

/**版权所有(C)2010 Android开源项目项目 *版权所有(C)2011 AdamNybäck * *根据Apache许可证2.0版(“许可证”)获得许可; *除遵守许可证外,您不得使用此文件。 *您可以在获得许可证的副本 * * http://www.apache.org/licenses/LICENSE-2.0 * *除非适用法律要求或书面同意,根据许可证分发的软件 *分布在“原样”的基础, *没有任何形式的保证或条件,无论是明示还是暗示。 *请参阅许可证以了解许可证下的特定语言管理权限和 *限制。 */

package ***.***.***.***; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.drawable.AnimationDrawable; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.Ndef; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.balysv.materialripple.MaterialRippleLayout; 
import com.blogspot.android_er.androidnfctagdiscovered.R; 
import com.pixelcan.inkpageindicator.InkPageIndicator; 

import hpbyp.ir.app.hojre.fragment.slider.SliderPagerAdapter; 
import hpbyp.ir.app.hojre.utiles.G; 
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; 

/** 
* An {@link Activity} which handles a broadcast of a new tag that the device just discovered. 
*/ 
public class ActivityLoadDataFromNFC extends AppCompatActivity { 
    @Override 
    protected void attachBaseContext(Context newBase) { 
     super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_load_data_from_nfc); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     mAdapter = NfcAdapter.getDefaultAdapter(this); 
     if (mAdapter == null) { 
      //nfc not support your device. 
      return; 
     } 
     mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
       getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    } 

    NfcAdapter mAdapter; 
    PendingIntent mPendingIntent; 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mAdapter != null) { 
      mAdapter.disableForegroundDispatch(this); 
     } 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     GetDataFromTag(tag, intent); 

    } 

    private void GetDataFromTag(Tag tag, Intent intent) { 
     Ndef ndef = Ndef.get(tag); 
     try { 
      ndef.connect(); 
//   txtType.setText(ndef.getType().toString()); 
//   txtSize.setText(String.valueOf(ndef.getMaxSize())); 
//   txtWrite.setText(ndef.isWritable() ? "True" : "False"); 
      Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 

      if (messages != null) { 
       NdefMessage[] ndefMessages = new NdefMessage[messages.length]; 
       for (int i = 0; i < messages.length; i++) { 
        ndefMessages[i] = (NdefMessage) messages[i]; 
       } 
       NdefRecord record = ndefMessages[0].getRecords()[0]; 

       byte[] payload = record.getPayload(); 
       String text = new String(payload); 
       Log.e("tag", "vahid" + text); 
       ndef.close(); 

      } 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Cannot Read From Tag.", Toast.LENGTH_LONG).show(); 
     } 
    } 

}