技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務器類 >

Android下的POS打印機調(diào)用的簡單實現(xiàn)教程

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2017-11-07 00:29┆點擊:

  Android下的設備調(diào)試,如果設備提供了驅(qū)動,按照廠家的驅(qū)動調(diào)試即可;設備未提供驅(qū)動,只能按照通用的方法進行調(diào)試。這里采用的是調(diào)用USB接口來控制打印機輸出。

  1.首先獲取USB管理器

  public UsbAdmin(Context context) {

  mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

  mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);

  IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

  context.registerReceiver(mUsbReceiver, filter);

  }

  使用一個延遲意圖來接收usb接入時的廣播,當廣播接收到時,說明有新的設備接入。

  添加一個boardcast action

  復制代碼 代碼如下:

  private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

  private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

  public void onReceive(Context context, Intent intent) {

  String action = intent.getAction();

  if (ACTION_USB_PERMISSION.equals(action)) {

  synchronized (this) {

  UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

  if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

  if (device != null) {

  setDevice(device);

  } else {

  Closeusb();

  // mDevice = device;

  }

  } else {

  Log.d(TAG, "permission denied for device " + device);

  }

  }

  }

  }

  };

  取到usb設備的引用,android系統(tǒng)會詢問你是否允許設備訪問,默認為false;當允許了訪問之后,會判斷USB的引用是否為null,如果不為空則會調(diào)用setDevice來創(chuàng)建一個Connection,否則會關(guān)閉本次連接。

  在setDevice中,我們可以獲取設備的功能集(UsbInterface),也可以獲取通信通道(UsbEndpoint),同時也創(chuàng)建了host與device的連接用來傳輸數(shù)據(jù)。

  private void setDevice(UsbDevice device) {

  if (device != null) {

  UsbInterface intf = null;

  UsbEndpoint ep = null;

  int InterfaceCount = device.getInterfaceCount();

  int j;

  mDevice = device;

  for (j = 0; j < InterfaceCount; j++) {

  int i;

  intf = device.getInterface(j);

  Log.i(TAG, "接口是:" + j + "類是:" + intf.getInterfaceClass());

  if (intf.getInterfaceClass() == 7) {

  int UsbEndpointCount = intf.getEndpointCount();

  for (i = 0; i < UsbEndpointCount; i++) {

  ep = intf.getEndpoint(i);

  Log.i(TAG, "端點是:" + i + "方向是:" + ep.getDirection() + "類型是:" + ep.getType());

  if (ep.getDirection() == 0 && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {

  Log.i(TAG, "接口是:" + j + "端點是:" + i);

  break;

  }

  }

  if (i != UsbEndpointCount) {

  break;

  }

  }

  }

  if (j == InterfaceCount) {

  Log.i(TAG, "沒有打印機接口");

  return;

  }

  mEndpointIntr = ep;

  UsbDeviceConnection connection = mUsbManager.openDevice(device);

  if (connection != null && connection.claimInterface(intf, true)) {

  Log.i(TAG, "打開成功! ");

  mConnection = connection;

  } else {

  Log.i(TAG, "打開失敗! ");

  mConnection = null;

  }

  }

  }

  2.在相關(guān)的類中新建一個UsbAdmin,調(diào)用openUsb,這里首先是走了上面的setDevice()方法,獲取到了設備的引用,當連接通道建立時列出所有USB設備,當設備的引用不存在時同樣列出所有的USB設備,并且都請求獲取USB權(quán)限。

  public void openUsb() {

  if (mDevice != null) {

  setDevice(mDevice);

  if (mConnection == null) {

  HashMap deviceList = mUsbManager.getDeviceList();

  Iterator deviceIterator = deviceList.values().iterator();

  while (deviceIterator.hasNext()) {

  UsbDevice device = deviceIterator.next();

  mUsbManager.requestPermission(device, mPermissionIntent);

  }

  }

  } else {

  HashMap deviceList = mUsbManager.getDeviceList();

  Iterator deviceIterator = deviceList.values().iterator();

  while (deviceIterator.hasNext()) {

  UsbDevice device = deviceIterator.next();

  mUsbManager.requestPermission(device, mPermissionIntent);

  }

  }

  }

  3.當上面兩部都走完了之后,我們就可以發(fā)送指令來控制已經(jīng)建立連接的打印機了,這里我們使用的是標準的ESC/POS指令集,為硬件默認,貼出代碼,這里的指令集采用的是十進制表示形式,也可以替換成十六進制。

  public class printerCmdUtils {

  /**

  * 這些數(shù)據(jù)源自愛普生指令集,為POS機硬件默認

  */

  public static final byte ESC = 27;//換碼

  public static final byte FS = 28;//文本分隔符

  public static final byte GS = 29;//組分隔符

  public static final byte DLE = 16;//數(shù)據(jù)連接換碼

  public static final byte EOT = 4;//傳輸結(jié)束

  public static final byte ENQ = 5;//詢問字符

  public static final byte SP = 32;//空格

  public static final byte HT = 9;//橫向列表

  public static final byte LF = 10;//打印并換行(水平定位)

  public static final byte CR = 13;//歸位鍵

  public static final byte FF = 12;//走紙控制(打印并回到標準模式(在頁模式下) )

  public static final byte CAN = 24;//作廢(頁模式下取消打印數(shù)據(jù) )

  //------------------------打印機初始化-----------------------------

  /**

  * 打印機初始化

  * @return

  */

  public static byte[] init_printer()

  {

  byte[] result = new byte[2];

  result[0] = ESC;

  result[1] = 64;

  return result;

  }

  //------------------------換行-----------------------------

  /**

  * 換行

  * @param lineNum要換幾行

  * @return

  */

  public static byte[] nextLine(int lineNum)

  {

  byte[] result = new byte[lineNum];

  for(int i=0;i

  {

  result[i] = LF;

  }

  return result;

  }

  //------------------------下劃線-----------------------------

  /**

  * 繪制下劃線(1點寬)

  * @return

  */

  public static byte[] underlineWithOneDotWidthOn()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 45;

  result[2] = 1;

  return result;

  }

  /**

  * 繪制下劃線(2點寬)

  * @return

  */

  public static byte[] underlineWithTwoDotWidthOn()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 45;

  result[2] = 2;

  return result;

  }

  /**

  * 取消繪制下劃線

  * @return

  */

  public static byte[] underlineOff()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 45;

  result[2] = 0;

  return result;

  }

  //------------------------加粗-----------------------------

  /**

  * 選擇加粗模式

  * @return

  */

  public static byte[] boldOn()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 69;

  result[2] = 0xF;

  return result;

  }

  /**

  * 取消加粗模式

  * @return

  */

  public static byte[] boldOff()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 69;

  result[2] = 0;

  return result;

  }

  //------------------------對齊-----------------------------

  /**

  * 左對齊

  * @return

  */

  public static byte[] alignLeft()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 97;

  result[2] = 0;

  return result;

  }

  /**

  * 居中對齊

  * @return

  */

  public static byte[] alignCenter()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 97;

  result[2] = 1;

  return result;

  }

  /**

  * 右對齊

  * @return

  */

  public static byte[] alignRight()

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 97;

  result[2] = 2;

  return result;

  }

  /**

  * 水平方向向右移動col列

  * @param col

  * @return

  */

  public static byte[] set_HT_position( byte col )

  {

  byte[] result = new byte[4];

  result[0] = ESC;

  result[1] = 68;

  result[2] = col;

  result[3] = 0;

  return result;

  }

  //------------------------字體變大-----------------------------

  /**

  * 字體變大為標準的n倍

  * @param num

  * @return

  */

  public static byte[] fontSizeSetBig(int num)

  {

  byte realSize = 0;

  switch (num)

  {

  case 1:

  realSize = 0;break;

  case 2:

  realSize = 17;break;

  case 3:

  realSize = 34;break;

  case 4:

  realSize = 51;break;

  case 5:

  realSize = 68;break;

  case 6:

  realSize = 85;break;

  case 7:

  realSize = 102;break;

  case 8:

  realSize = 119;break;

  }

  byte[] result = new byte[3];

  result[0] = 29;

  result[1] = 33;

  result[2] = realSize;

  return result;

  }

  //------------------------字體變小-----------------------------

  /**

  * 字體取消倍寬倍高

  * @param num

  * @return

  */

  public static byte[] fontSizeSetSmall(int num)

  {

  byte[] result = new byte[3];

  result[0] = ESC;

  result[1] = 33;

  return result;

  }

  //------------------------切紙-----------------------------

  /**

  * 進紙并全部切割

  * @return

  */

  public static byte[] feedPaperCutAll()

  {

  byte[] result = new byte[4];

  result[0] = GS;

  result[1] = 86;

  result[2] = 65;

  result[3] = 0;

  return result;

  }

  /**

  * 進紙并切割(左邊留一點不切)

  * @return

  */

  public static byte[] feedPaperCutPartial()

  {

  byte[] result = new byte[4];

  result[0] = GS;

  result[1] = 86;

  result[2] = 66;

  result[3] = 0;

  return result;

  }

  //------------------------切紙-----------------------------

  public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){

  byte[] byte_3 = new byte[byte_1.length+byte_2.length];

  System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);

  System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);

  return byte_3;

  }

  public static byte[] byteMerger(byte[][] byteList){

  int length = 0;

  for(int i=0;i

  {

  length += byteList[i].length;

  }

  byte[] result = new byte[length];

  int index = 0;

  for(int i=0;i

  {

  byte[] nowByte = byteList[i];

  for(int k=0;k

  {

  result[index] = nowByte[k];

  index++;

  }

  }

  return result;

  }

  }

  4.在以上都完成之后,就可以把你需要的字符串轉(zhuǎn)換成byte數(shù)組并調(diào)用sendCommand方法來進行打印了

  @SuppressLint("NewApi")

  public boolean sendCommand(byte[] Content) {

  boolean Result;

  synchronized (this) {

  int len = -1;

  if (mConnection != null) {

  len = mConnection.bulkTransfer(mEndpointIntr, Content, Content.length, 10000);

  }

  if (len < 0) {

  Result = false;

  Log.i(TAG, "發(fā)送失敗! " + len);

  } else {

  Result = true;

  Log.i(TAG, "發(fā)送" + len + "字節(jié)數(shù)據(jù)");

  }

  }

  return Result;

  復制代碼 代碼如下:

  len = mConnection.bulkTransfer(mEndpointIntr, Content, Content.length, 10000);