蓝牙打印:之前在做项目时,遇到要做手机端的蓝牙打印,其实就是手机端结合热敏打印机,通过蓝牙的连接,进行数据的传输,最终打印出所需要的小票。
POS机打印:POS机的打印也是结合硬件设备,但是与手机端的不同之处在于,POS硬件支持打印功能,一般情况下,不同的厂商制造出的POS机的打印方式是不同的。我在做项目时,遇到了两种POS的打印方式。这两种打印方式的共同点在于都是跨进程通信,调用该硬件提供的SDK的打印方式去进行打印。
语音播报:Jpush和讯飞的结合使用,进行语音播报,类似于现在支付宝收款成功之后的语音。(但是实现方式肯定是不一样的)。
本文主要介绍:蓝牙打印
Talk is cheap ,show me the code.
android蓝牙打印demo源码
结合之前的demo(包括打印图片)
步骤一: 如图所示,导入printerlibs.jar包。
步骤二:复制相关类文件,以及资源文件等。
步骤三:初始化蓝牙
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
//初始化蓝牙
initBlePrint();
btn_connect_printer= (Button) findViewById(R.id.btn_connect_printer);
btn_printer_text= (Button) findViewById(R.id.btn_printer_text);
btn_printer_text.setOnClickListener(this);
btn_connect_printer.setOnClickListener(this);
}
private void initBlePrint() {
// 初始化字符串资源
InitGlobalString();
mHandler = new MHandler(MainActivity.this);
WorkService.addHandler(mHandler);
if (null == WorkService.workThread) {
intent = new Intent(MainActivity.this, WorkService.class);
startService(intent);
}
}
private void InitGlobalString() {
Global.toast_success = getString(R.string.toast_success);
Global.toast_fail = getString(R.string.toast_fail);
Global.toast_notconnect = getString(R.string.toast_notconnect);
Global.toast_usbpermit = getString(R.string.toast_usbpermit);
}
static class MHandler extends Handler {
WeakReference<MainActivity> mActivity;
MHandler(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
MainActivity theActivity = mActivity.get();
switch (msg.what) {
}
}
}
步骤四:获取已经匹配成功的蓝牙设备
/**
* 得到已经匹配的蓝牙的list集合
* @return
*/
private List<Map<String, Object>> getBoundedPrinters() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
return list;
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a
// ListView
Map<String, Object> map = new HashMap<String, Object>();
map.put(ICON, android.R.drawable.stat_sys_data_bluetooth);
// Toast.makeText(this,
// ""+device.getBluetoothClass().getMajorDeviceClass(),
// Toast.LENGTH_LONG).show();
map.put(PRINTERNAME, device.getName());
map.put(PRINTERMAC, device.getAddress());
list.add(map);
}
}
return list;
}
步骤五:连接蓝牙打印机设备
/**
* 初始化RecyclerView
*/
private void initRecyclerView() {
rvSettingconnect.setLayoutManager(new LinearLayoutManager(this));
// 创建RecyclerView的数据适配器
adapter = new BleDevicesRecyclerViewAdapter(this);
//设置分割线
rvSettingconnect.addItemDecoration(new DividerItemDecoration(this, VERTICAL_LIST));
// 设置RecyclerView的数据适配器(适配器包装)
rvSettingconnect.setAdapter(adapter);
if (null != boundedPrinters) {
adapter.setDatas(boundedPrinters);
}
//点击item连接蓝牙打印机
adapter.setOnItemClickListener(new BleDevicesRecyclerViewAdapter.OnItemClickListener() {
@Override
public void OnItemClick(View view, int position, Map<String, Object> data) {
String address = (String) boundedPrinters.get(position).get(PRINTERMAC);
String name = (String) boundedPrinters.get(position).get(PRINTERNAME);
dialog.setMessage(Global.toast_connecting + " " + name);
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
if (WorkService.workThread != null) {
WorkService.workThread.connectBt(address);
} else {
Toast.makeText(ConnectBTPairedActivity.this, "WorkService.workThread is null", Toast.LENGTH_SHORT).show();
}
}
});
}
步骤六:连接成功之后,进行打印
/**
* 打印dialog
*/
Dialog dialog;
private void toDialogActivity() {
View view = LayoutInflater.from(context).inflate(R.layout.print_dialog, null);
// 对话框
dialog = new Dialog(context);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
// 设置宽度为屏幕的宽度
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = (int) (display.getWidth()); // 设置宽度
dialog.getWindow().setAttributes(lp);
dialog.getWindow().setContentView(view);
Button btn_cancle_schexiao = (Button) view.findViewById(R.id.btn_cancle_schexiao);
Button btn_ok_schexiao = (Button) view.findViewById(R.id.btn_ok_schexiao);
btn_cancle_schexiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//取消打印
dialog.hide();
}
});
btn_ok_schexiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.hide();
printText();
}
});
}
String payMethod = "对账单";
public void printText() {
// 不要直接和Pos打交道,要通过workThread来交流
if (WorkService.workThread.isConnected()) {
String text = "null";
text = "=============================" + "\n"
+ "金额:" + "12.5元" + "\n"
+ "状态:" + "给我退款" + "\n"
+ "用户:" + "Sagelu" + "\n"
+ "时间:" + "2017-06-06" + "\n"
+ "人物:" + "RNG" + "\n"
+ "编号:" + "1231521591596" + "\n"
+ "金额:" + "50W" + "\n"
+ "时间:" + "2012-06-23" + "\n"
+ "-----------------------------" + "\n"
+ "此小票不需要顾客签字"
+ "\r\n\r\n\r\n"; // 加三行换行,避免走纸
byte header[] = null;
byte strbuf[] = null;
// 设置GBK编码
// Android手机默认也是UTF8编码
header = new byte[]{0x1b, 0x40, 0x1c, 0x26, 0x1b, 0x39,
00};
try {
strbuf = text.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//打印订单头部--字体大小以及居中sage...
String headText = "付款" + "\r\n\r\n";
Bundle dataAlign = new Bundle();
dataAlign.putInt(Global.INTPARA1, 1);
Bundle dataTextOut = new Bundle();
dataTextOut.putString(Global.STRPARA1, headText);
dataTextOut.putString(Global.STRPARA2, "GBK");
dataTextOut.putInt(Global.INTPARA1, 0);
dataTextOut.putInt(Global.INTPARA2, 1);
dataTextOut.putInt(Global.INTPARA3, 1);
WorkService.workThread.handleCmd(Global.CMD_POS_SALIGN,
dataAlign);
WorkService.workThread.handleCmd(Global.CMD_POS_STEXTOUT,
dataTextOut);
//打印订单详情body
byte buffer[] = DataUtils.byteArraysToBytes(new byte[][]{
header, strbuf});
Bundle data = new Bundle();
data.putByteArray(Global.BYTESPARA1, buffer);
data.putInt(Global.INTPARA1, 0);
data.putInt(Global.INTPARA2, buffer.length);
WorkService.workThread.handleCmd(Global.CMD_POS_WRITE, data);
} else {
Toast.makeText(context, Global.toast_notconnect,
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, ConnectBTPairedActivity.class);
startActivity(intent);
// toDialog();
}
}
其实也就是基于printerlibs.jar包进行的蓝牙打印开发。难度不是很大,其中的打印原理等内容封装在jar中。
之后会写POS机的打印方式。
网友评论