Android蓝牙开发(二)

前言
在之前的两篇博客中简单介绍了蓝牙协议中的一些Api和利用蓝牙协议来实现一个简单聊天功能,不清楚的可以看看这两篇文章,文章一:蓝牙API,文章二:利用蓝牙实现简单的聊天功能,在有了之前的基础的情况下,来了解一下怎么样利用蓝牙实现连接打印机并且实现打印小票这个功能。
第一步:权限
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
第二步:检测是否打开蓝牙
获取蓝牙适配器来判断是否打开蓝牙,这里我就不贴代码了,上一篇文章中有详细的介绍怎么样利用适配器BluetoothAdapter来检测是否打开蓝牙的步骤
第三步:连接
连接的时候需要注意要先搜索附近的蓝牙设备才能连接,具体操作上一篇文章中也有介绍
第四步:
指定文字打印格式:先贴出代码

public class TextFormatUtil {

	/**
 * 打印纸一行最大的字节
 */
private static final int DEFAULT_LINE_BYTE_SIZE = 35;

private int lineByteSize = DEFAULT_LINE_BYTE_SIZE;
/**
 * 分隔符
 */
private static final String SEPARATOR = "$";

private static StringBuffer sb = new StringBuffer();
/**
 * 行元素集合
 */
private final List<TextWeightBean> lineElements = new ArrayList<TextWeightBean>();

/**
 * 排版居中标题
 *
 * @param title
 * @return
 */
public String getLineTitle(String title) {
	sb.delete(0, sb.length());
	for (int i = 0; i < (lineByteSize - getBytesLength(title)) / 2; i++) {
		sb.append(" ");
	}
	sb.append(title);
	sb.append("\n");
	return sb.toString();
}
/**
 * 根据比重打印文字,全部文字居左对齐
 *
 * @return
 */
public String getLineTextAccordingWeight(List<TextWeightBean> list) {
	sb.delete(0, sb.length());
	float totalWeight = 0;
	for (int i = 0; i < list.size(); i++) {
		totalWeight += list.get(i).getWeight();
	}
	for (int i = 0; i < list.size(); i++) {
		TextWeightBean textWeightBean = list.get(i);
		String showText = textWeightBean.getText();
		int holdSize = (int) (textWeightBean.getWeight() / totalWeight * lineByteSize);
		showText = formatText(showText, holdSize);
		sb.append(showText);
	}
	sb.append("\n");
	return sb.toString();
}
/**
 * 显示的文字格式化
 * @param showText
 * @param holdSize
 * @return
 */
private String formatText(String showText, int holdSize) {
	int textSize = getBytesLength(showText);
	if (textSize > holdSize) {
		showText = subText(showText, holdSize);
	} else {
		for (int j = 0; j < holdSize - textSize; j++) {
			showText += " ";
		}
	}
	return showText;
}
private String subText(String showText, int holdSize) {
	int size = 0;
	int index = 0;
	int symbolLength = "..".getBytes(Charset.forName("GB2312")).length;
	for (int j = 0; j < showText.length(); j++) {
		String c = showText.substring(j, j + 1);
		size += c.getBytes(Charset.forName("GB2312")).length;
		index = j;
		if (size > holdSize - symbolLength) {
			break;
		}
	}
	showText = showText.substring(0, index) + "..";

	return formatText(showText, holdSize);
}	
/**
 * 获取数据长度
 * @param msg
 * @return
 */
@SuppressLint("NewApi")
private static int getBytesLength(String msg) {
	if (msg == null) {
		return "null".getBytes(Charset.forName("GB2312")).length;
	}
	return msg.getBytes(Charset.forName("GB2312")).length;
}
/**
 * 获取最大长度
 * @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;
}

}

第四步:指定图片格式
指定图片格式这一步骤在前面一篇文章中有介绍相对应得格式,不了解的可以看看这篇文章http://blog.csdn.net/qq_34942689/article/details/72820226

第五步:打印

private void sendMessage(String message) {
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothService to write
        byte[] send;
        try {
            send = message.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            send = message.getBytes();
        }
        bluetoos.getService(this).write(send);
    }

最后在你需要打印的地方调用sendMessage方法就可以实现打印功能。
以上就是整个利用蓝牙实现连接打印机实现打印小票的功能,有不清楚的地方或者有什么更好的建议欢迎留言,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值