print.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <style lang="scss" scoped>
  2. .center{
  3. font-size: 32upx;
  4. padding: 30upx;
  5. .connected-title{
  6. text{color: #2079ff;}
  7. }
  8. .seek-btn{
  9. margin: 30upx 0;
  10. width: 60%;
  11. height: 80upx;
  12. color: #FFFFFF;
  13. background: #169bd5;
  14. border-radius: 20upx;
  15. }
  16. .ble-list{
  17. width: 65%;
  18. height: 80upx;
  19. text{color: #2079ff;}
  20. }
  21. }
  22. </style>
  23. <template>
  24. <view class="center">
  25. <view class="flex connected-title">已连接蓝牙:<text>{{nameBLE}}</text></view>
  26. <view class="flex-center seek-btn" @click="searchBle">搜索蓝牙打印机</view>
  27. <!--<button @click="searchBle">搜索蓝牙打印机</button>-->
  28. <!--<view style="margin-top: 30upx;" :key="index" v-for="(item, index) in devices">-->
  29. <view class="flex-space ble-list" :key="index" v-for="(item, index) in devices" @click="onConn(item)">
  30. {{item.name}} <text>连接</text>
  31. </view>
  32. <!--<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>-->
  33. <!--<textarea @blur="bindTextAreaBlur" auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText"/>-->
  34. <!--<button style="margin-top: 100upx;" @click="billPrint()">票据打印</button>-->
  35. </view>
  36. </template>
  37. <script>
  38. import BLE from "@/mixins/BLE";
  39. // var tsc = require("./js/gprint/tsc.js");
  40. // var esc = require("./js/gprint/esc.js");
  41. export default {
  42. mixins: [BLE],
  43. data() {
  44. return {
  45. // devices: [],
  46. // currDev: null,
  47. // connId: "",
  48. // piaojuText: "",
  49. // deviceId:'6CE888E4-6BF4-2D2A-60AE-07480F6A39A6',
  50. };
  51. },
  52. onLoad: function() {},
  53. // methods: {
  54. // destroyed: function() {
  55. // console.log("destroyed----------");
  56. // if (this.connId != "") {
  57. // uni.closeBLEConnection({
  58. // deviceId: this.connId,
  59. // success(res) {
  60. // console.log(res);
  61. // }
  62. // });
  63. // }
  64. // },
  65. // searchBle() {
  66. // var that = this;
  67. // uni.openBluetoothAdapter({
  68. // success(res) {
  69. // console.log(res,"打开 蓝牙模块");
  70. // that.onDevice();//监听搜索新设备
  71. // //获取本机蓝牙适配器状态。
  72. // uni.getBluetoothAdapterState({
  73. // success: function(res) {
  74. // console.log(res);
  75. // if (res.available) {
  76. // if (res.discovering) {
  77. // that.stopFindBule();
  78. // }
  79. // //搜索蓝牙
  80. // //开始搜寻附近的蓝牙外围设备
  81. // console.log("开始搜寻附近的蓝牙外围设备");
  82. // uni.startBluetoothDevicesDiscovery({
  83. // success(res) {
  84. // console.log(res);
  85. // }
  86. // });
  87. // } else {
  88. // console.log("本机蓝牙不可用");
  89. // }
  90. // }
  91. // });
  92. // }
  93. // });
  94. // },
  95. // onDevice() {
  96. // var that = this;
  97. // //监听寻找到新设备的事件
  98. // uni.onBluetoothDeviceFound(function(devices) {
  99. // console.log(devices,'设备列表');
  100. // var re = JSON.parse(JSON.stringify(devices));
  101. // let name = re.devices[0].name?re.devices[0].name:'未知设备';
  102. // let deviceId = re.devices[0].deviceId;
  103. // that.devices.push({name: name,deviceId: deviceId,services: []});
  104. // // that.zidonglianjie();
  105. // // if (name != "未知设备" && name != "") {
  106. // // let deviceId = re.devices[0].deviceId;
  107. // // that.devices.push({name: name,deviceId: deviceId,services: []});
  108. // // }
  109. // });
  110. // },
  111. // zidonglianjie(){
  112. // this.devices.forEach((ele)=>{
  113. // if(ele.deviceId==this.deviceId){
  114. // this.onConn(ele);
  115. // }
  116. // })
  117. // },
  118. // stopFindBule() {
  119. // console.log("停止搜寻附近的蓝牙外围设备---------------");
  120. // uni.stopBluetoothDevicesDiscovery({
  121. // success(res) {
  122. // console.log(res);
  123. // }
  124. // });
  125. // },
  126. // onConn(item) {
  127. // var that = this;
  128. // console.log("连接蓝牙---------------" + item.deviceId);
  129. // let deviceId = item.deviceId;
  130. // uni.createBLEConnection({
  131. // deviceId: deviceId,
  132. // complete(res) {
  133. // if (res.errMsg == "createBLEConnection:ok") {
  134. // uni.setStorageSync('BLE',item);
  135. // console.log("连接蓝牙-[" + item.name + "]--成功");
  136. // that.$msg("连接蓝牙-[" + item.name + "]--成功");
  137. // that.connId = deviceId;
  138. // that.currDev = item;
  139. // setTimeout(function() {
  140. // that.getBLEServices(deviceId);
  141. // }, 2000);
  142. // } else {
  143. // console.log(res);
  144. // }
  145. // //连接成功 关闭搜索
  146. // that.stopFindBule();
  147. // }
  148. // });
  149. // },
  150. //
  151. // getBLEServices(_deviceId) {
  152. // var that = this;
  153. // let deviceId = _deviceId;
  154. // console.log("获取蓝牙设备所有服务(service)");
  155. //
  156. // uni.getBLEDeviceServices({
  157. // // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  158. // deviceId: deviceId,
  159. // complete(res) {
  160. // console.log(res);
  161. // let serviceId = "";
  162. //
  163. // for (var s = 0; s < res.services.length; s++) {
  164. // console.log(res.services[s].uuid);
  165. // let serviceId = res.services[s].uuid;
  166. // uni.getBLEDeviceCharacteristics({
  167. // // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  168. // deviceId: deviceId,
  169. // // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  170. // serviceId: serviceId,
  171. // success(res) {
  172. // var re = JSON.parse(JSON.stringify(res));
  173. //
  174. // console.log(
  175. // "deviceId = [" + deviceId + "] serviceId = [" + serviceId + "]"
  176. // );
  177. // for (var c = 0; c < re.characteristics.length; c++) {
  178. // if (re.characteristics[c].properties.write == true) {
  179. // let uuid = re.characteristics[c].uuid;
  180. // console.log(
  181. // " deviceId = [" +
  182. // deviceId +
  183. // "] serviceId = [" +
  184. // serviceId +
  185. // "] characteristics=[" +
  186. // uuid
  187. // );
  188. //
  189. // for (var index in that.devices) {
  190. // if (that.devices[index].deviceId == deviceId) {
  191. // that.devices[index].services.push({
  192. // serviceId: serviceId,
  193. // characteristicId: uuid
  194. // });
  195. // break;
  196. // }
  197. // }
  198. // console.log(JSON.stringify(that.devices));
  199. // }
  200. // }
  201. // }
  202. // });
  203. // }
  204. // },
  205. // fail(res) {
  206. // console.log(res);
  207. // }
  208. // });
  209. // },
  210. // senBlData(deviceId, serviceId, characteristicId, uint8Array) {
  211. // console.log("************deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics=[" +characteristicId +"]");
  212. // var uint8Buf = Array.from(uint8Array);
  213. // function split_array(datas, size) {
  214. // var result = {};
  215. // var j = 0;
  216. // for (var i = 0; i < datas.length; i += size) {
  217. // result[j] = datas.slice(i, i + size);
  218. // j++;
  219. // }
  220. // console.log(result);
  221. // return result;
  222. // }
  223. // var sendloop = split_array(uint8Buf, 20);
  224. // // console.log(sendloop.length)
  225. // function realWriteData(sendloop, i) {
  226. // var data = sendloop[i];
  227. // if (typeof data == "undefined") {
  228. // return;
  229. // }
  230. // console.log("第【" + i + "】次写数据" + data);
  231. // var buffer = new ArrayBuffer(data.length);
  232. // var dataView = new DataView(buffer);
  233. // for (var j = 0; j < data.length; j++) {
  234. // dataView.setUint8(j, data[j]);
  235. // }
  236. // uni.writeBLECharacteristicValue({
  237. // deviceId,
  238. // serviceId,
  239. // characteristicId,
  240. // value: buffer,
  241. // success(res) {
  242. // realWriteData(sendloop, i + 1);
  243. // }
  244. // });
  245. // }
  246. // var i = 0;
  247. // realWriteData(sendloop, i);
  248. // },
  249. // senBleLabel() {
  250. // //标签模式
  251. // let deviceId = this.currDev.deviceId;
  252. // let serviceId = this.currDev.services[0].serviceId;
  253. // let characteristicId = this.currDev.services[0].characteristicId;
  254. // var command = tsc.jpPrinter.createNew();
  255. // command.setSize(40, 60);
  256. // command.setGap(2);
  257. // command.setCls();
  258. // // command.setText(50, 10, "TSS24.BF2", 1, 1, "打印测试")
  259. // // command.setQR(50, 50, "L", 5, "A", "977767937@qq.com")
  260. // // command.setQR(a, b, c, d, e, f ")
  261. // command.setText(30, 0, 2, 0, "arial", "测试");
  262. // command.setText("福建省厦门市集美管控黄庄三里9号绿意花街");
  263. // // command.setText("阿斯顿",'','13333333333');
  264. // // command.setText(10, 90, "TSS24.BF2", 1, 1, "收花人电话:5521667788");
  265. // // command.setText(10, 130, "TSS24.BF2", 1, 1, "收花时间:2020-6-10 上午");
  266. // // command.setText(10,170,"TSS24.BF2",1,1,"收花地址:11111厦门市集美区凤林前山路147号110室");
  267. // command.setPagePrint();
  268. // this.senBlData(deviceId, serviceId, characteristicId, command.getData());
  269. // },
  270. // billPrint() {
  271. // //票据模式
  272. // let deviceId = this.currDev.deviceId;
  273. // let serviceId = this.currDev.services[0].serviceId;
  274. // let characteristicId = this.currDev.services[0].characteristicId;
  275. // var command = esc.jpPrinter.createNew();
  276. // command.init();
  277. // command.setText(this.piaojuText);
  278. // command.setPrintAndFeedRow(1);
  279. // this.senBlData(deviceId, serviceId, characteristicId, command.getData());
  280. // }
  281. // }
  282. };
  283. </script>