android与sp通讯

来源:互联网 发布:apriori算法思想 编辑:程序博客网 时间:2024/04/30 02:08

一。读文件节点

private static final byte[] GET_SP_TIGGER_TPYE = new byte[] {0x02,0x00,0x00,0x04,0x6c,0x05,0x00,0x00,0x6d,0x40};//获取触发状态private static final byte[] GET_SP_ACTIVATE_TPYE = new byte[] {0x02,0x00,0x00,0x06,(byte)0xA1,0x13,0x00,0x02,0x64,0x00,(byte)0xD2,0x40};//获取激活状态private static final byte[] GET_SP_SN_TPYE = new byte[] {0x02,0x00,0x00,0x04,0x6C,0x01,0x00,0x00,0x69,0x40};//获取SN状态
byte[] spDate = getSPDate(GET_SP_TIGGER_TPYE, 15);//读取sp的触发状态
byte[] spSNnumbersAll = getSPDate(GET_SP_SN_TPYE, 112);
byte[] activate = getSPDate(GET_SP_ACTIVATE_TPYE, 11);


private byte[] getSPDate(byte[] inputDate,int resultHope){   //sp通讯的方法 String res=""; FileOutputStream fos = null; FileInputStream fin = null; byte [] buffer = new byte[200]; int result = -1; int read_num = 0; File file = new File("/dev/kl81dev0");//驱动暴露出来的共享内存区域        try{              fos = new FileOutputStream(file);//将字节数据写出到文件              fin = new FileInputStream(file);//读取本地文件中的字节数据              while ((result == -1)&&(read_num < 8)) {//未读到指定长度的信息时,循环8次读取           fos.write(inputDate,0, inputDate.length);//在指定的区域写入驱动所给的信息(相当于提醒驱动我要得到什么信息)           //int length = fin.available();           result = fin.read(buffer,0, buffer.length);//在指定的区域读入驱动所反馈的信息(刚刚提醒驱动后,驱动所写入的信息)           if(result != resultHope){              read_num ++;              result = -1;              Thread.sleep(8);//为了防止大量的指令冲突,延时8毫秒,确保指令可以正常读取              buffer = new byte[200];           }else{              if(buffer[4] == inputDate[4] && buffer[5] == inputDate[5]){                  break;              }else{                 read_num ++;                 result = -1;                 Thread.sleep(8);//为了防止大量的指令冲突,延时8毫秒,确保指令可以正常读取                 buffer = new byte[200];              }           }         }      Log.v(TAG,"getSPDate  buffer : " + bytesToHexString(buffer) +  " , result : " + result +"  resultHope  = "+                         resultHope + " order check  "+ (buffer[4] == inputDate[4] && buffer[5] == inputDate[5]));     }catch(Exception e){          e.printStackTrace();     }finally{         if (fos != null) {                   try {                       fos.close();//关闭流                   }catch (Exception e){                       Log.d(TAG,"e "+e);                   }                }                if(fin != null) {                   try {                       fin.close();//关闭流                   }catch (Exception e){                       Log.d(TAG,"e "+e);                   }                }          }          return buffer;}

二。

byte[] LicImei = readNvramValue();String res = bytesToHexString1(LicImei); //byte数组转化为string字符串   

public static String bytesToHexString1(byte[] bytes) {        if (bytes == null) return null;    StringBuilder ret = new StringBuilder(bytes.length);    for (int i = 0 ; i < bytes.length ; i++) {        int b;
//    b = 0x0f & (bytes[i] >> 4);                               一个byte有8位,理论上讲值可能为0-255//  ret.append("0123456789abcdef".charAt(b));
        b = 0x0f & bytes[i]; ret.append("0123456789abcdef".charAt(b)); } return ret.toString().toUpperCase();}

private byte[] readNvramValue() {    int barcode_lid = 59;    byte array[]=new byte[17];  //读取17位byte数据       try{        IBinder binder= ServiceManager.getService("NvRAMAgent");        NvRAMAgent agent = NvRAMAgent.Stub.asInterface (binder);        byte[] buff = null;        try{            buff = agent.readFile(barcode_lid);        }catch(Exception e){            e.printStackTrace();        }        if(buff.length == 2048) {            for(int i=0;i<17;i++){                array[i]=buff[1280+i];            }        }    }catch(Exception e){        e.printStackTrace();    }    return array;}
NvRAMAgent.java

package com.android.server;import android.os.Binder;import android.os.IBinder;import android.os.IInterface;import android.os.Parcel;import android.os.RemoteException;public interface NvRAMAgent extends IInterface{    /** Local-side IPC implementation stub class. */    public static abstract class Stub extends Binder implements NvRAMAgent    {        private static final String DESCRIPTOR = "NvRAMAgent";        /** Construct the stub at attach it to the interface. */        public Stub()        {            this.attachInterface(this, DESCRIPTOR);        }        /**         * Cast an IBinder object into an NvRAMAgent interface,         * generating a proxy if needed.         */        public static NvRAMAgent asInterface(IBinder obj)        {            if ((obj==null)) {                return null;            }            IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);            if (((iin!=null)&&(iin instanceof NvRAMAgent))) {                return ((NvRAMAgent)iin);            }            return new Stub.Proxy(obj);        }        public IBinder asBinder()        {            return this;        }        public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException        {            switch (code)            {                case INTERFACE_TRANSACTION:                {                    reply.writeString(DESCRIPTOR);                    return true;                }                case TRANSACTION_READFILE:                {                    data.enforceInterface(DESCRIPTOR);                    int _arg0;                    _arg0 = data.readInt();                    byte[] _result = this.readFile(_arg0);                    reply.writeNoException();                    reply.writeByteArray(_result);                    return true;                }                case TRANSACTION_WRITEFILE:                {                    data.enforceInterface(DESCRIPTOR);                    int _arg0;                    _arg0 = data.readInt();                    byte[] _arg1;                    _arg1 = data.createByteArray();                    int _result = this.writeFile(_arg0, _arg1);                    reply.writeNoException();                    reply.writeInt(_result);                    return true;                }                default:                {                    break;                }            }            return super.onTransact(code, data, reply, flags);        }        private static class Proxy implements NvRAMAgent        {            private IBinder mRemote;            Proxy(IBinder remote)            {                mRemote = remote;            }            public IBinder asBinder()            {                return mRemote;            }            public String getInterfaceDescriptor()            {                return DESCRIPTOR;            }            public byte[] readFile(int file_lid) throws RemoteException            {                Parcel _data = Parcel.obtain();                Parcel _reply = Parcel.obtain();                byte[] _result;                try {                    _data.writeInterfaceToken(DESCRIPTOR);                    _data.writeInt(file_lid);                    mRemote.transact(Stub.TRANSACTION_READFILE, _data, _reply, 0);                    _reply.readException();                    _result = _reply.createByteArray();                }                finally {                    _reply.recycle();                    _data.recycle();                }                return _result;            }            public int writeFile(int file_lid, byte[] buff) throws RemoteException            {                Parcel _data = Parcel.obtain();                Parcel _reply = Parcel.obtain();                int _result;                try {                    _data.writeInterfaceToken(DESCRIPTOR);                    _data.writeInt(file_lid);                    _data.writeByteArray(buff);                    mRemote.transact(Stub.TRANSACTION_WRITEFILE, _data, _reply, 0);                    _reply.readException();                    _result = _reply.readInt();                }                finally {                    _reply.recycle();                    _data.recycle();                }                return _result;            }        }        static final int TRANSACTION_READFILE = (IBinder.FIRST_CALL_TRANSACTION + 0);        static final int TRANSACTION_WRITEFILE = (IBinder.FIRST_CALL_TRANSACTION + 1);    }    public byte[] readFile(int file_lid) throws RemoteException;    public int writeFile(int file_lid, byte[] buff) throws RemoteException;}




原创粉丝点击