簡體   English   中英

如何從phonegap javascript調用此android藍牙打印程序?

[英]How to call this android bluetooth printing program from phonegap javascript?

我被做成了一個藍牙打印機應用程序(基於Android),用於使用datecs DPP-350打印機設備打印一些文本。 該程序使用datecs外部庫,例如bluetoohconnector和RFComm軟件包。 它運行良好,代碼如下:

package com.myapp.MobilePrinter1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.djarum.MobilePrinter1.BluetoothConnector;
import com.datecs.api.card.FinancialCard;
import com.datecs.api.printer.Printer;
import com.datecs.api.printer.PrinterInformation;
import com.datecs.api.printer.ProtocolAdapter;
import com.datecs.api.printer.ProtocolAdapter.Channel;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MobilePrinter1Activity extends Activity {

    public static final String CONNECTION_STRING = "connection_string";

    private final Handler mHandler = new Handler();

    private final Thread mConnectThread = new Thread() {
        @Override
        public void run() {
            String connectionString = "bth://00:01:90:E6:40:52";

            showProgress("Connecting");

            if (connectionString.startsWith("bth://")) {
                String address = connectionString.substring(6);
                connectBth(address);
            } else {
                throw new IllegalArgumentException("Unsupported connection string");
            }

            dismissProgress();
        }   

        void connectBth(String address) {
            //setPrinterInfo(R.drawable.help, address);            

            try {
                mBthConnector = BluetoothConnector.getConnector(MobilePrinter1Activity.this);
                mBthConnector.connect(address);
                mPrinter = getPrinter(
                        mBthConnector.getInputStream(), 
                        mBthConnector.getOutputStream());               
            } catch (IOException e) {
                //error(R.drawable.bluetooth, e.getMessage());              
                return;
            }                       

            mPrinterInfo = getPrinterInfo();            
        }

        Printer getPrinter(InputStream in, OutputStream out) throws IOException {
            ProtocolAdapter adapter = new ProtocolAdapter(in, out);
            Printer printer = null;

            if (adapter.isProtocolEnabled()) {
                Channel channel = adapter.getChannel(ProtocolAdapter.CHANNEL_PRINTER);
                InputStream newIn = channel.getInputStream();
                OutputStream newOut = channel.getOutputStream();
                printer = new Printer(newIn, newOut);
            } else {
                printer = new Printer(in, out);
            }

            return printer;
        }

        PrinterInformation getPrinterInfo() {
            PrinterInformation pi = null;

            try {
                pi = mPrinter.getInformation();
                //setPrinterInfo(R.drawable.printer, pi.getName());                
            } catch (IOException e) {
                e.printStackTrace();                                
            }

            return pi;
        }
    };

    private BluetoothConnector mBthConnector;
    private Printer mPrinter;
    private PrinterInformation mPrinterInfo;
    private ProgressDialog mProgressDialog;
    private BluetoothConnector mConnector;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            mConnector = BluetoothConnector.getConnector(this);
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
            finish();           
        }

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printText();                
            }           
        });

        findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printBarcode();                 
            }           
        });  

        findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printImage();               
            }           
        });
    }

    public void printText() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintText2();
                dismissProgress();
            }
        }.start();                              
    } 

    public void printBarcode() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintBarcode();
                dismissProgress();
            }
        }.start();                              
    } 

    public void printImage() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintImage();
                dismissProgress();
            }
        }.start();                              
    } 

    @Override
    protected void onStart() {
        super.onStart();
        mConnectThread.start();        
    }   

    @Override
    protected void onStop() {
        super.onStop();

        if (mBthConnector != null) {
            try {
                mBthConnector.close();
            } catch (IOException e) {
                e.printStackTrace();
            }           
        }       
    }   

    private void showProgress(final String text) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressDialog = ProgressDialog.show(
                        MobilePrinter1Activity.this,
                        "Please wait", 
                        text,
                        true);                            
            }           
        });             
    }

    private void showProgress(int resId) {
        showProgress(getString(resId));
    }

    private void dismissProgress() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {                
                mProgressDialog.dismiss();             
            }           
        });     
    }            

    private void doPrintSelfTest() {
        try {           
            mPrinter.printSelfTest();                       
        } catch (IOException e) {
            //error(R.drawable.selftest, getString(R.string.failed_print_self_test) + ". " + 
                    //e.getMessage());
        }
    }

    private void doPrintText2() {
        EditText EditText1;
        EditText1=(EditText)findViewById(R.id.editText1);
        String temp;
        try {           
            mPrinter.reset();  
            mPrinter.printTaggedText(EditText1.getText().toString());   
            //mPrinter.printTaggedText("Testing Testing!!"); 
            mPrinter.feedPaper(110);
        } catch (IOException e) {
            //error(R.drawable.text, getString(R.string.failed_print_text) + ". " + 
                    //e.getMessage());          
        }
    }

    private void doPrintBarcode() {
        EditText EditText1;
        EditText1=(EditText)findViewById(R.id.editText1);
        try {           
            mPrinter.reset();

            mPrinter.setBarcode(Printer.ALIGN_CENTER, false, 2, Printer.HRI_BOTH, 100);
            mPrinter.printBarcode(Printer.BARCODE_CODE128, EditText1.getText().toString());
            mPrinter.feedPaper(38);

            mPrinter.feedPaper(110);
        } catch (IOException e) {
            //error(R.drawable.barcode, getString(R.string.failed_print_barcode) + ". " +
                    //e.getMessage());          
        }
    }
    private void doPrintImage() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo_djarum);
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        final int[] argb = new int[width * height];     
        bitmap.getPixels(argb, 0, width, 0, 0, width, height);              

        try {
            mPrinter.reset();            
            mPrinter.printImage(argb, width, height, Printer.ALIGN_LEFT, true);
            mPrinter.feedPaper(110);            
        } catch (IOException e) {
            Toast.makeText(MobilePrinter1Activity.this, e.getMessage(), 1).show();
        }
    }

    private void dialog(final int id, final String title, final String msg) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                AlertDialog dlg = new AlertDialog.Builder(MobilePrinter1Activity.this)
                .setTitle(title)
                .setMessage(msg)               
                .create();  
                dlg.setIcon(id);
                dlg.show();             
            }           
        });             
    }

    private void error(final int resIconId, final String message) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {             
                AlertDialog dlg = new AlertDialog.Builder(MobilePrinter1Activity.this)
                .setTitle("Error")
                .setMessage(message)               
                .create();
                dlg.setIcon(resIconId);
                dlg.setOnDismissListener(new OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        MobilePrinter1Activity.this.finish();
                    }                   
                });
                dlg.show();             
            }           
        });             
    }

    private void setPrinterInfo(final int resIconId, final String text) {
        mHandler.post(new Runnable() {          
            @Override
            public void run() {
                //((ImageView)findViewById(R.id.icon)).setImageResource(resIconId);
                //((TextView)findViewById(R.id.name)).setText(text);
            }
        });
    }
}

現在的主要問題是如何從phonegap調用此程序? 我已經嘗試使用droidGap,但是當我啟動打印機線程時,它將給我錯誤。 有誰知道如何解決這個問題? 非常感謝..

我不認為您可以從標准的android瀏覽器中調用太多API(除了一些類似的位置,聯系人以外的所有對象),但是可能的另一種方法是將webview嵌入本機應用程序中(可以是上述線程代碼) ),並使用android平台的JavaScript接口api從Javascript事件中調用此代碼(這很簡單)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM