Android POS开发

引言:
有Landi,Newland,银联等智能pos机开发,本文档以MagCard为例,其他相关操作调用类似,本文档仅供学习

详情:
Landi相关文档和newland相关文档,都以MagCard为例

流程:

Landi pos机MagCard调用流程:
1:导入landi提供的eptapilibrary.jar包

compile files('src/main/jniLibs/armeabi/eptapilibrary.jar')

2:初始化MagCardReader,通过调用getInstance方法

MagCardReader.getInstance().searchCard(listener);

3:在activity中使用范例

    MagCardReader.OnSearchListener listener = new MagCardReader.OnSearchListener() {
        @Override
        public void onCrash() {
        }
        @Override
        public void onFail(int code) {
            String errorMessage = getErrorDescription(code);
            showErrorMessage("ERROR ["+errorMessage+"]");
        }
        @Override
        public void onCardStriped(boolean[] hasTrack, String[] track) {
            StringBuilder infoBuilder = new StringBuilder();
            for(int i=0; i<3; i++) {
                infoBuilder.append("TRACK ");
                infoBuilder.append(i+1);
                infoBuilder.append(" - exist ? ");
                infoBuilder.append(hasTrack[i]);
                if(hasTrack[i]) {
                    infoBuilder.append(" [");
                    infoBuilder.append(track[i]);
                    infoBuilder.append("]");
                }

                infoBuilder.append("\n");

            }
            infoBuilder.append("\n\n");
            infoBuilder.deleteCharAt(infoBuilder.length()-1);
            displayMagCardInfo(infoBuilder.toString());
        }

        @Override
        public boolean checkValid(int[] trackStates, String[] track) {
            if(trackStates[1] != TRACK_STATE_NULL && trackStates[1]  != TRACK_STATE_OK) {
                return false;
            }
            if(track[1].length() < 21 || track[1].length() > 37) {
                return false;
            }
            if(track[1].indexOf('=') < 12) {
                return false;
            }

            if(trackStates[1] == TRACK_STATE_NULL && trackStates[2] == TRACK_STATE_NULL) {
                return false;
            }
            return true;
        };
        public String getErrorDescription(int code) {
            switch(code) {
            case ERROR_NODATA:
                return "no data";
            case ERROR_NEEDSTART:
                return "need restart search";//This error never happened
            case ERROR_INVALID:
                return "has invalid track";//
            }
            return "unknown error - "+code;
        }
    };

public void searchCard() {
        try {
            MagCardReader.getInstance().searchCard(listener);
        } catch (RequestException e) {
            e.printStackTrace();
        }
    }

Newland POS机MagCard调用流程:
1:导入厂商提供的sdkserviceapi.jar包

2:manifest.xml配置service

 <service android:name=".service.MyService">
            <intent-filter>
                <action android:name="com.zacloud.device_service" />
            </intent-filter>
        </service>

3:理解设备接口服务IDeviceService,这是接口具体信息,只是看看而已

public interface IDeviceService extends IInterface {
    IBinder getBeeper() throws RemoteException;

    IBinder getLed() throws RemoteException;

    IBinder getSerialPort() throws RemoteException;

    IBinder getScanner(int var1) throws RemoteException;

    IBinder getMagCardReader() throws RemoteException;

    IBinder getInsertCardReader() throws RemoteException;

    IBinder getRFCardReader() throws RemoteException;

    IBinder getPinpad(int var1) throws RemoteException;

    IBinder getPrinter() throws RemoteException;

    IBinder getPBOC() throws RemoteException;

    IBinder getDeviceInfo() throws RemoteException;

    public abstract static class Stub extends Binder implements IDeviceService {
        private static final String DESCRIPTOR = "com.zacloud.deviceservice.aidl.IDeviceService";
        static final int TRANSACTION_getBeeper = 1;
        static final int TRANSACTION_getLed = 2;
        static final int TRANSACTION_getSerialPort = 3;
        static final int TRANSACTION_getScanner = 4;
        static final int TRANSACTION_getMagCardReader = 5;
        static final int TRANSACTION_getInsertCardReader = 6;
        static final int TRANSACTION_getRFCardReader = 7;
        static final int TRANSACTION_getPinpad = 8;
        static final int TRANSACTION_getPrinter = 9;
        static final int TRANSACTION_getPBOC = 10;
        static final int TRANSACTION_getDeviceInfo = 11;

        public Stub() {
            this.attachInterface(this, "com.zacloud.deviceservice.aidl.IDeviceService");
        }

        public static IDeviceService asInterface(IBinder obj) {
            if (obj == null) {
                return null;
            } else {
                IInterface iin = obj.queryLocalInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                return (IDeviceService)(iin != null && iin instanceof IDeviceService ? (IDeviceService)iin : new IDeviceService.Stub.Proxy(obj));
            }
        }

        public IBinder asBinder() {
            return this;
        }

