上一篇文章重点介绍了android中蓝牙的开启配对连接及通信的相关知识,本文介绍连接打印机实现打印并格式化的方案,前提便是开启蓝牙并配对连接成功。
1.实现字符串的打印
private boolean isConnection = false;
//上文的连接代码
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
bluetoothSocket.connect();
if(device.getBondState()==BluetoothDevice.BOND_BONDED) {
outputStream = bluetoothSocket.getOutputStream();
isConnection=true;
}
/**
* 发送数据
*/
private void send(String sendData){
if(isConnection) {
System.out.println("开始打印!!");
try {
byte[] data = sendData.getBytes("gbk");
outputStream.write(data,0,data.length);
outputStream.flush();
} catch (IOException e) {
Toast.makeText(PrintDataActivity.this,"发送失败!",Toast.LENGTH_SHORT)
.show();
}
} else {
Toast.makeText(PrintDataActivity.this,"设备未连接,请重新连接!",Toast.LENGTH_SHORT)
.show();
}
}
2.实现格式化打印
本文提供一种样式的代码。其实样式的实现无非就是计算起始点的位置而已,至于其中的数据可以通过循环或者反射的方式放进去,如果有疑问或者需要其他的样式可留言。
private static final int LINE_BYTE_SIZE = 32;// 打印纸一行最大的字节
private static final String SEPARATOR = "$";//分隔符
private static StringBuffer sb = new StringBuffer();
/**
* 排版居中标题
* @param title
* @return
*/
public static String printTitle(String title){
sb.delete(0, sb.length());
//前面一半用空格填充
for(int i=0;i<(LINE_BYTE_SIZE - getBytesLength(title))/2;i++){
sb.append(" ");
}
sb.append(title);
return sb.toString();
}
/**
* 获取最大长度
* @param msgs
* @return
*/
private static int getMaxLength(Object[] msgs){
int max = 0;
int tmp;
for(Object oo : msgs){
tmp = getBytesLength(oo.toString());
if(tmp>max){
max = tmp;
}
}
return max;
}
/**
* 获取数据长度
* @param msg
* @return
*/
private static int getBytesLength(String msg){
return msg.getBytes(Charset.forName("GB2312")).length;
}
/**
* 打印订餐单 (左中右对称)
*
* 例: 菜名 数量 单价
* 豆沙包 1 3.0
* 蛋 1 1.5
* 包子 3 11.0
* 土豆 2 4.1
* @param menuMsgMap Key为餐次 Value为 菜谱字符串数组 格式为:豆沙包$数量$单价
* @return
*/
public static String printMenuMSG(LinkedHashMap<String,LinkedList<String>> menuMsgMap){
sb.delete(0, sb.length());
String menus[] = null;
List<String> menuNames = new ArrayList<String>();
List<String> menuPrices = new ArrayList<String>();
for(List<String> strList : menuMsgMap.values()){
for(String str : strList){
if(str.contains(SEPARATOR)){
menus = str.split("["+SEPARATOR+"]");
if(menus!=null && menus.length!=0){
menuNames.add(menus[0]);
menuPrices.add(menus[2]);
}
}
}
}
String menuNameTxt = "菜名";
String numTxt = "数量";
String priceTxt = "单价\n";
int leftPrefixLength = getMaxLength(menuNames.toArray());
int rightPrefixLength = getMaxLength(menuPrices.toArray());
if(rightPrefixLength<getBytesLength(priceTxt))
rightPrefixLength = getBytesLength(priceTxt);
int leftMiddleNameLength = (leftPrefixLength-getBytesLength(menuNameTxt))/2;
for(int i=0;i<leftMiddleNameLength;i++){
sb.append(" ");
}
sb.append(menuNameTxt);
int middleNumTxtLength = (LINE_BYTE_SIZE - leftPrefixLength - rightPrefixLength - getBytesLength(numTxt))/2;
for(int i=0;i<middleNumTxtLength+leftMiddleNameLength;i++){
sb.append(" ");
}
sb.append(numTxt);
int middlePriceTxtLength = (rightPrefixLength-getBytesLength(priceTxt))/2;
for(int i=0;i<middleNumTxtLength+middlePriceTxtLength;i++){
sb.append(" ");
}
sb.append(priceTxt);
for(Entry<String,LinkedList<String>> entry : menuMsgMap.entrySet()){
if(!"".equals(entry.getKey()))
sb.append(entry.getKey()+"\n");
for(String menu : entry.getValue()){
if(menu.contains(SEPARATOR)){
menus = menu.split("["+SEPARATOR+"]");
if(menus!=null && menus.length!=0){
sb.append(menus[0]);
for(int i=0;i<(leftPrefixLength - getBytesLength(menus[0]) + middleNumTxtLength + getBytesLength(numTxt)/2 -1);i++){
sb.append(" ");
}
sb.append(menus[1]);
for(int i=0;i<(middleNumTxtLength+getBytesLength(numTxt)/2 + 1 -getBytesLength(menus[1])+middlePriceTxtLength);i++){
sb.append(" ");
}
sb.append(menus[2]+"\n");
}
}else{ //不包含分隔符 直接打印
for(int i=0;i<LINE_BYTE_SIZE/getBytesLength(menu)-getBytesLength("\n");i++){
sb.append(menu);
}
sb.append("\n");
}
}
}
return sb.toString();
}
至此,关于android连接打印机实现格式化打印的知识介绍完毕了,如有疑问欢迎留言。