nfc自动响应(回调)及如何自定义封装nfc功能

例子只对cpu卡操作,若有需要可在mTechLists 添加,并在nfc贴卡自动响应部分进行操作处理



public class NfcUtil {
    String TAG = "EtcModuleOperat";
    //cpu卡的操作

    private NfcAdapter mAdapter;
    private PendingIntent mPendingIntent;
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private Activity activity;

    private IsoDep isoDep = null;
    private boolean dsrcState = false;

    public NfcUtil(Activity activity) {
        this.activity = activity;
        mAdapter = NfcAdapter.getDefaultAdapter(activity);

        mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try
        {
            ndef.addDataType("*/*");
        }
        catch (IntentFilter.MalformedMimeTypeException e)
        {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[]{ndef,};
        //此处只对IsoDep 即cpu卡操作
        mTechLists = new String[][]{
                new String[]{IsoDep.class.getName()}};
    }


    /**
     * 通读执行发送随机数来判断是否连接
     * 判断nfc isoDep 是否连接
     *
     * @return 0 发送和返回的数据正常,1 发送成功,返回失败/无返回/未插ic卡 , -1 发送失败
     */
    public int isConnect() {
        if (isoDep != null && isoDep.isConnected()){
            Log.e("tag","获取随机码:" + "0084000008");
            String[] send = {"0084000008"};
            int result = dsrcCosCommond(send);//2 获取随机码
            Log.e("tag","  响应报文:" + (send[0]));
            if (result == 0) {
                Log.e(TAG, ": yes");
                return result;
            }
        }
        Log.e(TAG, ": no" );
        return 1;
    }

    /**
     * 执行cpu指令,成功返回 0,nfc功能未打开返回 -1, isoDep 断开(不存在) -2 ,nfc执行异常返回 -100,此处只执行命令
     *
     * @param commonds
     * @return
     */
    public int dsrcCosCommond(String[] commonds) {
        byte[] send, result;
        if (!dsrcState) {
            return -1;
        }
        if (isoDep == null || !isoDep.isConnected()) {
            return -2;
        }
        for (int i = 0; i < commonds.length; i++) {
            send = HexUtil.HexStringToByteArray(commonds[i]);
            try {
                result = isoDep.transceive(send);
                commonds[i] = HexUtil.ByteArrayToHexString(result);
            } catch (IOException e) {
                e.printStackTrace();
                return -100;
            }
        }
        return 0;
    }

    //dsrc部分为对nfc 的 cpu卡操作部分

    /**
     * 透传打开
     *
     * @return
     */
    public int dsrcOpen() {
        dsrcState = true;
        return 0;
    }

    /**
     * 关闭透传
     *
     * @return
     */
    public int dsrcClose() {
        dsrcState = false;
        return 0;
    }

    /**
     *初始化 进行nfc自动响应,贴卡时nfc自动连接isodep
     * @return 0:成功,非0:失败
     */
    public int initSdk() {

        Bundle extras = new Bundle();
        extras.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY,5000);

        int flags = NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
        flags |= NfcAdapter.FLAG_READER_NFC_A;
        mAdapter.enableForegroundDispatch(activity, mPendingIntent, mFilters, mTechLists);
        mAdapter.enableReaderMode(activity, new NfcAdapter.ReaderCallback() {
            @Override
            public void onTagDiscovered(Tag tag) {
                isoDep = IsoDep.get(tag);
                try {
                    isoDep.connect();
                    //isoDep.setTimeout(500);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        },flags,extras);
    }

    /**
     * 关闭
     *
     * @return
     */
    public void releaseSdk() {
        mAdapter.disableForegroundDispatch(activity);
    }

}

你可能感兴趣的:(Android)