• NFC读写电子便签总结


    编写NFC程序的基本步骤

    1)设置权限,限制Android版本、安装的设备:

    1
    2
    3
    4
    <uses-sdk android:minSdkVersion="14"/>
    <uses-permission android:name="android.permission.NFC" />
    <!-- 要求当前设备必须要有NFC芯片 -->
    <uses-feature android:name="android.hardware.nfc" android:required="true"

    public class NfcManager {

    private NfcAdapter mNfcAdapter;
    private boolean mEnabled;
    private WeakReference<Activity> mActivityWeakReference;
    private MessageListener mMessageListener;
    private NfcAdapter.ReaderCallback mReaderCallback = new NfcAdapter.ReaderCallback() {
    @Override
    public void onTagDiscovered(Tag tag) {
    processTag(tag);
    }
    };

    private NfcManager(NfcAdapter nfcAdapter) {
    mNfcAdapter = nfcAdapter;
    }
    public static NfcManager get(Context context) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
    if (nfcAdapter != null) {
    return new NfcManager(nfcAdapter);
    } else {
    return null;
    }
    }
    /**
    * 设备是否支持nfc
    */
    public static boolean testSupportNfc(Context context) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
    if (nfcAdapter == null) {
    T.showShort(InspectionApplication_.getApplication(), Tools.getStringValue(
    InspectionApplication_.getApplication(), R.string.dialog_not_support_NFC));
    return false;
    }
    if (!nfcAdapter.isEnabled()) {
    T.showShort(InspectionApplication_.getApplication(), Tools.getStringValue(
    InspectionApplication_.getApplication(), R.string.please_open_nfc));
    context.startActivity(new Intent("android.settings.NFC_SETTINGS"));
    return false;
    }
    return true;
    }
    public synchronized boolean enable(Activity activity, MessageListener listener) {
    if (mEnabled) {
    return true;
    }
    if (!mNfcAdapter.isEnabled()) {
    return false;
    }
    mNfcAdapter.enableReaderMode(activity, mReaderCallback, NfcAdapter.FLAG_READER_NFC_A, null);
    mActivityWeakReference = new WeakReference<>(activity);
    mMessageListener = listener;
    mEnabled = true;
    return true;
    }
    public synchronized boolean disable(Activity activity) {
    if (!mEnabled) {
    return true;
    }
    if (activity != mActivityWeakReference.get()) {
    return false;
    }
    mNfcAdapter.disableReaderMode(activity);
    mActivityWeakReference.clear();
    mActivityWeakReference = null;
    mMessageListener = null;
    mEnabled = false;
    return true;
    }
    private void processTag(Tag tag) {
    try {
    List<String> techs = Arrays.asList(tag.getTechList());
    if (!techs.contains("android.nfc.tech.Ndef")) {
    Log.e("NDEF not included");
    return;
    }
    String msg = "BEGIN:VCARD " + "VERSION:2.1 " + "中国湖北省武汉市 "
    + "武汉大学计算机学院 " + "END:ABCDE";
    byte[] textBytes = msg.getBytes();
    //将字节数组封装到一个NdefRecord实例中去
    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
    NdefRecord.RTD_TEXT, new byte[] {}, textBytes);
    //新建NdefRecord数组,本例中数组只有一个元素
    NdefRecord[] records2 = {textRecord};
    //新建一个NdefMessage实例
    NdefMessage message = new NdefMessage(records2);

    Ndef ndef2 = Ndef.get(tag);
    // 打开连接
    ndef2.connect();
    // 写入NDEF信息
    ndef2.writeNdefMessage(message);
    // 关闭连接
    ndef2.close();
    Ndef ndef = Ndef.get(tag);
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    if (records == null || records.length == 0) {
    KLog.e("NDEF record not found");
    return;
    }
    NdefRecord record = records[0];
    final String textType = "text/plain";
    if (!record.toMimeType().equals(textType)) {
    Log.e("NDEF record is not " + textType);
    return;
    }
    if (mMessageListener == null) {
    return;
    }
    byte[] payload = record.getPayload();
    byte length = payload[0];
    int start = 1 + length;
    mMessageListener
    .onMessage(new String(Arrays.copyOfRange(payload, start, payload.length)));
    } catch (Exception e) {
    Log.e(e);
    }
    }
    public interface MessageListener {
    void onMessage(String message);
    }

    }
    /////////////////////////////////

    NFCSActivity 

    onResume(){

       closeToNfc()

    }

    public void closeToNfc() {
    if (!NfcManager.testSupportNfc(mContext)) {
    return;
    }
    final NfcManager manager = NfcManager.get(mContext);
    if (manager == null) {
    return;
    }
    final DataWrapper<AlertDialog> dataWrapper = new DataWrapper<>();
    final NfcManager.MessageListener listener = new NfcManager.MessageListener() {
    @Override
    public void onMessage(final String message) {
    Message msg=new Message();
    msg.what=1;
    msg.obj=message;
    handler.sendMessage(msg);

    }
    };
    manager.enable(mContext, listener);

    }
    ///////////////////////////////
    LinearLayout rootlayout = new LinearLayout(mContext);
    rootlayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    UnitUtils.dip2px(mContext, 48)));
    rootlayout.setOrientation(LinearLayout.HORIZONTAL);
    TextView text_Name = new TextView(mContext);
    LinearLayout.LayoutParams text_Name_Params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
    text_Name.setLayoutParams(text_Name_Params);
    text_Name.setId(View.generateViewId());
    text_Name.setText(R.string.activity_nfc_id);
    rootlayout.addView(text_Name);

    EditText et_name = new EditText(mContext);
    LinearLayout.LayoutParams et_Name_Params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.MATCH_PARENT);
    et_name.setLayoutParams(et_Name_Params);
    et_name.setId(View.generateViewId());
    et_name.setMaxLines(1);
    if(msg.obj!=null){
    et_name.setText(msg.obj.toString());
    mNFCs.add(msg.obj.toString());
    }
    et_name.setSelection(et_name.getText().length());
    rootlayout.addView(et_name);
    linear.addView(rootlayout);


    }

    }
    };

  • 相关阅读:
    vim 打开了没有权限写入的文件,怎么才能不必重新编辑
    Linux 关闭终端响铃的特性
    Python 的 print 函数
    C 语言标准库中的qsort函数使用
    java知识学习14-面向对象
    java知识学习13-方法
    java知识学习12-数组
    mac格式化重装系统
    java知识学习11- IDEA
    java知识学习10-跳转控制语句(break/continue)、循环嵌套、Random
  • 原文地址:https://www.cnblogs.com/spps/p/8995291.html
Copyright © 2020-2023  润新知