1

我有一个程序可以将数据从 android 手机发送到移动打印蓝牙。我的代码有问题。发生运行时错误。我无法分析它。请帮忙。非常感谢

这是我的代码示例:

   private static final String TAG = "Test";
    static final private int PrintText_ID = Menu.FIRST + 1;
    static final private int PrintBarcode_ID = Menu.FIRST + 2;
    static final private int LineFeed_ID = Menu.FIRST + 3;
    static final private int SetCharacterSet_ID = Menu.FIRST + 4;
    static final private int Connect_ID = Menu.FIRST + 5;
    static final private int Disconnect_ID = Menu.FIRST + 6;    
    static final private int GetStatus_ID = Menu.FIRST + 7;
    static final private int GetPowerStatus_ID = Menu.FIRST + 8;
    static final private int Directio_ID = Menu.FIRST + 9;
    static final private int MsrTrack1_ID = Menu.FIRST + 10;
    static final private int MsrTrack2_ID = Menu.FIRST + 11;
    static final private int MsrTrack3_ID = Menu.FIRST + 12;
    static final private int MsrTrack12_ID = Menu.FIRST + 13;
    static final private int MsrTrack23_ID = Menu.FIRST + 14;
    static final private int PrintText1K_ID = Menu.FIRST + 15; 

    private ZQPrinter PrinterService = null;   
    private boolean conn = false;

    private byte dataBar[] ={0x38, 0x38, 0x30, 0x31, 0x31, 0x31, 0x35, 0x31, 0x31};    //"JAN13(EAN) 8801115114031"
    private byte dataBar2[] ={0x38, 0x38, 0x30, 0x31, 0x31};
    private byte dataBar3[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37};
    String PrintTextSample="ABCDEFGabcdefg123456!@#$%^\r\n";


    public ZQExample()
    {
        Log.e(TAG, "+++ ON Contructor +++"); 

    }


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        CheckGC("onCreate_Start" );
        Log.e(TAG, "+++ ON CREATE +++");        
        setContentView(R.layout.main);
        TextView t = (TextView) findViewById(R.id.text);        
        registerForContextMenu(t);

        PrinterService = new ZQPrinter();
        CheckGC("onCreate_End" );
    }

    @Override
    public void onStart() {
        super.onStart();
        CheckGC("onStart_Start" );
        TextView t = (TextView) findViewById(R.id.text); 
        t.setText("");              
        t.append("AM-3XM EXAMPLE\n\n");          
        t.append("do long press on touchscreen\n");
        Log.e(TAG, "++ ON START ++");
        CheckGC("onStart_End" );
    }

    @Override
    public synchronized void onResume() {
        super.onResume();
        Log.e(TAG, "+ ON RESUME +");
        CheckGC("onResume_End" );
    }    

    @Override
    public synchronized void onPause() {
        super.onPause();
        Log.e(TAG, "- ON PAUSE -");
        CheckGC("onPause_End" );
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, "-- ON STOP --");
        CheckGC("onStop_End" );
        // defense code for HTC Desire because of reconnect' fail       
        // Runtime.getRuntime().exit(0);       
    }

    @Override
    public void onDestroy() {
         Log.e(TAG, "--- ON DESTROY ---");  
        super.onDestroy();
        if(PrinterService != null)
        {   
         PrinterService.Disconnect(); 
         PrinterService = null;
        }
        CheckGC("onDestroy_End" );
    }   

    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        CheckGC("onCreateContextMenu_Start" );
        if (conn == false) {
            menu.add(0, Connect_ID, 0, "Connect");
        } 
        else {
            menu.add(0, Disconnect_ID, 0,  "Disconnect");
            menu.add(0, PrintText_ID, 0,  "PrintText"); 
            menu.add(0, PrintText1K_ID, 0,  "PrintText1K");
            menu.add(0, PrintBarcode_ID, 0,  "PrintBarcode");
            menu.add(0, LineFeed_ID , 0,  "LineFeed");
            menu.add(0, GetStatus_ID , 0,  "GetStatus");
            menu.add(0, GetPowerStatus_ID, 0,  "GetPowerStatus");
            menu.add(0, Directio_ID, 0,  "Directio");
            menu.add(0, MsrTrack1_ID, 0,  "MsrTrack1");
            menu.add(0, MsrTrack2_ID, 0,  "MsrTrack2");
            menu.add(0, MsrTrack3_ID, 0,  "MsrTrack3");
            menu.add(0, MsrTrack12_ID, 0,  "MsrTrack12");
            menu.add(0, MsrTrack23_ID, 0,  "MsrTrack23");
            menu.add(0, SetCharacterSet_ID, 0,  "SetCharacterSet");
        }
        CheckGC("onCreateContextMenu_End" );
    }

    public boolean onContextItemSelected(MenuItem item) {
        int returevlaue=ZQPrinter.AB_SUCCESS;
        TextView t = (TextView) findViewById(R.id.text);   
        switch (item.getItemId()) {
        case Connect_ID:
            CheckGC("Connect_Start" );

            t.setText("");
            t.append("Connect...\n");
            int nRet = PrinterService.Connect("00:1F:B7:03:C8:05");
            if ( nRet == 0 ) {
                t.append("Success\n");
                conn = true;
            }
            else {
                Log.e("Test", String.valueOf(nRet));
                t.append("Fail\n\n"); 
                Toast.makeText(ZQExample.this,"check printer & bluetooth",Toast.LENGTH_SHORT).show();
                conn = false;               
            }
            t.append("do long press on touchscreen\n"); 
            CheckGC("Connect_End" );            
            //////
            return true; 
        case Disconnect_ID:
            CheckGC("Disconnect_Start" );
            t.setText("");          
            t.append("Disconnect...\n");        
            PrinterService.Disconnect(); 
            PrinterService = null;
            t.append("Success\n\n");            
            t.append("do long press on touchscreen\n"); 
            conn = false; 
            CheckGC("Disconnect_End" );
            return true;

        case PrintText_ID:
            CheckGC("PrintText_Start" );
            t.setText("");          
            t.append("PrintText...\n"); 
            //////
            System.setProperty("file.encoding", "gb2312");
            String str = "²âÊÔ´òÓ¡¼òÌå\r\n";
            PrinterService.PrintText(str, ZQPrinter.ALIGNMENT_LEFT, ZQPrinter.FT_DEFAULT, 0);
            //////
            System.setProperty("file.encoding", "big5");
            String str2 = "œyÔ‡´òÓ¡·±ów\r\n";
            PrinterService.PrintText(str2, ZQPrinter.ALIGNMENT_LEFT, ZQPrinter.FT_DEFAULT, 0);
            //////////
       returevlaue=PrinterService.GetStatus();    
        if(returevlaue==ZQPrinter.AB_SUCCESS){   
           for(int i=0 ; i<50;i++) 
           {       
               returevlaue=PrinterService.PrintText(PrintTextSample,
                        ZQPrinter.ALIGNMENT_LEFT, 
                        ZQPrinter.FT_DEFAULT, 
                        ZQPrinter.TS_0WIDTH | ZQPrinter.TS_0HEIGHT );
                    CheckGC("PrintText_Loop" );
               if(returevlaue!=ZQPrinter.AB_SUCCESS)
                {
                    break;
                }

           }
        }

        if(returevlaue==ZQPrinter.AB_SUCCESS){
            returevlaue=PrinterService.LineFeed(5);
        }

        returevlaue=PrinterService.GetStatus();

            if(returevlaue==ZQPrinter.AB_SUCCESS){
                t.append("Success\n\n");
            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
            }


            t.append("\n do long press on touchscreen\n");  
            CheckGC("PrintText_End" );
            return true;

        case PrintBarcode_ID :
            CheckGC("PrintBarcode_Start" );
            t.setText("");          
            t.append("PrintBarcode...\n");  
             returevlaue=PrinterService.GetStatus();

            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.PrintBarcode(dataBar ,dataBar.length ,  ZQPrinter.BCS_JAN13, 50, 3,  ZQPrinter.ALIGNMENT_LEFT,  ZQPrinter.BC_TEXT_BELOW);
            }
            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.LineFeed(1);
            }

            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.PrintBarcode(dataBar2,dataBar2.length,  ZQPrinter.BCS_JAN8, 50, 3,  ZQPrinter.ALIGNMENT_LEFT,  ZQPrinter.BC_TEXT_BELOW);
            }
            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.LineFeed(1);
            }

            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.PrintBarcode(dataBar3 ,dataBar3.length ,  ZQPrinter.BCS_Code39, 50, 3,  ZQPrinter.ALIGNMENT_LEFT,  ZQPrinter.BC_TEXT_BELOW);
            }
            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.LineFeed(1);
            }

            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.PrintBarcode(dataBar3, dataBar3.length,  ZQPrinter.BCS_Code128, 50, 3,  ZQPrinter.ALIGNMENT_LEFT,  ZQPrinter.BC_TEXT_BELOW);
            }
            if(returevlaue==ZQPrinter.AB_SUCCESS){
             returevlaue=PrinterService.LineFeed(1);
            }
            returevlaue=PrinterService.GetStatus();

            if(returevlaue==ZQPrinter.AB_SUCCESS){ 
              t.append("Success\n\n");

            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
            }         
            t.append("\n do long press on touchscreen\n"); 
            CheckGC("PrintBarcode_End" );
            return true;      

        case LineFeed_ID :   
            CheckGC("LineFeed_Start" );
            t.setText("");          
            t.append("LineFeed...\n"); 
            returevlaue=PrinterService.GetStatus();

            if(returevlaue==ZQPrinter.AB_SUCCESS){ 
              returevlaue=PrinterService.LineFeed(5);
            }

            returevlaue=PrinterService.GetStatus();
            if(returevlaue==ZQPrinter.AB_SUCCESS){
                t.append("Success\n\n");
            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            }

            t.append("\n do long press on touchscreen\n");  
            CheckGC("LineFeed_End" );
            return true;            

        case GetStatus_ID:   
            CheckGC("GetStatus_Start" );
            t.setText("");          
            t.append("GetStatus...\n");             
            returevlaue=PrinterService.GetStatus();
            if(returevlaue==ZQPrinter.AB_SUCCESS){
                t.append("Success\n\n");
            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            }            
            t.append("\n do long press on touchscreen\n"); 
            CheckGC("GetStatus_End" );
            return true;

        case SetCharacterSet_ID:
            CheckGC("SetCharacterSet_Start" );
            t.setText("");          
            t.append("SetCharacterSet...\n");           
            returevlaue=PrinterService.SetCharacterSet(0);
            if(returevlaue==ZQPrinter.AB_SUCCESS){ //0:USA, 19:Euro
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="Data ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            }           
            t.append("\n do long press on touchscreen\n");  
            CheckGC("SetCharacterSet_End" );
            return true;       

        case GetPowerStatus_ID:  
            CheckGC("GetPowerStatus_Start" );
            t.setText("");          
            t.append("GetPowerStatus...\n");            
            returevlaue=PrinterService.GetPowerStatus();
            if(returevlaue==ZQPrinter.AB_SUCCESS){
                t.append("Success\n\n");
            }else if(returevlaue==ZQPrinter.PWR_HIGH){
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="BXL_PWR_HIGH ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;

            }else if(returevlaue==ZQPrinter.PWR_MIDDLE){
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="BXL_PWR_MIDDLE ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;

            }else if(returevlaue==ZQPrinter.PWR_LOW){
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="BXL_PWR_LOW ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;

            }else if(returevlaue==ZQPrinter.PWR_SMALL){
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="BXL_PWR_SMAL ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;

            }else if(returevlaue==ZQPrinter.PWR_NOT){
                t.append("Success\n\n");
                String tem_buffer=new String();
                tem_buffer="BXL_PWR_NOT ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;

            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            }           
            t.append("\n do long press on touchscreen\n");
            CheckGC("GetPowerStatus_End" );
            return true;

        case MsrTrack1_ID:
            CheckGC("MsrTrack1_Start" );
            t.setText("");          
            t.append("MsrTrack1...\n");  
            byte[] i1 = PrinterService.MsrTrack1();
            char temchar1;
            if(i1!=null){
                PrintOutput("MsrTrack1",i1);
                t.append("Success\n\n");
               //////////////////////////////////// 
                String tem_buffer=new String();
                tem_buffer="Data :("+i1.toString()+") ";
                for(int i=0;i<i1.length;i++)
                {
                    temchar1=(char)i1[i];
                    tem_buffer=tem_buffer+" "+temchar1;
                }
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
              ///////////////////////////////////////   
            }else{
                t.append("Fail\n\n");
            }       
            t.append("\n do long press on touchscreen\n");   
            i1=null;
            CheckGC("MsrTrack1_End" );
            return true;    

        case MsrTrack2_ID:
            CheckGC("MsrTrack2_Start" );
            t.setText("");          
            t.append("MsrTrack2...\n");  
            byte[] i2 = PrinterService.MsrTrack2();
            char temchar2;
            if(i2!=null){
                t.append("Success\n\n");
                PrintOutput("MsrTrack2",i2);
            ////////////////////////////////////    
                String tem_buffer=new String();
                tem_buffer="Data :("+i2.toString()+") ";
                for(int i=0;i<i2.length;i++)
                {
                     temchar2=(char)i2[i];
                     tem_buffer=tem_buffer+" "+temchar2;
                }
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
            ////////////////////////////////////
            }else{
                t.append("Fail\n\n");
            }        
            t.append("\n do long press on touchscreen\n"); 
            i2=null;
            CheckGC("MsrTrack1_End" );
            return true;      

        case MsrTrack3_ID: 
            CheckGC("MsrTrack1_End" );
            t.setText("");          
            t.append("MsrTrack3...\n");  
            byte[] i3 = PrinterService.MsrTrack3();
            char temchar3;
            if(i3!=null){
                 PrintOutput("MsrTrack3",i3);
                 t.append("Success\n\n");
                ////////////////////////////////////
                 String tem_buffer=new String();
                 tem_buffer="Data :("+i3.toString()+") ";
                 for(int i=0;i<i3.length;i++)
                 {
                     temchar3=(char)i3[i];
                     tem_buffer=tem_buffer+" "+temchar3;
                 }
                 t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                 tem_buffer=null;
                ////////////////////////////////////
            }else{
                t.append("Fail\n\n");
            }        
            t.append("\n do long press on touchscreen\n"); 
            i3=null;
            CheckGC("MsrTrack1_End" );
            return true;
        case MsrTrack12_ID: 
            CheckGC("MsrTrack12_End" );
            t.setText("");          
            t.append("MsrTrack12...\n");  
            byte[] i4 = PrinterService.MsrTrack12();
            char temchar4;
            if(i4!=null){
                 PrintOutput("MsrTrack12",i4);
                 t.append("Success\n\n");
                ////////////////////////////////////
                 String tem_buffer=new String();
                 tem_buffer="Data :("+i4.toString()+") ";
                 for(int i=0;i<i4.length;i++)
                 {
                     temchar4=(char)i4[i];
                     tem_buffer=tem_buffer+" "+temchar4;
                 }
                 t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                 tem_buffer=null;
                ////////////////////////////////////
            }else{
                t.append("Fail\n\n");
            }        
            t.append("\n do long press on touchscreen\n"); 
            i4=null;
            CheckGC("MsrTrack12_End" );
            return true;
        case MsrTrack23_ID: 
            CheckGC("MsrTrack23_End" );
            t.setText("");          
            t.append("MsrTrack23...\n");  
            byte[] i5 = PrinterService.MsrTrack3();
            char temchar5;
            if(i5!=null){
                 PrintOutput("MsrTrack23",i5);
                 t.append("Success\n\n");
                ////////////////////////////////////
                 String tem_buffer=new String();
                 tem_buffer="Data :("+i5.toString()+") ";
                 for(int i=0;i<i5.length;i++)
                 {
                     temchar5=(char)i5[i];
                     tem_buffer=tem_buffer+" "+temchar5;
                 }
                 t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                 tem_buffer=null;
                ////////////////////////////////////
            }else{
                t.append("Fail\n\n");
            }        
            t.append("\n do long press on touchscreen\n"); 
            i5=null;
            CheckGC("MsrTrack23_End" );
            return true;
        case Directio_ID: 
            CheckGC("Directio_Start" );
            t.setText("");          
            t.append("Directio...\n");  
            byte sendApdu[] = {0x10, 0x04, 0x01}; 
            byte[] readData = new byte[1];
            int[] readLength = new int[1];
            readLength[0]=readData.length;
            returevlaue=PrinterService.Directio(sendApdu,sendApdu.length,readData,readLength);
            if(returevlaue==ZQPrinter.AB_SUCCESS){
                t.append("Success\n\n");
             ////////////////////////////////////
             String tem_buffer=new String();
             tem_buffer="DataSize:("+readLength[0]+") ,Date : ";
             for(int i=0;i<readLength[0];i++)
             {
                 tem_buffer=tem_buffer+" "+readData[i];
             }
             t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
             tem_buffer=null;
             ////////////////////////////////////   
            }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
            }           
            t.append("\n do long press on touchscreen\n");
            CheckGC("Directio_End" );
            return true;
        case PrintText1K_ID: 
            CheckGC("PrintText1K_Start" );
             t.setText("");         
             t.append("PrintText1K...\n"); 

             String test=new String();  
             for(int i=0 ;i<120;i++)
             {
                 test+="ABCDEFGabcdefg123456!@#$%^ ";
             }

             returevlaue=PrinterService.PrintText(test, 
                        ZQPrinter.ALIGNMENT_LEFT, 
                        ZQPrinter.FT_DEFAULT, 
                        ZQPrinter.TS_0WIDTH | ZQPrinter.TS_0HEIGHT );


             if(returevlaue==ZQPrinter.AB_SUCCESS){
                returevlaue=PrinterService.LineFeed(5);
                t.append("Success\n\n");
             }else{
                t.append("Fail\n\n");
                String tem_buffer=new String();
                tem_buffer="ERROR ["+returevlaue+"]";
                t.append(tem_buffer.subSequence(0,tem_buffer.getBytes().length));
                tem_buffer=null;
             }

             test=null;
             t.append("\n do long press on touchscreen\n");  
             CheckGC("PrintText1K_End" );
             return true;

        }

        return super.onContextItemSelected(item);
    }

    void CheckGC(String FunctionName)
    {
        long VmfreeMemory =Runtime.getRuntime().freeMemory();
        long VmmaxMemory=Runtime.getRuntime().maxMemory();
        long VmtotalMemory=Runtime.getRuntime().totalMemory();
        long Memorypercentage=((VmtotalMemory-VmfreeMemory)*100)/VmtotalMemory; 

        Log.i(TAG,FunctionName+"Before Memorypercentage"+Memorypercentage+"% VmtotalMemory["+VmtotalMemory+"] "+"VmfreeMemory["+VmfreeMemory+"] "+"VmmaxMemory["+VmmaxMemory+"] ");

          //Runtime.getRuntime().gc();
          System.runFinalization();
          System.gc();
          VmfreeMemory =Runtime.getRuntime().freeMemory();
          VmmaxMemory=Runtime.getRuntime().maxMemory();
          VmtotalMemory=Runtime.getRuntime().totalMemory();
          Memorypercentage=((VmtotalMemory-VmfreeMemory)*100)/VmtotalMemory; 
        Log.i(TAG,FunctionName+"_After Memorypercentage"+Memorypercentage+"% VmtotalMemory["+VmtotalMemory+"] "+"VmfreeMemory["+VmfreeMemory+"] "+"VmmaxMemory["+VmmaxMemory+"] ");
    }
    public void PrintOutput(String FunctionName,byte[] Data)
    {
        for(int i=0;i<Data.length;i++)
        {
            Log.i(TAG,FunctionName+"Data["+i+"]="+Data[i]);
        }
    }   
4

0 回答 0