        public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
            IBinder _result;
            IBinder _result;
            int _arg0;
            switch(code) {
            case 1:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getBeeper();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 2:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getLed();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 3:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getSerialPort();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 4:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _arg0 = data.readInt();
                _result = this.getScanner(_arg0);
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 5:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getMagCardReader();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 6:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getInsertCardReader();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 7:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getRFCardReader();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 8:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _arg0 = data.readInt();
                _result = this.getPinpad(_arg0);
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 9:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getPrinter();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 10:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getPBOC();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 11:
                data.enforceInterface("com.zacloud.deviceservice.aidl.IDeviceService");
                _result = this.getDeviceInfo();
                reply.writeNoException();
                reply.writeStrongBinder(_result);
                return true;
            case 1598968902:
                reply.writeString("com.zacloud.deviceservice.aidl.IDeviceService");
                return true;
            default:
                return super.onTransact(code, data, reply, flags);
            }
        }

        private static class Proxy implements IDeviceService {
            private IBinder mRemote;

            Proxy(IBinder remote) {
                this.mRemote = remote;
            }

            public IBinder asBinder() {
                return this.mRemote;
            }

            public String getInterfaceDescriptor() {
                return "com.zacloud.deviceservice.aidl.IDeviceService";
            }

            public IBinder getBeeper() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(1, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getLed() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(2, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getSerialPort() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(3, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getScanner(int cameraId) throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    _data.writeInt(cameraId);
                    this.mRemote.transact(4, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getMagCardReader() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(5, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getInsertCardReader() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(6, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getRFCardReader() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(7, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getPinpad(int kapId) throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    _data.writeInt(kapId);
                    this.mRemote.transact(8, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getPrinter() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(9, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getPBOC() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(10, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }

            public IBinder getDeviceInfo() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();

                IBinder _result;
                try {
                    _data.writeInterfaceToken("com.zacloud.deviceservice.aidl.IDeviceService");
                    this.mRemote.transact(11, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readStrongBinder();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }

                return _result;
            }
        }
    }
}

4:在MyService中绑定和解绑IDeviceService

 private  static IDeviceService deviceService;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            deviceService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            deviceService = IDeviceService.Stub.asInterface(service);
        }
    };

    public void connectDevice(Context paramContext)
            throws Exception {
        if (deviceService == null) {
            Intent intent = new Intent();
            intent.setPackage("com.zacloud.deviceservice");
            intent.setAction("com.zacloud.device_service");
            bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        } else {
        }
    }

    private void  disconnectDevice() {
        if (deviceService != null) {
            try {
                unbindService(serviceConnection);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                deviceService = null;
            }
        } else {
        }
    }

    public static IDeviceService getDeviceService() {
        return deviceService;
    }

    public void onCreate() {
        super.onCreate();
        try {
            connectDevice(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onDestroy() {
        super.onDestroy();
        disconnectDevice();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public boolean onUnbind(Intent paramIntent) {
        disconnectDevice();
        return false;
    }

5:在MainActivity onCreate()启动MyService

 startService(new Intent(this, MyService.class));

6:在需求activity中初始化IMagCardReader

IMagCardReader magCardReader =IMagCardReader.Stub.asInterface(MyService.getDeviceService().getMagCardReader());

7:调用searchCard()方法,实现MagCardListener

new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    magCardReader.searchCard(60, new MagCardListener.Stub() {
                        @Override
                        public void onTimeout() throws RemoteException {
                        }

                        @Override
                        public void onSuccess(final Bundle bundle) throws RemoteException {
                            Log.i("magic", "卡号:" + bundle.getString("PAN"));
                            Log.i("magic", "TRACK1:" + bundle.getString("TRACK1"));
                            Log.i("magic", "TRACK2:" + bundle.getString("TRACK2"));
                            Log.i("magic", "TRACK3:" + bundle.getString("TRACK3"));
                            Log.i("magic", "SERVICE_CODE:" + bundle.getString("SERVICE_CODE"));
                            Log.i("magic", "EXPIRED_DATE:" + bundle.getString("EXPIRED_DATE"));
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    pan = bundle.get("PAN").toString();
                                    if (pan != null) {
                                        numEt.setText(pan);
                                    }
                                }
                            });
                        }

                        @Override
                        public void onError(int errorCode) throws RemoteException {
                            switch (errorCode) {
                                case 90:
                                    Log.i("magic", "刷卡失败");
                                    break;
                                case 99:
                                    Log.i("magic", "设备服务异常");
                                    break;
                                case 100:
                                    Log.i("magic", "请求异常");
                                    break;
                                default:
                                    Log.i("magic", "请求异常");
                                    break;
                            }
                        }
                    });
                } catch (Exception e) {
                    Log.i("magic", "磁条卡寻卡异常");
                }
            }
        }).start();

本文仅供参考,谢谢

猜你喜欢

转载自blog.csdn.net/ware00/article/details/80508483
今日推荐