国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android下的POS打印機調用的簡單實現

2019-12-12 03:06:58
字體:
來源:轉載
供稿:網友

本文基于GP58系列,它可以兼容ESC/POS指令集,對EPSON的打印機通用.

Android下的設備調試,如果設備提供了驅動,按照廠家的驅動調試即可;設備未提供驅動,只能按照通用的方法進行調試。這里采用的是調用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系統會詢問你是否允許設備訪問,默認為false;當允許了訪問之后,會判斷USB的引用是否為null,如果不為空則會調用setDevice來創建一個Connection,否則會關閉本次連接。

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

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.在相關的類中新建一個UsbAdmin,調用openUsb,這里首先是走了上面的setDevice()方法,獲取到了設備的引用,當連接通道建立時列出所有USB設備,當設備的引用不存在時同樣列出所有的USB設備,并且都請求獲取USB權限。

public void openUsb() {     if (mDevice != null) {       setDevice(mDevice);       if (mConnection == null) {         HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();         Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();          while (deviceIterator.hasNext()) {           UsbDevice device = deviceIterator.next();           mUsbManager.requestPermission(device, mPermissionIntent);         }       }     } else {       HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();       Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();        while (deviceIterator.hasNext()) {         UsbDevice device = deviceIterator.next();         mUsbManager.requestPermission(device, mPermissionIntent);       }     }   } 

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

public class printerCmdUtils {    /**    * 這些數據源自愛普生指令集,為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;//數據連接換碼   public static final byte EOT = 4;//傳輸結束   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;//作廢(頁模式下取消打印數據 )          //------------------------打印機初始化-----------------------------         /**    * 打印機初始化    * @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<lineNum;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<byteList.length;i++)     {         length += byteList[i].length;     }     byte[] result = new byte[length];          int index = 0;     for(int i=0;i<byteList.length;i++)     {         byte[] nowByte = byteList[i];         for(int k=0;k<byteList[i].length;k++)         {           result[index] = nowByte[k];           index++;         }     }     return result;    }          } 

4.在以上都完成之后,就可以把你需要的字符串轉換成byte數組并調用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, "發送失敗! " + len);       } else {         Result = true;         Log.i(TAG, "發送" + len + "字節數據");       }     }     return Result; 

復制代碼 代碼如下:

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

這一步僅僅加了同步鎖,并未開啟一個新的線程去處理,在本機上沒有問題,但上面的USB通信機制的文章有提到要放到異步線程,這里需要注意。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威宁| 嘉禾县| 尼木县| 叙永县| 丰原市| 钦州市| 如东县| 隆尧县| 会泽县| 托克逊县| 临武县| 铜川市| 思南县| 广灵县| 沙田区| 自治县| 香格里拉县| 西丰县| 同德县| 卫辉市| 察哈| 鄂托克前旗| 绥江县| 阿图什市| 本溪| 泾源县| 灵台县| 土默特左旗| 万宁市| 荔波县| 稻城县| 新丰县| 宜春市| 白山市| 江达县| 白沙| 崇信县| 营山县| 玉溪市| 绥宁县| 封开县|