Android蓝牙共享活动与自动配对之间的连接

时间:2012-10-24 22:41:14

标签: android printing bluetooth sharing

我很少见到这里,因为我的大部分问题都是用例子和样本解决的。哪个我也不错。现在我的问题是我无法弄清楚到底是怎么回事。所以我买了这台打印机,我得到了SDK, 在sdk中,一旦我加载它,我必须按连接,它配对,我打入引脚(0000),一旦完成,我得到测试按钮显示并打印字符串等。

我想要它做的是,连接到打印机(自动或带针,无论如何) 然后按“下一个活动”并能够在下一个活动中打印我想要的蓝牙打印机。但是当下一个活动加载时,连接关闭,我无法使用打印机,除非我一起创建连接方法进入该活动,我需要一遍又一遍地配对设备。所以这是我的SDK。我真的希望有人可以帮助我,因为我会疯狂,我尝试使用getbondeddevices但我不认为我足够聪明......所以这里。也许有人可以看到打印机如何连接以及如何在整个活动中分享它?

甚至可能是关于如何配对设备然后在下一个活动中使用它的教程...任何帮助都会很棒

    public class PrintTestAcitvity extends Activity {

    // btOperation bo = new btOperation();
    //// ¶ÔÆ뷽ʽ
    /**
     * printing text align left
     */
    public static final int AT_LEFT     = 0;
    /**
     * printing text align center
     */
    public static final int AT_CENTER   = 1;
    /**
     * printing text align right
     */
    public static final int AT_RIGHT    = 2;
    private static final String GetBondedDevice = null;
   // public String curentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    public static String curentDateTimeString() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
        Date now = new Date();
        String strDate = sdfDate.format(now);
        return strDate;
    }
    private Button btConnect = null;
    private Button btCancel = null;
    private Button asciiSend = null;
    private Button graphicSend = null;
    private regoPrinter mobileprint = null;
    private boolean bConnect = true;
    private TextView textTitle = null;
