第一步:首先需要一个蓝牙打印工具类
import android.bluetooth.BluetoothSocket;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import com.github.promeg.pinyinhelper.Pinyin;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
/**
-
蓝牙打印工具类
*/
public class PrintUtil {private OutputStreamWriter mWriter = null;
private OutputStream mOutputStream = null;public final static int WIDTH_PIXEL = 384;
public final static int IMAGE_SIZE = 320;
private static String myTime;/**
- 初始化Pos实例
- @param encoding 编码
- @throws IOException
*/
public PrintUtil(OutputStream outputStream, String encoding) throws IOException {
mWriter = new OutputStreamWriter(outputStream, encoding);
mOutputStream = outputStream;
initPrinter();
}
public void print(byte[] bs) throws IOException {
mOutputStream.write(bs);
}public void printRawBytes(byte[] bytes) throws IOException {
mOutputStream.write(bytes);
mOutputStream.flush();
}/**
- 初始化打印机
- @throws IOException
*/
public void initPrinter() throws IOException {
mWriter.write(0x1B);
mWriter.write(0x40);
mWriter.flush();
//获取当前时间
getTime();
}
/**
- 打印换行
- @return length 需要打印的空行数
- @throws IOException
*/
public void printLine(int lineNum) throws IOException {
for (int i = 0; i < lineNum; i++) {
mWriter.write("\n");
}
mWriter.flush();
}
/**
- 打印换行(只换一行)
- @throws IOException
*/
public void printLine() throws IOException {
printLine(1);
}
/**
- 打印空白(一个Tab的位置,约4个汉字)
- @param length 需要打印空白的长度,
- @throws IOException
*/
public void printTabSpace(int length) throws IOException {
for (int i = 0; i < length; i++) {
mWriter.write("\t");
}
mWriter.flush();
}
/**
- 绝对打印位置
- @return
- @throws IOException
*/
public byte[] setLocation(int offset) throws IOException {
byte[] bs = new byte[4];
bs[0] = 0x1B;
bs[1] = 0x24;
bs[2] = (byte) (offset % 256);
bs[3] = (byte) (offset / 256);
return bs;
}
public byte[] getGbk(String stText) throws IOException {
byte[] returnText = stText.getBytes(“GBK”); // 必须放在try内才可以
return returnText;
}private int getStringPixLength(String str) {
int pixLength = 0;
char c;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (Pinyin.isChinese©) {
pixLength += 24;
} else {
pixLength += 12;
}
}
return pixLength;
}public int getOffset(String str) {
return WIDTH_PIXEL - getStringPixLength(str);
}/**
- 打印文字
- @param text
- @throws IOException
*/
public void printText(String text) throws IOException {
mWriter.write(text);
mWriter.flush();
}
/**
- 对齐0:左对齐,1:居中,2:右对齐
*/
public void printAlignment(int alignment) throws IOException {
mWriter.write(0x1b);
mWriter.write(0x61);
mWriter.write(alignment);
}
public void printLargeText(String text) throws IOException {
mWriter.write(0x1b); mWriter.write(0x21); mWriter.write(48); mWriter.write(text); mWriter.write(0x1b); mWriter.write(0x21); mWriter.write(0); mWriter.flush();
}
public void printTwoColumn(String title, String content) throws IOException {
int iNum = 0;
byte[] byteBuffer = new byte[100];
byte[] tmp;tmp = getGbk(title); System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length); iNum += tmp.length; tmp = setLocation(getOffset(content)); System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length); iNum += tmp.length; tmp = getGbk(content); System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length); print(byteBuffer);
}
public void printThreeColumn(String left, String middle, String right) throws IOException {
int iNum = 0;
byte[] byteBuffer = new byte[200];
byte[] tmp = new byte[0];System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length); iNum += tmp.length; tmp = getGbk(left); System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length); iNum += tmp.length; int pixLength = getStringPixLength(left) % WIDTH_PIXEL; if (pixLength > WIDTH_PIXEL / 2 || pixLength == 0) { middle = "\n\t\t" + middle; } tmp = setLocation(192); System.arraycopy(tmp, 0, byteBuffer, iN