技术分享

记录踩过的坑和别的大佬没有叽歪的点

开始做蓝牙打印看到网上很多做混合开发 和安卓开发,ios开发的例子,插件等等版本大同小异,并不是像其他博客上所说的那么简单,[下载插件,无需改动,连接打印开始,图片为证],没那么简单.往往困扰我们没有进行下去的是细节,那么

为了能让小伙伴们少走弯路我来分享一下吧,

这个插件很多 随便下载一个 列表,连接,打印 断连 这四个功能

蓝牙打印插件下载

插件放在  软件 目录 WeX5_V3.8\model\Native\plugins  下

插件内java文件可以手动修改 根据自己的需要, 有的需要 改写蓝牙连接功能, new一个线程去连接,我这里没有new,连接需要时间, 可能会报错 read failed,socket might closed or timeout,read ret:-1

java

package cordova.plugin.bluetooth.printer;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Set;
import java.util.UUID;import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;import android.content.Context;public class BluetoothPrinter extends CordovaPlugin {private static final String LOG_TAG = "BluetoothPrinter";BluetoothAdapter mBluetoothAdapter;BluetoothSocket mmSocket;BluetoothDevice mmDevice;OutputStream mmOutputStream;InputStream mmInputStream;Thread workerThread;byte[] readBuffer;int readBufferPosition;int counter;volatile boolean stopWorker;public BluetoothPrinter() {}@Overridepublic boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {if (action.equals("list")) {listBT(callbackContext);////return true;} else if (action.equals("connect")) {String name = args.getString(0);if (findBT(callbackContext, name)) {try {connectBT(callbackContext);} catch (IOException e) {Log.e(LOG_TAG, e.getMessage());e.printStackTrace();}} else {callbackContext.error("Bluetooth Device Not Found: " + name);}return true;} else if (action.equals("disconnect")) {try {disconnectBT(callbackContext);} catch (IOException e) {Log.e(LOG_TAG, e.getMessage());e.printStackTrace();}return true;}else if (action.equals("print")) {try {String msg = args.getString(0);print(callbackContext, msg);} catch (IOException e) {Log.e(LOG_TAG, e.getMessage());e.printStackTrace();}return true;}else if (action.equals("printPOSCommand")) {try {String msg = args.getString(0);printPOSCommand(callbackContext, hexStringToBytes(msg));} catch (IOException e) {Log.e(LOG_TAG, e.getMessage());e.printStackTrace();}return true;}return false;}//This will return the array list of paired bluetooth printersvoid listBT(CallbackContext callbackContext) {BluetoothAdapter mBluetoothAdapter = null;String errMsg = null;try {mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter == null) {errMsg = "No bluetooth adapter available";Log.e(LOG_TAG, errMsg);callbackContext.error(errMsg);return;}if (!mBluetoothAdapter.isEnabled()) {Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);this.cordova.getActivity().startActivityForResult(enableBluetooth, 0);}Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();if (pairedDevices.size() > 0) {JSONArray json = new JSONArray();for (BluetoothDevice device : pairedDevices) {/*Hashtable map = new Hashtable();map.put("type", device.getType());map.put("address", device.getAddress());map.put("name", device.getName());JSONObject jObj = new JSONObject(map);*/json.put(device.getName());}callbackContext.success(json);} else {callbackContext.error("No Bluetooth Device Found");}//Log.d(LOG_TAG, "Bluetooth Device Found: " + mmDevice.getName());} catch (Exception e) {errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}}// This will find a bluetooth printer deviceboolean findBT(CallbackContext callbackContext, String name) {try {mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter == null) {Log.e(LOG_TAG, "No bluetooth adapter available");}if (!mBluetoothAdapter.isEnabled()) {Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);this.cordova.getActivity().startActivityForResult(enableBluetooth, 0);}Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();if (pairedDevices.size() > 0) {for (BluetoothDevice device : pairedDevices) {if (device.getName().equalsIgnoreCase(name)) {mmDevice = device;return true;}}}Log.d(LOG_TAG, "Bluetooth Device Found: " + mmDevice.getName());} catch (Exception e) {String errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}return false;}// Tries to open a connection to the bluetooth printer deviceboolean connectBT(CallbackContext callbackContext) throws IOException {try {// Standard SerialPortService IDUUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);//String cmd="Qsprinter";//PrintService.pl.connect(cmd);mmSocket.connect();mmOutputStream = mmSocket.getOutputStream();mmInputStream = mmSocket.getInputStream();beginListenForData();//Log.d(LOG_TAG, "Bluetooth Opened: " + mmDevice.getName());callbackContext.success("Bluetooth Opened: " + mmDevice.getName());//callbackContext.success("Bluetooth Opened: " + "ceshi_shebei");return true;} catch (Exception e) {String errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}return false;}// After opening a connection to bluetooth printer device, // we have to listen and check if a data were sent to be printed.void beginListenForData() {try {final Handler handler = new Handler();// This is the ASCII code for a newline characterfinal byte delimiter = 10;stopWorker = false;readBufferPosition = 0;readBuffer = new byte[1024];workerThread = new Thread(new Runnable() {public void run() {while (!Thread.currentThread().isInterrupted() && !stopWorker) {try {int bytesAvailable = mmInputStream.available();if (bytesAvailable > 0) {byte[] packetBytes = new byte[bytesAvailable];mmInputStream.read(packetBytes);for (int i = 0; i < bytesAvailable; i++) {byte b = packetBytes[i];if (b == delimiter) {byte[] encodedBytes = new byte[readBufferPosition];System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);/*final String data = new String(encodedBytes, "US-ASCII");readBufferPosition = 0;handler.post(new Runnable() {public void run() {myLabel.setText(data);}});*/} else {readBuffer[readBufferPosition++] = b;}}}} catch (IOException ex) {stopWorker = true;}}}});workerThread.start();} catch (NullPointerException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}//This will send data to bluetooth printerboolean print(CallbackContext callbackContext, String msg) throws IOException {try {//mmOutputStream.write(new byte[] { 0x1b, 0x74,0x15 });//后添加//mmOutputStream.write(msg.getBytes());mmOutputStream.write(msg.getBytes("GBK"));//防止中文打印乱码//mmOutputStream.write(new byte[] { 0x1d, 0x0c });//后添加/*String message="20160825888";if (message.length() > 0) {byte[] btdata=null;try {btdata=message.getBytes("ASCII");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}//Enable the barcodemmOutputStream.write(new byte[]{0x1d,0x45,0x43,0x01});//Set the barcode height is 162mmOutputStream.write(new byte[]{0x1d,0x68,(byte) 0xa2});//Set HRI character print location on bottommmOutputStream.write(new byte[]{0x1d,0x48,0x02});mmOutputStream.write(new byte[]{0x1d,0x77,0x02});//Print the barcode use code128byte[] qrHead=new byte[]{0x1d,0x6b,0x49,(byte) btdata.length};
//              byte[] qrHead=new byte[]{0x1d,0x6b,0x44,(byte) btdata.length};byte[] barCodeData=new byte[qrHead.length+btdata.length];System.arraycopy(qrHead, 0, barCodeData, 0, qrHead.length);System.arraycopy(btdata, 0, barCodeData, qrHead.length, btdata.length);mmOutputStream.write(barCodeData);mmOutputStream.write(new byte[] { 0x1d, 0x0c });//mmOutputStream.printText("\r\n");//               Bitmap btMap = BarcodeCreater.creatBarcode(PrintBarCodeActivity.this,
//                      message, 384, 100, true, 1);
//              PrintService.pl.printImage(btMap);
//              PrintService.pl.write(new byte[] { 0x1d, 0x0c });}//结束*///mmOutputStream.write(new byte[] { 0x1b, 0x74,0x15 });////mmOutputStream.write(msg.getBytes("UTF-8"));//mmOutputStream.write(new byte[] { 0x1d, 0x0c });///*mmOutputStream.write(new byte[] { 0x1b, 0x74,0x15 });////mmOutputStream.write(msg.getBytes());mmOutputStream.write(msg.getBytes("GBK"));//防止中文打印乱码mmOutputStream.write(new byte[] { 0x1d, 0x0c });//*/// tell the user data were sent//Log.d(LOG_TAG, "Data Sent");callbackContext.success("Data Sent success");return true;} catch (Exception e) {String errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}return false;}boolean printPOSCommand(CallbackContext callbackContext, byte[] buffer) throws IOException {try {mmOutputStream.write(buffer);// tell the user data were sentLog.d(LOG_TAG, "Data Sent");callbackContext.success("Data Sent");return true;} catch (Exception e) {String errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}return false;}// disconnect bluetooth printer.boolean disconnectBT(CallbackContext callbackContext) throws IOException {try {stopWorker = true;mmOutputStream.close();mmInputStream.close();mmSocket.close();callbackContext.success("Bluetooth Disconnect");return true;} catch (Exception e) {String errMsg = e.getMessage();Log.e(LOG_TAG, errMsg);e.printStackTrace();callbackContext.error(errMsg);}return false;}public byte[] getText(String textStr) {// TODO Auto-generated method stubbyte[] send;byte[] send=null;try {send = textStr.getBytes("GBK");} catch (UnsupportedEncodingException e) {send = textStr.getBytes();}return send;}public static byte[] hexStringToBytes(String hexString) {hexString = hexString.toLowerCase();String[] hexStrings = hexString.split(" ");byte[] bytes = new byte[hexStrings.length];for (int i = 0; i < hexStrings.length; i++) {char[] hexChars = hexStrings[i].toCharArray();bytes[i] = (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1]));}return bytes;}private static byte charToByte(char c) {return (byte) "0123456789abcdef".indexOf(c);}}

js

var exec = require('cordova/exec');var BTPrinter = {list: function(fnSuccess, fnError){exec(fnSuccess, fnError, "BluetoothPrinter", "list", []);},connect: function(fnSuccess, fnError, name){exec(fnSuccess, fnError, "BluetoothPrinter", "connect", [name]);},disconnect: function(fnSuccess, fnError){exec(fnSuccess, fnError, "BluetoothPrinter", "disconnect", []);},print: function(fnSuccess, fnError, str){exec(fnSuccess, fnError, "BluetoothPrinter", "print", [str]);},printPOSCommand: function(fnSuccess, fnError, str){exec(fnSuccess, fnError, "BluetoothPrinter", "printPOSCommand", [str]);}
};module.exports = BTPrinter;

示例下载

示例下载

//蓝牙打印Model.prototype.btClick = function(event){debugger;var strCmd1 = "T 12 2 0 10 12号字体测试\n";//文本var strCmd2 = addCPCLQRCode(0,40,'M', 2, 5, "12号中文字二维码测试");//二维码         var strCmd3 = addCPCLBarCode(150,10,'128',50,0,1,1,'123456');//条形码var strCmd4 = "T 12 2 180 70 123456\n";//文本var byte2 = "! 0 200 200 300 1\n" //这是开始代码+strCmd1+strCmd2+strCmd3+strCmd4+"PRINT\n";//这是结束代码//蓝牙打印if( this.blueConect) {var packageID = this.packageID;BTPrinter.print ( function (data){console.log ( "success" ) ;console.log (packageID) ;alert (data) ;},function(err){console.log ( "Error") ;console.log (err);alert (err) ;}, byte2);}};

最后说几个问题

图一

 连接蓝牙之前需要用手机的蓝牙模块先连接蓝牙打印机  点击蓝牙列表>> 蓝牙连接(3秒左右会连接成功)>>打印

2

用手机连接蓝牙时也是我被坑的地方这个手没有出现 ble_name,但是不影响我阐述, 看图4图5 打印机有两个蓝牙信号 一个name[打印机信号名] 一个ble_name[打印机蓝牙信号名]

咱不知道为啥要两个 看图标一个是蓝牙标识,一个是打印机标识 两个名字只差一个L字母 开始我连接的是 带L的信号(蓝牙信号),不用配对密码可以直接连接,打印机也会显示连接成功的信号,

所以没在意一直以为是程序问题 功能上只有列表可以用,连接 打印都无法使用,就是因为当初输了1234没成功所以固执的认为这个是给配对用的参考,就没有输入0000,

其实0000才是配对密码,打印不需要输入配对是默认的,只要在手机一方输入即可配对成功,后来才知道要用带打印机图标的信号,程序没有问题,是信号选择的问题.每一个细节都肯能困扰你打印失败,让你排错的方向走叉

3

4

图5

总结一下,1插件名错误,

2,修改插件要重新编译打包

3.cordova 基本插件引入

4.测试最好单开app 减少干扰项,后期排错方便

5.打印机说明书

6.打印机信号都试试

7.UI2 打包 网络 网速影响app

8.打印纸安装

9.打印距离

10.手机版本4.2以上.

11.蓝牙打印机蓝牙低功耗4.0以上

记着的就这么多,有问题的可以留言,其实功能很简单无非是一些细节没有控制好,知道了就简单的一逼

祝你们好运

WeX5 3.8开发工具之蓝牙打印(全流程记录不是最全,胜似最全)相关推荐

  1. 一起看 I/O | Android 开发工具最新更新

    作者 / Juan Sebastian Oviedo, Senior Product Manager 在今年的 Google I/O 大会上,我们为 Android Studio 的 Beta 和 C ...

  2. 微信小程序开发工具初次使用Git 记录一下

    微信开发工具版本号: 这里只是简单记录一下开发工具内的git使用. 首先是点击微信开发者工具内的版本管理(如图): 会出现如下界面,然后点击初始化仓库,然后点击确定. 然后直接点击设置 去添加你的远程 ...

  3. Android打印机--蓝牙打印

    关于Android蓝牙打印和网络打印,其实都是利用Socket通信机制,只是蓝牙打印将socket做了一层封装BluetoothSocket ,打印的数据都是以流的方式进行传输或保存的,程序需要数据的 ...

  4. H5 App开发工具 WeX5

    WeX5是H5 App开发工具,Apache开源,免费开放所有代码,所开发的应用均能"一秒打开"!对跨平台多前端应用开发的支持极好,一次开发,多平台运行 . WeX5采用混合应用( ...

  5. wex5 转换 html5,WeX5开发工具(html5 app开发框架)V3.9 开源版

    WeX5开发工具(html5 app开发框架)是一套免费开源的wex5应用快速开发框架平台.非常不错的html5开发工具,用来开发安卓.苹果.web.微信服务号等等,采用Apache源码,所有的代码都 ...

  6. 使用WEX5移动开发工具制作仿淘宝APP

    毕业设计-使用WEX5移动开发工具制作仿淘宝APP 系统设计的意义 本课题来源于对日常逛超市.购物的生活体验和指导老师的提示.本系统是基于 WeX5的仿淘宝App系统,它商家们提供了一个更广阔的商品推 ...

  7. 蓝牙开发工具市场现状-市场规模、市场份额、市场定位、产品类型以及发展规划

    辰宇信息咨询市场调研公司最近发布-<2022-2028全球与中国蓝牙开发工具市场调研报告> 内容摘要 本文重点分析在全球及中国有重要角色的企业,分析这些企业蓝牙开发工具产品的市场规模.市场 ...

  8. Wex5 开发工具及相关插件安装

    一.Wex5简介 WeX5是跨端移动开发框架,将H5的标签封装成组件,实现可视化.组件化快速开发.实现一次开发,多端(iOS.安卓和微信)运行.前端采用H5 + CSS3 + JS标准,使用AMD规范 ...

  9. 【分享】html5 开发工具——WeX5中的各种绑定方式

    今天整理一下 html5 开发工具--WeX5中的各种绑定方式,下面分为表现类.流程类.交互类 3 种类型分别介绍. 表现类绑定 表现类的绑定属性有visible.text.html.css.styl ...

最新文章

  1. Excel实用小技巧
  2. Real-Time-Voice-Cloning的使用教程
  3. 绝对好文:嵌入式系统的软件架构设计!
  4. mysql windows软件_windows版MySQL软件的安装
  5. Docker拉取RabbitMQ镜像运行启动
  6. 普通人买得到国债吗?
  7. IOS判断用邮箱登录验证是不是合法的方法
  8. 开源的49款Java 网络爬虫软件
  9. Oracle将CLOB字段类型转为Varchar2类型
  10. ARM汇编程序——加法
  11. Quartus-II入门(全加器)
  12. matlab矩阵与常数相乘,矩阵与常数的运算.ppt
  13. 最简单的 UE 4 C++ 教程 —— 扫描多线轨迹【十六】
  14. 计算机英语boot,电脑开不了机显示英文boottmgr
  15. (一)Python基础语法
  16. 2019---高考加油!!!
  17. word中添加背景色
  18. 铸铁的弹性模量和泊松比_常用材料弹性模量和泊松比.docx
  19. ​【原创】基于SSM的校园二手物品交易商城(毕业设计源代码)
  20. 3dmax,查看场景中所有材质

热门文章

  1. Hadoop解决内存受限问题
  2. 使用sqlldr导入文本数据到oracle
  3. SpringIOC容器-对象依赖
  4. 深入理解Oracle的并行操作【好文认真读】
  5. mysql blob 比较_与MSSQL对比学习MYSQL的心得(四)--BLOB数据类型
  6. thinkphp回调的php调用db类,请问thinkphp中model类自动完成功能 回调函数能不能获取其他字段的值?...
  7. c语言指针++_C ++此指针| 查找输出程序| 套装1
  8. php多线程是什么意思,多线程是什么意思
  9. 保姆级教程,终于搞懂脏读、幻读和不可重复读了!(经典回顾)
  10. 面试官:重写 equals 时为什么一定要重写 hashCode?