net.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // pages/sendCommand/sendCommand.js
  2. /**
  3. * 此Demo仅供参考,可打印数字,英文,符号,中文,
  4. * 小程序支持的蓝牙为低功耗蓝牙(BLE),数据量大需分包发送
  5. */
  6. var app = getApp();
  7. var tsc = require("../../utils/tsc.js");
  8. var esc = require("../../utils/esc.js");
  9. var encode = require("../../utils/encoding.js");//GB18030
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. ssid:"",
  16. password:"",
  17. looptime: 0,
  18. currentTime: 1,
  19. lastData: 0,
  20. oneTimeData: 60,
  21. returnResult: "",
  22. buffSize: [],
  23. buffIndex: 0,
  24. printNum: [],
  25. printerNum: 1,
  26. currentPrint: 1,
  27. netCommand: [31, 27,31, 27, 0, 17, 34, 80],
  28. selfCommand: [126, 87, 67],//斑马指令自检页
  29. end:[0],
  30. isNet:false,//标记是否为配网
  31. defaultType: true,
  32. passwordType: true
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function (options) {
  38. var that = this;
  39. wx.notifyBLECharacteristicValueChange({
  40. deviceId: app.BLEInformation.deviceId,
  41. serviceId: app.BLEInformation.notifyServiceId,
  42. characteristicId: app.BLEInformation.notifyCharaterId,
  43. state: true,
  44. success: function (res) {
  45. wx.onBLECharacteristicValueChange(function (r) {
  46. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  47. })
  48. },
  49. fail: function (e) {
  50. console.log(e)
  51. },
  52. complete: function (e) {
  53. console.log(e)
  54. }
  55. })
  56. },
  57. ssid: function (e) { //获取输入SSID
  58. this.setData({
  59. ssid: e.detail.value
  60. })
  61. // console.log("字符长度:" + data.name.length)
  62. },
  63. clear:function(){//清空输入
  64. this.setData({
  65. ssid: ""
  66. })
  67. },
  68. password: function (e) { //获取输入password
  69. this.setData({
  70. password: e.detail.value
  71. })
  72. },
  73. //defaultType:眼睛状态 passwordType:密码可见与否状态
  74. eyeStatus: function () {
  75. if (this.data.defaultType) {
  76. this.setData({
  77. passwordType: false,
  78. defaultType: false,
  79. })
  80. } else {
  81. this.setData({
  82. passwordType: true,
  83. defaultType: true,
  84. })
  85. }
  86. },
  87. settiing: function () { //设置
  88. console.log("SSID长度:" + this.data.ssid.length)
  89. if (this.data.ssid.length == 0 ) {
  90. wx.showModal({
  91. title: '提示',
  92. content: '名称SSID不能为空!',
  93. showCancel: false,
  94. })
  95. }else {
  96. wx.setStorageSync("ssid", this.data.ssid)
  97. wx.setStorageSync("password", this.data.password)
  98. var t_ssid = "\"" + this.data.ssid +"\""
  99. var t_password = "\""+this.data.password + "\""
  100. this.setData({
  101. looptime: 0,
  102. isNet:true,
  103. })
  104. var SSID = new encode.TextEncoder(
  105. 'utf-8', {
  106. NONSTANDARD_allowLegacyEncoding: true
  107. }).encode(t_ssid);
  108. var PASSWORD = new encode.TextEncoder(
  109. 'utf-8', {
  110. NONSTANDARD_allowLegacyEncoding: true
  111. }).encode(t_password);
  112. var data = [];
  113. for (var i = 0; i < this.data.netCommand.length;i++){
  114. data.push(this.data.netCommand[i]);
  115. }
  116. for (var i = 0; i < SSID.length; i++) {
  117. data.push(SSID[i]);
  118. }
  119. data.push(0);
  120. for (var i = 0; i < PASSWORD.length; i++) {
  121. data.push(PASSWORD[i]);
  122. }
  123. data.push(0);
  124. this.prepareSend(data)
  125. }
  126. },
  127. receiptTest: function () { //打印自检页
  128. this.prepareSend(this.data.selfCommand)
  129. },
  130. prepareSend: function (buff) { //准备发送,根据每次发送字节数来处理分包数量
  131. console.log(buff)
  132. var that = this
  133. var time = that.data.oneTimeData
  134. var looptime = parseInt(buff.length / time);
  135. var lastData = parseInt(buff.length % time);
  136. console.log(looptime + "---" + lastData)
  137. that.setData({
  138. looptime: looptime + 1,
  139. lastData: lastData,
  140. currentTime: 1,
  141. })
  142. that.Send(buff)
  143. },
  144. Send: function (buff) { //分包发送
  145. var that = this
  146. var currentTime = that.data.currentTime
  147. var loopTime = that.data.looptime
  148. var lastData = that.data.lastData
  149. var onTimeData = that.data.oneTimeData
  150. var printNum = that.data.printerNum
  151. var currentPrint = that.data.currentPrint
  152. var buf
  153. var dataView
  154. if (currentTime < loopTime) {
  155. buf = new ArrayBuffer(onTimeData)
  156. dataView = new DataView(buf)
  157. for (var i = 0; i < onTimeData; ++i) {
  158. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  159. }
  160. } else {
  161. buf = new ArrayBuffer(lastData)
  162. dataView = new DataView(buf)
  163. for (var i = 0; i < lastData; ++i) {
  164. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  165. }
  166. }
  167. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  168. wx.writeBLECharacteristicValue({
  169. deviceId: app.BLEInformation.deviceId,
  170. serviceId: app.BLEInformation.writeServiceId,
  171. characteristicId: app.BLEInformation.writeCharaterId,
  172. value: buf,
  173. success: function (res) {
  174. if(that.data.isNet){
  175. wx.showModal({
  176. title: '发送成功',
  177. content: '请耐心等待配网...\r\n配网成功打印:Config SUCCESS!\r\n配网失败打印:Config Fail!!!',
  178. showCancel: false,
  179. confirmText: '知道啦',
  180. success: function (res) {
  181. if (res.confirm) {
  182. that.setData({
  183. isNet: false,
  184. })
  185. that.onShow();
  186. } else if (res.cancel) {
  187. console.log('用户点击关闭')
  188. }
  189. }
  190. })
  191. console.log("配网")
  192. }else{
  193. console.log("不配网")
  194. }
  195. console.log("发送成功")
  196. console.log(res)
  197. },
  198. fail: function (e) {
  199. console.log("发送失败")
  200. wx.showModal({
  201. title: '提示',
  202. content: '发送失败',
  203. showCancel: false
  204. })
  205. console.log(e)
  206. return;
  207. },
  208. complete: function () {
  209. currentTime++
  210. if (currentTime <= loopTime) {
  211. that.setData({
  212. currentTime: currentTime
  213. })
  214. that.Send(buff)
  215. } else {
  216. if (currentPrint == printNum) {
  217. that.setData({
  218. looptime: 0,
  219. lastData: 0,
  220. currentTime: 1,
  221. currentPrint: 1
  222. })
  223. } else {
  224. currentPrint++
  225. that.setData({
  226. currentPrint: currentPrint,
  227. currentTime: 1,
  228. })
  229. that.Send(buff)
  230. }
  231. }
  232. }
  233. })
  234. },
  235. /**
  236. * 生命周期函数--监听页面初次渲染完成
  237. */
  238. onReady: function () {
  239. var list = []
  240. var numList = []
  241. var j = 0
  242. for (var i = 60; i < 200; i += 10) {
  243. list[j] = i;
  244. j++
  245. }
  246. for (var i = 1; i < 10; i++) {
  247. numList[i - 1] = i
  248. }
  249. this.setData({
  250. buffSize: list,
  251. oneTimeData: list[0],
  252. printNum: numList,
  253. printerNum: numList[0]
  254. })
  255. },
  256. /**
  257. * 生命周期函数--监听页面显示
  258. */
  259. onShow: function () {
  260. var that = this
  261. //获取本地数据
  262. var ssid = wx.getStorageSync('ssid');
  263. var password = wx.getStorageSync('password');
  264. console.log("已保存"+ssid);
  265. console.log("已保存" + password);
  266. if (ssid) {
  267. this.setData({
  268. ssid: ssid
  269. });
  270. }
  271. if (password) {
  272. this.setData({
  273. password: password
  274. });
  275. }
  276. },
  277. buffBindChange: function (res) { //更改打印字节数
  278. var index = res.detail.value
  279. var time = this.data.buffSize[index]
  280. console.log("index" + index);
  281. console.log("time" + time);
  282. this.setData({
  283. buffIndex: index,
  284. oneTimeData: time
  285. })
  286. },
  287. /**
  288. * 生命周期函数--监听页面隐藏
  289. */
  290. onHide: function () {
  291. },
  292. /**
  293. * 生命周期函数--监听页面卸载
  294. */
  295. onUnload: function () {
  296. // wx.closeBLEConnection({
  297. // deviceId: app.BLEInformation.deviceId,
  298. // success: function (res) {
  299. // console.log("关闭蓝牙成功")
  300. // },
  301. // })
  302. },
  303. /**
  304. * 页面相关事件处理函数--监听用户下拉动作
  305. */
  306. onPullDownRefresh: function () {
  307. },
  308. /**
  309. * 页面上拉触底事件的处理函数
  310. */
  311. onReachBottom: function () {
  312. },
  313. /**
  314. * 用户点击右上角分享
  315. */
  316. onShareAppMessage: function () {
  317. }
  318. })