package com.rehome.sgbaxj.activity;

import android.view.View;
import android.widget.TextView;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.base.BaseActivity3;
import com.rinsun.mtdriver.MTnativeInterface;

public class NfcinfoActivity extends BaseActivity3 {


    TextView tvNfc;
    MTnativeInterface mt = MTnativeInterface.getInstance();//获取传感器调用接口


    @Override
    public int getLayoutId() {
        return R.layout.activity_nfcinfo;
    }

    @Override
    public void initView() {
        tvNfc = findViewById(R.id.tv_nfc);
        initToolbar("NFC信息", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //使用完一定记得关闭电源
        mt.RFIDclose();
        mt.IRTclose();
        mt.VIBclose();
        mt.RPMclose();
        initNFC();
        //回调函数（自动返回数据）。CMDtype:数据类型，参数说明见“说明文档.txt”
        mt.setOnReceiveListener(new MTnativeInterface.OnReceiveListener() {
            @Override
            public void onShowMessage(byte[] recBuff, int CMDtype, int tag) {
                /* 根据CMDtype区分数据类型，普通测量值/频谱数据/配置数据。如果10秒没读到数据会返回超时提示并关闭电源 */
                System.out.println("-------------");
                System.out.println(new String(recBuff) + "/" + CMDtype + "/" + tag);
                if (CMDtype == MTnativeInterface.CMD_RFID_R) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (recBuff != null) {
                                //RFID数据
                                String recv = new String(recBuff);
                                if(tvNfc!=null){
                                    tvNfc.setText(recv);
                                }
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void initData() {
        readRFID();
    }

    public void readRFID() {
        /*========读卡示例，参数说明见“说明文档.txt”============================================================*/
        mt.RFIDopen(MTnativeInterface.CARD_14443);//开启电源。(不可与振动同时打开,它们共用硬件资源)
        mt.RFIDread(-1, "");
    }

    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(tvNfc!=null){
                    tvNfc.setText(result);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        //使用完一定记得关闭电源
        mt.RFIDclose();
        mt.IRTclose();
        mt.VIBclose();
        mt.RPMclose();
        super.onDestroy();
    }
}