//  private EditText portName;
    private int iObjectCode;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btConnect   = (Button)findViewById(R.id.connect);
        btCancel    = (Button)findViewById(R.id.cancel);
        asciiSend   = (Button)findViewById(R.id.asc_send);
        graphicSend = (Button)findViewById(R.id.gra_send);
        textTitle   = (TextView)findViewById(R.id.title);     
 //       portName  = (EditText)findViewById(R.id.portName);

        iObjectCode = 0;
        mobileprint = new regoPrinter();
        asciiSend.setEnabled(false);  
        graphicSend.setEnabled(false);



        btConnect.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {

                String strName = "MPT-II"; // portName.getText().toString();
                if(strName.length() == 0)
                {
                    Toast.makeText(PrintTestAcitvity.this, "Error:port name empty", Toast.LENGTH_SHORT).show(); 
                    return;
                }

                if(bConnect)
                {       
                    try
                    { 
                         mobileprint.CON_LnitPrintLib();

                        iObjectCode = mobileprint.CON_ConnectDevice(strName);
                    }
                    catch(printLibException e)
                    {
                        Toast.makeText(PrintTestAcitvity.this, e.GetMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                    finally
                    {
                        if(iObjectCode != 0)
                        {
                            // connect succeed  
                            textTitle.setText(strName + PrintTestAcitvity.this.getString(R.string.consucceed));
                            btConnect.setText(R.string.disconnect);
                            bConnect = false;
                         asciiSend.setEnabled(true);
                         graphicSend.setEnabled(true);
                        }
                    }
                }
                else
                {
                    textTitle.setText(strName + PrintTestAcitvity.this.getString(R.string.disconnect));
                    asciiSend.setEnabled(false);
                    graphicSend.setEnabled(false);
                    try {
                        mobileprint.CON_CloseConnect(iObjectCode);
                    } catch (printLibException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    bConnect = true;
                    btConnect.setText(R.string.connect);
                }
            }
        });        

        btCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(!bConnect)
                {
                    try {
                        mobileprint.CON_CloseConnect(iObjectCode);
                    } catch (printLibException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                mobileprint.CON_FreePrintLib();
                finish();
                return;
            }
        });

        asciiSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub 
                     try { 
                         // mobileprint.CON_PageStart(iObjectCode, 576, 0) &&
                     if(mobileprint.ASCII_QueryPrinterStatus(iObjectCode))
                     {  
                         //Çå³ý´òÓ¡»ú»º³å
                         mobileprint.ASCII_Reset(iObjectCode);

                         //´òÓ¡LogoͼƬ£¬ÏȽ«Í¼Æ¬ÏÂÔØÖÁ´òÓ¡»úÖÐÔÙÖ´Ðд˺¯Êý
                         //mobileprint.PrintFlashPic(1, 0);


                         //´òÓ¡title
                         mobileprint.ASCII_AlignType(iObjectCode, 1);

                         mobileprint.ASCII_FormatString(iObjectCode,false,false,true,false,false);                       
                         mobileprint.ASCII_SendString(iObjectCode, "Plate:" +"   " + "ABCD123", "gb2312");
                         // mobileprint.ASCII_PrintCRLF(iObjectCode, 2);
                         mobileprint.ASCII_FeedLines(iObjectCode, 1);
                         mobileprint.ASCII_Reset(iObjectCode);


                         mobileprint.ASCII_PrintCRLF(iObjectCode, 2);

                         mobileprint.CON_PageEnd(iObjectCode);
                     }
                     else
                     {
                            Toast.makeText(PrintTestAcitvity.this, "Printer status error",
                                    Toast.LENGTH_LONG).show();
                     }           
                } catch (printLibException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(PrintTestAcitvity.this, e.GetMessage(),
                            Toast.LENGTH_LONG).show();
                }               
            }
        });

        graphicSend.setOnClickListener(new OnClickListener()
        { 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                     try { 
                     if(mobileprint.CON_PageStart(iObjectCode, 576, 450) && mobileprint.ASCII_QueryPrinterStatus(iObjectCode))
                     {  
                         //Çå³ý´òÓ¡»ú»º³å
                         mobileprint.ASCII_Reset(iObjectCode);

                            mobileprint.DRAW_Line(iObjectCode,0, 1, 575, 1,3);
                            mobileprint.DRAW_Line(iObjectCode,0, 1, 0, 450,3);
                            mobileprint.DRAW_Line(iObjectCode,575, 1, 575, 450,3);
                            mobileprint.DRAW_Line(iObjectCode,0, 450, 575, 450,3);
                            mobileprint.DRAW_Line(iObjectCode,465, 1, 465, 450,1);
                            mobileprint.DRAW_Rectangle(iObjectCode, 0, 0, 80, 50, 1, true);
                            /* mobileprint.DRAW_Text(iObjectCode, 30, 80, "ÉϺ£¿ì½Ý¿ìÔËÓÐÏÞ¹«Ë¾ ", 40); 
                            mobileprint.DRAW_Text(iObjectCode,90, 185, "ÉϺ£         ->     ±±¾©", 35);
                            mobileprint.DRAW_Text(iObjectCode,80, 245, "(ÉϺ£±±Ç๫·°ì)", 20);
                            mobileprint.DRAW_Text(iObjectCode,310, 245, "(»ðʮ·վ)", 20);
                            mobileprint.DRAW_Text(iObjectCode,20, 305, "20112-02100100-0101-2112-10", 30);
                            mobileprint.DRAW_Code128(iObjectCode, 60, 350, 1, 60, "754432112-2");
                            mobileprint.DRAW_CreateRotalBlock(iObjectCode,476, 0, 450,100, 2);//(0.0) Ϊ×óÉϽǵľØÐÎ
                            mobileprint.DRAW_Code128(iObjectCode, 40, 5, 1, 60, "754432112-2");
                            */

                         mobileprint.CON_PageEnd(iObjectCode);
                     } 
                     else
                     {
                            Toast.makeText(PrintTestAcitvity.this, "Printer status error",
                                    Toast.LENGTH_LONG).show();
                     }           
                } catch (printLibException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(PrintTestAcitvity.this, e.GetMessage(),
                            Toast.LENGTH_LONG).show();
                }               
            }
        });
    }
}

0 个答案:

没有答案