2012-01-06 23 views
0

我做了一個藍牙打印機應用程序(基於Android),用於打印一些使用datecs DPP-350打印機設備的文本。這個程序使用一個datecs外部庫,例如bluetoohconnector和RFComm包。它很好地工作,這裏的代碼:如何從phonegap javascript調用此android bluetooth打印程序?

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,但它會給我錯誤,當我開始打印機的線程。有誰知道如何解決這個問題?非常感謝..

+0

這是可能的在android中創建藍牙打印機API而不使用外部庫。 – Karthick 2013-04-05 07:27:14

回答

0

我不認爲你可以從標準的Android瀏覽器(除了一些像位置,聯繫人n所有)調用太多的API,但有可能是其他方式一輪嵌入一個本地應用程序的webview它可以是你上面提到的線程代碼),並使用JavaScript平臺的JavaScript接口API(這是非常簡單的)從JavaScript事件中調用此代碼。

+0

我同意你dcool,但在這個項目中,我必須使用phonegap來創建該程序,並且必須使用datecs dpp-350作爲打印機..任何其他解決方案?謝謝 – 2012-01-06 04:19:59

+0

我目前沒有..但請更新在這裏,如果一旦你得到它的工作方式。 – dcool 2012-01-06 04:53:29

相關問題