在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → OpenMobileApi demo

OpenMobileApi demo

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:10.58M
  • 下载次数:1
  • 浏览次数:19
  • 发布时间:2021-01-12
  • 实例类别:Android平台开发
  • 发 布 人:gwbby
  • 所需积分:2
 相关标签: openmobile demo API

实例介绍

【实例简介】获取reader、打开会话、打开通道 demo(需要SE硬件支持)
【实例截图】

【核心代码】

package com.gwb.oma;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.se.omapi.Channel;
import android.se.omapi.Reader;
import android.se.omapi.SEService;
import android.se.omapi.Session;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executor;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = MainActivity.class.getSimpleName();
    private Handler mHandler;
    private ArrayList<Session> mSessionList = null;
    private ArrayList<Channel> mChannelList = null;

    private final int MSG_SHOW_READERS = 0x01;
    private final int MSG_OPEN_SESSION = 0x02;
    private final int MSG_OPEN_CHANNEL = 0x03;

    private boolean mIsConnected = false;
    private SEService mSeService;
    Reader[] mReaders;
    @BindView(value = R.id.tv)
    TextView mTv;
    @BindView(value = R.id.btn_get_reader)
    Button mBtnGetReaders;
    @BindView(value = R.id.btn_close_sessions)
    Button mBtnCloseSessions;
    @BindView(value = R.id.btn_open_session)
    Button mBtnOpenSession;
    @BindView(value = R.id.btn_open_channel)
    Button mBtnOpenChannel;

    private void setAllBtn(boolean enable) {
        mBtnGetReaders.setEnabled(enable);
        mBtnCloseSessions.setEnabled(enable);
        mBtnOpenSession.setEnabled(enable);
        mBtnOpenChannel.setEnabled(enable);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mSessionList = new ArrayList<>();
        mChannelList = new ArrayList<>();
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                String str = "";
                switch (message.what) {
                    case MSG_SHOW_READERS:
                        for (Reader reader : mReaders) {
                            str  = reader.getName()   ":"   reader.toString()   "\r\n";
                        }
                        Log.d(TAG, str);
                        mTv.setText(str);
                        break;
                    case MSG_OPEN_SESSION:
                        for (Reader reader : mReaders) {
                            try {
                                Session session = reader.openSession();
                                mSessionList.add(session);
                                str  = reader.getName()   " openSession Success\r\n";
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            mTv.setText(str);
                        }
                        break;
                    case MSG_OPEN_CHANNEL:
                        for (Session session : mSessionList) {
                            for (int i = 0; i < 2; i  ) {
                                try {
                                    Channel channel = null;
                                    if (i == 0) {
                                        channel = session.openBasicChannel(null);
                                        if (channel != null) {
                                            str  = session.toString()   " openBasicChannel(null) Success\r\n";
                                            mChannelList.add(channel);
                                            byte[] response = channel.getSelectResponse();
                                            str  = "response: "   response.toString();
                                        }
                                    } else if (i == 1) {
                                        channel = session.openLogicalChannel(null);
                                        if (channel != null) {
                                            str  = session.toString()   " openLogicalChannel(null) Success\r\n";
                                            mChannelList.add(channel);
                                            byte[] response = channel.getSelectResponse();
                                            str  = "response: "   response.toString();
                                        }
                                    }

                                } catch (IOException | SecurityException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        mTv.setText(str);
                        break;
                    default:
                        break;
                }

                return false;
            }
        });

        mIsConnected = false;
        SeConnectedListener mListener = new SeConnectedListener();
        mSeService = new SEService(this, new Executor() {
            @Override
            public void execute(Runnable runnable) {
                runnable.run();
            }
        }, mListener);
        setAllBtn(false);
    }

    @Override
    @OnClick({R.id.btn_get_reader, R.id.btn_close_sessions, R.id.btn_open_session, R.id.btn_open_channel})
    public void onClick(View view) {
        if (!mIsConnected) {
            return;
        }
        Message msg = Message.obtain();
        switch (view.getId()) {
            case R.id.btn_get_reader:
                mReaders = mSeService.getReaders();
                Log.d(TAG, "readers:"   mReaders.toString());
                msg.what = MSG_SHOW_READERS;
                mHandler.sendMessage(msg);
                break;
            case R.id.btn_close_sessions:
                for (Reader reader : mReaders) {
                    reader.closeSessions();
                }
                break;
            case R.id.btn_open_session:
                msg.what = MSG_OPEN_SESSION;
                mHandler.sendMessage(msg);
                break;
            case R.id.btn_open_channel:
                msg.what = MSG_OPEN_CHANNEL;
                mHandler.sendMessage(msg);
                break;
            default:
                return;
        }
    }

    private class SeConnectedListener implements SEService.OnConnectedListener {

        @Override
        public void onConnected() {
            Log.d(TAG, "SeConnectedListener:onConnected");
            mIsConnected = true;
            setAllBtn(true);
        }
    }
}

标签: openmobile demo API

实例下载地址

OpenMobileApi demo

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警