适配三方打印apk---GS v 0 m xL xH yL yH d1....dk 打印光栅位图

package com.android.bluetooth.print;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.widget.Toast;
import android.util.Log;

import java.util.ArrayList;
import java.util.UUID;
import com.pos.sdk.printer.PosPrinter;
import com.pos.sdk.printer.POIPrinterManager;
import com.pos.sdk.printer.PosPrinterInfo;
import com.pos.sdk.printer.models.BitmapPrintLine;
import com.pos.sdk.printer.models.PrintLine;
import com.pos.sdk.printer.models.TextPrintLine;

import static com.android.bluetooth.print.BtPosPrintService.bytesToHexString;
public class PosPrintUtil {
private static final String TAG="PosPrintUtil";
private static PrinterAttributes attributes;
public final static int TYPE_CONTENT = 1;
public final static int TYPE_COMMAND = 2;
public static final byte GS = 29;
public static final byte CR = 13;
public static Bitmap bitmap;
public static byte[] buffer1 = new byte[50000];
public static final boolean PRINTER_SUPPORT = SystemProperties.getInt("ro.bluetooth.printer.support",0)==1;
public static final String PRINTER_ADDRESS = SystemProperties.get("ro.bluetooth.printer.address","11:22:33:44:55:66");
public static final String PRINTER_SOCKET = SystemProperties.get("ro.bluetooth.printer.socket","com.android.local.bluetoothprinter");
public static final String PRINTER_NAME = SystemProperties.get("ro.bluetooth.printer.name","BluetoothPrinter");
public static final UUID PRINTER_UUID = UUID.fromString(SystemProperties.get("ro.bluetooth.printer.uuid","00001101-0000-1000-8000-00805F9B34FB"));

private POIPrinterManager printerManager;
private PosPrinter posPrinter;
private PrinterListener printer_callback = new PrinterListener();

private Context mContext;

private Integer mLineSpace = Integer.valueOf(2);
private int mPosition = PrintLine.CENTER;

public PosPrintUtil(Context context){
this.mContext = context;
}

class PrinterListener implements POIPrinterManager.IPrinterListener {

@Override
public void onStart() {
Log.d(TAG, "start print");
}

@Override
public void onFinish() {
Log.d(TAG, "pint success");
}

@Override
public void onError(int errorCode, String detail) {
}
}

private void initPrint(){
Log.d(TAG, "initPrint");
printerManager = new POIPrinterManager(mContext);
}

private void openPrint(){
Log.d(TAG, "openPrint");
printerManager.open();
}

private void closePrint(){
Log.d(TAG, "closePrint");
printerManager.close();
}

private void onPrintStart(){
Log.d(TAG, "onPrintStart");
printerManager.setLineSpace(mLineSpace);
printerManager.cleanCache();
}

private void printText(byte[] data){
Log.d(TAG,"printText");
onPrintStart();
TextPrintLine mTextPrintLine = new TextPrintLine();
onPrintTextSettings(mTextPrintLine);
onPrintText(data,mTextPrintLine);
}

private void onPrintText(byte[] content,TextPrintLine textPrintLine){
String text = new String(content);
Log.d(TAG,"text" + text);
textPrintLine.setContent(text);
printerManager.addPrintLine(textPrintLine);
}

private void onPrintTextSettings(TextPrintLine textPrintLine){
textPrintLine.setType(PrintLine.TEXT);
textPrintLine.setSize(TextPrintLine.FONT_NORMAL);
textPrintLine.setPosition(mPosition);
}

private void printBitmap(byte[] data){
Log.d(TAG,"printBitmap");
onPrintStart();
BitmapPrintLine mBitmapPrintLine = new BitmapPrintLine();
onPrintBitmapSettings(mBitmapPrintLine);
onPrintBitmap(data,mBitmapPrintLine);
}

private void onPrintBitmapSettings(BitmapPrintLine bitmapPrintLine){
bitmapPrintLine.setType(PrintLine.BITMAP);
bitmapPrintLine.setPosition(mPosition);
}

private void onPrintBitmap(byte[] data , BitmapPrintLine bitmapPrintLine){
Bitmap mBitmap = ByteUtil.stringtoBitmap(new String(data));
bitmapPrintLine.setBitmap(mBitmap);
printerManager.addPrintLine(bitmapPrintLine);
}

public boolean printerPaper(){
PosPrinterInfo info = new PosPrinterInfo();
PosPrinter.getPrinterInfo(0, info);
if(info.mHavePaper == 1) {
return true;
}
return false;
}

public void handleData(byte[] data){
Log.d(TAG,"handleData");
build(mContext,data);
byte[] btye_1= ByteUtil.byteSplit1(data);
byte[] btye_2= ByteUtil.byteSplit2(data);
int mType = ByteUtil.getCommandType(btye_1);
Log.d(TAG,"mType =" + mType);
switch(mType){
case 1:
initPrint();
break;
case 2:
openPrint();
break;
case 3:
closePrint();
break;
case 4:
printText(btye_2);
break;
case 5:
printBitmap(btye_2);
break;
case 6:
//setLineSpace
mLineSpace = ByteUtil.bytetoInteger(btye_2);
break;
case 7:
//beginPrint
Log.d(TAG,"beginPrint" + printerPaper());
if(printerPaper()) {
printerManager.beginPrint(printer_callback);
}
break;
case 8:
//setPosition
mPosition = ByteUtil.bytetoint(btye_2);
break;
case 9:
//fontType
break;
case 10:
//lineWrap
if(printerPaper()) {
onPrintStart();
printerManager.lineWrap(ByteUtil.bytetoint(btye_2));
}
break;
case 11:
//speed
break;
case 12: printBitmapNew
(bitmap);
break;
default:
break;
}
}
//wenjie.gu add for 3-rd app printer
public synchronized static int build(Context context, byte[] data){
if(data!=null&&data.length>0){
//initEncode(context);
ArrayList<Byte> content = new ArrayList<Byte>();
ArrayList<byte[]> command = new ArrayList<byte[]>();
ArrayList<String> textList = new ArrayList<String>();
ArrayList<PrinterAttributes> attrList = new ArrayList<PrinterAttributes>();
//PrinterAttributes attributes = new PrinterAttributes();
if(attributes == null){
attributes = new PrinterAttributes();
}
content.clear();
command.clear();
textList.clear();
attrList.clear();
//attributes.setDefaultAttributes();
int type = -1;
int skipType = -1;
boolean loopBreaked = false;
boolean sendCommand = false;
boolean skipCommand = false;
boolean skipLFCR = false;
boolean textSizeChange = false;
//boolean line = true;
byte[] prefix = new byte[5];
byte[] intput = data;
while (intput!=null&&intput.length>0&&!loopBreaked){
int length = intput.length;
int currentType = TYPE_COMMAND; sendCommand =
false;
PrinterAttributes attr = new PrinterAttributes(); attr =
PrinterAttributes.clone(attributes);
byte b = intput[0];
int size = 1;
switch (b){
case GS:
if(length>=2) {
prefix[0] = intput[0];
prefix[1] = intput[1];
switch (prefix[1]) {
case 118: //'v'
if(length >= 8){
int prefix2 = intput[2]&0xFF;
if(prefix2 == 48){
int prefix3 = intput[3]&0xFF;
int xL = intput[4]&0xFF;
int xH = intput[5]&0xFF;
int yL = intput[6]&0xFF;
int yH = intput[7]&0xFF;

int k = xL+xH*256;
int m = yL+yH*256; size =
k*m +8;
byte[] arrayOfByte = new byte[k*m];
try {
System.arraycopy(intput, 8, arrayOfByte, 0, k*m);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace(); size =
length;
break;
}
bitmap = rasterToBitmap(arrayOfByte, k, m, prefix3);
break;
}
}
size = length; sendCommand =
true;
break;
case 119: //'w'
if(length>=3){
int nW = intput[2]&0xff;
if(nW < 2 || nW > 6){
nW = PrinterConfigs.CONFIG_PRINTER_BAR_WIDTH;
}
//nW = ContentHelper.px2dip(context, nW);
attr.setBarcodeWidth(nW);
}
size = 3; sendCommand =
true;
break;
case 72: //'H'
if(length>=3){
int nP = intput[2]&0xff;
if(nP >= 48){
nP -= 48;
}
if(nP < 0 || nP > 3){
nP = PrinterConfigs.CONFIG_PRINTER_HRI_POSITION;
}
attr.setBarcodeHRIPOS(nP);
}
size = 3; sendCommand =
true;
break;
case 102: //'f'
size = 3; sendCommand =
true;
break;
case 107: //'k'
if(length>=3){
prefix[2] = intput[2];
int prefix2 = prefix[2]&0xff; // m
if(prefix2>=0 && prefix2<=8){
attributes.setBarcodeFormat(prefix2);
int len = 0;
for(int i = 3; i < length; i++){
int temp = intput[i]&0xff;
if(temp == 0){
len = i - 3;
break;
}
}
if(len != 0){
ArrayList<Byte> barcode = new ArrayList<Byte>();
String barcodeString = "";
int prefix4 = intput[4]&0xff;
int prefix5 = intput[5]&0xff;
if(prefix2 == 8 && prefix4 == 123 && prefix5 == 67){
for(int j=3;j<3 + len;j++){
int temp = intput[j]&0xff;
if(temp < 10){
barcodeString += 0;
}
barcodeString += temp;
}
}else{
for(int j=3;j<3+len;j++){
barcode.add(intput[j]);
}
barcodeString = byteToString(context,barcode);
}
// PrinterManager.getInstance(context).printBarCode(context, barcodeString,
// attributes.getAlign(), attributes.getBarcodeWidth(),
// attributes.getBarcodeHeight(), attributes.getBarcodeHRIPOS(),
// attributes.getBarcodeFormat());
//printBlank = true;
size = 4 + len;
}
}else if(prefix2>=65 && prefix2<=73 && length >=4){
prefix[3] = intput[3];
int prefix3 = prefix[3]&0xff; // n
attributes.setBarcodeFormat(prefix2-65);
if(length >= 4 + prefix3){
ArrayList<Byte> barcode = new ArrayList<Byte>();
if(intput!=null&&intput.length>=4 + prefix3){
String barcodeString = "";
int prefix4 = intput[4]&0xff;
int prefix5 = intput[5]&0xff;
if(prefix2 == 73 && prefix4 == 123 && prefix5 == 67){
for(int j=4;j<4 + prefix3;j++){
int temp = intput[j]&0xff;
if(temp < 10){
barcodeString += 0;
}
barcodeString += temp;
}
}else{
for(int j=4;j<4 + prefix3;j++){
barcode.add(intput[j]);
}
barcodeString = byteToString(context,barcode);
}
// PrinterManager.getInstance(context).printBarCode(context, barcodeString,
// attributes.getAlign(), attributes.getBarcodeWidth(),
// attributes.getBarcodeHeight(), attributes.getBarcodeHRIPOS(),
// attributes.getBarcodeFormat());
size = 4 + prefix3;
}
}
}else{
size = 3; sendCommand =
true;
}
break;
}
size = length; sendCommand =
true;
break;
case 40: //'('
if(length >=3){
prefix[2] = intput[2];
int prefix2 = prefix[2]&0xff;
switch (prefix2) {
case 107: // 'k'
if(length>=8){
size = 8;
int pL = intput[3]&0xFF;
int pH = intput[4]&0xFF;
//int cn = intput[5]&0xff;
int fn = intput[6]&0xff;
int m = intput[7]&0xff;
switch (fn) {
case 65: size =
9;
break;
case 67:
if(m<1 || m>16){
m = PrinterConfigs.CONFIG_PRINTER_QR_SIZE;
}
attr.setQRCodeSize(m);
break;
case 69:
if(m<48 || m>51){
m = PrinterConfigs.CONFIG_PRINTER_QR_ERROR;
}
attr.setQRCodeError(m - 48);
break;
case 80:
int k = pL + pH*256 - 3;
int total = 8 + k;
if(length >= total){
byte[] qrcode = new byte[k];
if(intput!=null&&intput.length>=total){
for(int i=0,j=8;j<total;i++,j++){
qrcode[i] = intput[j];
}
String path = context.getFilesDir() + FileUtils.DEFAULT_QRCODE_DATA_FILE;
FileUtils.writeByteToFile(qrcode, path, false); attr.setQRCodeSave
(1);
}
size = total;
}else{
size = length; sendCommand =
true;
}
break;
case 81:
if(length >= 8){
if(attr.getQRCodeSave() == 1){
String path = context.getFilesDir() + FileUtils.DEFAULT_QRCODE_DATA_FILE;
byte[] bytes = FileUtils.readByteFromFile(path);
ArrayList<Byte> qrcode = new ArrayList<Byte>();
for(int i = 0; i < bytes.length; i++){
qrcode.add(bytes[i]);
}
String qrcodeString = byteToString(context,qrcode);
// PrinterManager.getInstance(context).printQRCode(
// qrcodeString, attr.getAlign(),
// attr.getQRCodeSize(), attr.getQRCodeError());
}
size = 8;
}else{
size = length; sendCommand =
true;
}
break;
default:
break;
}
}else{
size = length; sendCommand =
true;
}
break;
default: size =
3; sendCommand =
true;
break;
}
}
break;
case 69: //'E'
if(length>=3){
prefix[2] = intput[2];
int prefix2 = prefix[2]&0xFF;
}
size = 3;
break;
case 114: //'r'
if(length>=3){
prefix[2] = intput[2];
int prefix2 = prefix[2]&0xFF;
switch (prefix2) {
case 2:
case 50:
// return read(CASH_BOX_NODE);
case 5:
// return read(PRINTER_CUT_NODE);
}
}
size = 3;
break;
default: size =
2;
break;
}
}else {
size = length; loopBreaked =
true;
}
break;
default: size =
1; currentType =
TYPE_CONTENT;
break;
}

byte[] splitPrefix = splitPrefixArrays(intput,size);
byte[] splitSuffix = splitSuffixArrays(intput,size);
if (currentType == TYPE_COMMAND) {
//PrinterLogger.i(TAG, "build TYPE_COMMAND splitPrefix = " + BytesHelper.bytesToHexString(splitPrefix));
}
boolean typeChanged = (type!=-1&&type!=currentType);
if(sendCommand){
}
if (currentType == TYPE_COMMAND) {
//attributes = PrinterAttributes.merge(attributes,attr);
attributes = PrinterAttributes.clone(attr);
}
intput = splitSuffix; type = currentType;

}
boolean isTextList = textList!=null&&textList.size()>0;
if(isTextList){
String contentString = byteToString(context,content);
if(!TextUtils.isEmpty(contentString)){
textList.add(contentString);
content.clear();
}
// printListBytes (context,textList,attrList,skipType);
}else{
}
//attributes.setDefaultAttributes();
attrList.clear();
textList.clear();
content.clear();
}
return -1;
}

private static byte[] splitPrefixArrays(byte[] data,int splitLength){
byte[] splitArrays = null;
if(data!=null&&data.length>0&&splitLength>0){
final int dataLength = data.length;
if(dataLength>=splitLength){
int deltaLength = splitLength; splitArrays =
new byte[deltaLength];
System.arraycopy(data,0,splitArrays,0,deltaLength);
}
}
return splitArrays;
}
private static byte[] splitSuffixArrays(byte[] data,int splitLength){
byte[] splitArrays = null;
if(data!=null&&data.length>0&&splitLength>=0){
final int dataLength = data.length;
if(dataLength>splitLength){
int deltaLength = dataLength - splitLength; splitArrays =
new byte[deltaLength];
System.arraycopy(data,splitLength,splitArrays,0,deltaLength);
}
}
return splitArrays;
}
private static String byteToString(Context context,ArrayList<Byte> content){
byte[] buffer = null;
if(content!=null&&content.size()>0){
final int SIZE = content.size(); buffer =
new byte[SIZE];
for(int i=0;i<SIZE;i++){
buffer[i] = content.get(i);
}
}
return null;
}
private static String byteToHexString(ArrayList<Byte> content){
byte[] buffer = null;
if(content!=null&&content.size()>0){
final int SIZE = content.size(); buffer =
new byte[SIZE];
for(int i=0;i<SIZE;i++){
buffer[i] = content.get(i);
}
try {
String text = bytesToHexString(buffer);
return text;
}catch (Exception e){
}
}
return null;
}
public static Bitmap rasterToBitmap(byte[] data, int w, int h, int flag) {
int x = 0, y = 0;
int skip_x = 1, skip_y = 1;
if((flag == 1) || (flag == 49)) {
skip_x = 2;
} else if((flag == 2) || (flag == 50)) {
skip_y = 2;
} else if((flag == 3) || (flag == 51)) {
skip_x = 2; skip_y =
2;
}
Bitmap bitmap = Bitmap.createBitmap(((w * 8) * skip_x), (h * skip_y), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(0.0f, 0.0f, (float)bitmap.getWidth(), (float)bitmap.getHeight(), paint);
paint.setColor(Color.BLACK);
for(int j = 0; j < h; j++) {
for(int i = 0; i < w; i++) {
int m = data[((j * w) + i)];
for(int n = 0; n < 8; n++) {
if((1 << 7 - n & m) > 0) {
canvas.drawRect((float)x, (float)y, (float)(x + skip_x), (float)(y + skip_y), paint);
}
x += skip_x;
}
}
y += skip_y; x =
0x0;
}
return bitmap;
}
private void printBitmapNew( Bitmap bitmap) {
initPrint(); openPrint
(); onPrintStart
();
if(printerPaper()) {
Log.d("wenjie.gu","printBitmapNew");
printerManager.setPrintGray(Integer.valueOf(1600));
//set lineSpace
printerManager.setLineSpace(Integer.valueOf(2));
BitmapPrintLine bitmapPrintLine = new BitmapPrintLine();
bitmapPrintLine.setType(PrintLine.BITMAP);
bitmapPrintLine.setPosition(PrintLine.CENTER);
//select a picture in assets folder
bitmapPrintLine.setBitmap(bitmap);
printerManager.addPrintLine(bitmapPrintLine);
printerManager.beginPrint(printer_callback);
}
//set Gray
//printerManager.beginPrint(printer_callback);
}
}



/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.bluetooth.print;

import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.content.BroadcastReceiver;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.ParcelUuid;
import android.provider.Settings;
import android.util.Log;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothAdapter;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Locale;
import java.util.UUID;
import com.android.bluetooth.print.PosPrintUtil;
import com.android.bluetooth.Utils;

import android.os.SystemClock;
/**
* Provides Bluetooth A2DP profile, as a service in the Bluetooth application.
* @hide
*/
public class BtPosPrintService extends ProfileService {
private static final boolean DBG = false;
private static final String TAG="BtPosPrinter";

private PosPrintUtil mPosPrintUtil = null;
private AcceptThread mAcceptThread = new AcceptThread();

public int onStartCommand(Intent intent, int flags, int startId) {
mPosPrintUtil = new PosPrintUtil(this.getApplicationContext());
return super.onStartCommand(intent, flags, startId);
}

protected IProfileServiceBinder initBinder() {
return null;
}

protected boolean start() {
startServer();
return true;
}

protected boolean stop() {
stopServer();
return true;
}

private void startServer(){
if(PosPrintUtil.PRINTER_SUPPORT){
mAcceptThread.start();
}
}
private void stopServer(){
if(PosPrintUtil.PRINTER_SUPPORT){
mAcceptThread.cancel();
}
}

private class AcceptThread extends Thread {
private final String TAG = "AcceptThread";
private final String PRINTER_ADDRESS = PosPrintUtil.PRINTER_ADDRESS;
private final String PRINTER_SOCKET = PosPrintUtil.PRINTER_SOCKET;
private final String PRINTER_NAME = PosPrintUtil.PRINTER_NAME;
private final UUID PRINTER_UUID = PosPrintUtil.PRINTER_UUID;

private LocalServerSocket mServerSocket;
private Object mLock = new Object();

private InputStream mSocketIS;
private OutputStream mSocketOS;

private LocalSocket mLocalSocket;
private boolean mLooperAccept = true;

private byte[] mAddressBytes = new byte[6];
private byte[] mUidBytes = new byte[4];

private String mAddressString;
private int mUidInteger;
private boolean mStartAddress;

public AcceptThread() {
Log.i(TAG, "acceptThread()");
LocalServerSocket server = null;
try {
server = new LocalServerSocket(PRINTER_SOCKET);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mServerSocket = server;
mLooperAccept = true;
}
public void run() {
while(mLooperAccept){
Log.i(TAG, "run mServerSocket = " + mServerSocket);
if(mServerSocket!=null){
try {
mLocalSocket = mServerSocket.accept();
mSocketIS = mLocalSocket.getInputStream();
mSocketOS = mLocalSocket.getOutputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1, total = 0, avail = 0;

boolean isPrint = false;
mStartAddress = true;
Log.d(TAG,"byte[] buffer ="+buffer);
Log.d(TAG,"byte[] buffer ToHexString ="+bytesToHexString(buffer));
Log.d(TAG,"byte[] buffer.length ="+buffer.length);
Log.d(TAG,"ByteArrayOutputStream bos ="+bos);
Log.d(TAG,"ByteArrayOutputStream bos Arrays.toString="+ Arrays.toString(bos.toByteArray()));
Log.d(TAG,"ByteArrayOutputStream bos toByte ="+bos.toByteArray());
Log.d(TAG,"ByteArrayOutputStream bos toByteArray tohexstring ="+bytesToHexString(bos.toByteArray()));
while(mLooperAccept){
Log.i(TAG, "mLooperAccept 11111 ");
if(avail == 0 && isPrint){
Log.i(TAG, "mLooperAccept 2222 ");
doSendRawData(bos.toByteArray());
isPrint = false;
total = 0;
Arrays.fill(buffer, (byte)0);
bos.reset();
}

if((len=mSocketIS.read(buffer))!=-1){
bos.write(buffer,0,len);
Log.i(TAG, "mLooperAccept 5555 ");
total += len;
for(int i = 0; i < 50; i++){
avail = mSocketIS.available();
if(avail != 0){
break;
}
SystemClock.sleep(10);
}

}else{
doSendRawData(bos.toByteArray());
Log.i(TAG, "mLooperAccept 4444 ");
break;
}
if(total >= 10 || !mStartAddress){
isPrint = true;
}
}
bos.close();
mSocketIS.close();
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
}
}
}
}

public void cancel() {
Log.i(TAG, "cancel");
try {
mLooperAccept = false;
if(mServerSocket!=null){
mServerSocket.close();
}
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}

public void doSendRawData(final byte[] data){
if(mStartAddress && data!=null&&data.length>=10){
final int LENGTH = data.length - 10;
System.arraycopy(data,0,mAddressBytes,0,6);
System.arraycopy(data,6,mUidBytes,0,4);
mAddressString = Utils.getAddressStringFromByte(mAddressBytes);
mUidInteger = Utils.byteArrayToInt(mUidBytes);
Log.d(TAG, "AcceptThread run address="+mAddressString);
Log.d(TAG, "AcceptThread run uid="+mUidInteger);
Log.w("wenjie.gu","data =="+data);
Log.w("wenjie.gu","data =="+data.toString());
Log.w("wenjie.gu","data to string =="+data.toString());
Log.w("wenjie.gu","data bytetohex =="+bytesToHexString(data));
if(PRINTER_ADDRESS.equals(mAddressString)&&LENGTH>0){
byte[] printData = new byte[LENGTH];
System.arraycopy(data,10,printData,0,LENGTH);
Log.d(TAG, "AcceptThread run printData="+ printData);
mStartAddress = false;
if(data.length>=4){
handleData(data);

}
}else if(PRINTER_ADDRESS.equals(mAddressString)){
Log.d(TAG, "LENGTH < 0 ");
mStartAddress = false;
}
}else{
Log.d(TAG, "data.length = " + data.length);
Log.d("wenjie.gu", "data.length = " + data.length);
Log.w("wenjie.gu","data1 =="+data);
Log.w("wenjie.gu","data1 to string "+data.toString());
Log.w("wenjie.gu","data1 bytetohex =="+bytesToHexString(data));
if(data.length>=4){
handleData(data);
}
}
}
}

public void handleData (byte[] data){
mPosPrintUtil.handleData(data);
}
public static String bytesToHexString(byte[] bytes) {
if(bytes!=null&&bytes.length>0){
StringBuilder buffer = new StringBuilder(bytes.length * 2);
for(byte b : bytes) {
buffer.append(String.format("%02x", new Integer(b & 0xff))).append(" ");
}
return buffer.toString();
}
//*/
return "";
}


}



package com.android.bluetooth.print;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
public class ByteUtil {

private static final String TAG="ByteUtil";

public static Bitmap stringtoBitmap(String string) {
//将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

public static Integer bytetoInteger(byte[] data) {
String content = new String(data);
Integer length = null;
if(content != null) {
length =Integer.valueOf(content);
}
Log.d(TAG, "length = " + length);
return length;
}

public static int bytetoint(byte[] data) {
String content = new String(data);
int id = Integer.parseInt(content);
Log.d(TAG, "id = " + id);
return id;
}

public static byte[] byteSplit1(byte[] byte_3) {
byte[] byt_1 = new byte[4];
System.arraycopy(byte_3, 0, byt_1, 0, 4);
return byt_1;
}

public static byte[] byteSplit2(byte[] byte_3) {
byte[] byt_2 = new byte[byte_3.length-4];
System.arraycopy(byte_3, 4, byt_2, 0, byte_3.length-4);
return byt_2;
}

public static int getCommandType(byte[] data) {
Log.w("wenjie.gu","data[0]="+data[0]);
Log.w("wenjie.gu","data[1]="+data[1]);
Log.w("wenjie.gu","data[2]="+data[2]);
Log.w("wenjie.gu","data[3]="+data[3]);
Log.w("wenjie.gu","data[4]="+data[4]);
Log.w("wenjie.gu","data[5]="+data[5]);
Log.w("wenjie.gu","data[6]="+data[6]);
Log.w("wenjie.gu","data[7]="+data[7]);
Log.w("wenjie.gu","data[8]="+data[8]);
int type = 0;
if(data[0] ==1 && data[1] ==1 && data[2] ==1 && data[3] ==1) {
type = 1;
} else if(data[0] ==2 && data[1] ==2 && data[2] ==2 && data[3] ==2) {
type = 2;
} else if(data[0] ==3 && data[1] ==3 && data[2] ==3 && data[3] ==3) {
type = 3;
} else if(data[0] ==4 && data[1] ==4 && data[2] ==4 && data[3] ==4) {
type = 4;
} else if(data[0] ==5 && data[1] ==5 && data[2] ==5 && data[3] ==5) {
type = 5;
} else if(data[0] ==6 && data[1] ==6 && data[2] ==6 && data[3] ==6) {
type = 6;
} else if(data[0] ==7 && data[1] ==7 && data[2] ==7 && data[3] ==7) {
type = 7;
} else if(data[0] ==8 && data[1] ==8 && data[2] ==8 && data[3] ==8) {
type = 8;
} else if(data[0] ==9 && data[1] ==9 && data[2] ==9 && data[3] ==9) {
type = 9;
} else if(data[0] ==10 && data[1] ==10 && data[2] ==10 && data[3] ==10) {
type = 10;
} else if(data[0] ==11 && data[1] ==11 && data[2] ==11 && data[3] ==11) {
type = 11;
} else if(data[0] ==29 && data[1] ==118 && data[2] ==48 && data[3] ==0) {
type = 12;
} else {
type = 0;
}
Log.d(TAG, "type = " + type);
return type;
}
}


 
posted @ 2023-01-06 13:58  一颗苹果!!  阅读(65)  评论(0)    收藏  举报