bleconnect.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // pages/bluConnect/bleconnect.js
  2. const app = getApp();
  3. Page(
  4. {
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. scopeBluetooth: false,
  10. isScaning: false,
  11. connectStatus: 0,
  12. connectText: "连接中",
  13. connectPic: "",
  14. connectTips: "正在搜索设备,请保持开机状态...",
  15. buttonTips: "正在搜索设备",
  16. userFuzzyLocation: true,
  17. nvabarData: {
  18. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  19. title: '设备连接', //导航栏 中间的标题
  20. callback: () => {
  21. wx.switchTab({
  22. url: '../home/home',
  23. })
  24. }
  25. },
  26. isNotruter: false,
  27. deviceItem: {
  28. "pic": "../../img/g.png",
  29. "address": "xxxxxx",
  30. "name": "猫王钢琴音箱",
  31. "devName": "名称1", "state": "online", "power": 40
  32. }
  33. },
  34. updateBleStatus() {
  35. var _this = this;
  36. switch (_this.connectStatus) {
  37. case 1:
  38. // 搜索中
  39. _this.connectTips = "正在搜索设备,请保持开机状态...";
  40. break;
  41. case 2:
  42. // 连接中
  43. _this.connectText = "连接中...";
  44. _this.connectPic = "";
  45. break;
  46. case 3:
  47. // 连接失败
  48. _this.connectText = "连接失败";
  49. _this.connectPic = "../../img/i.png";
  50. break;
  51. case 4:
  52. // 连接成功
  53. _this.connectText = "连接成功";
  54. _this.connectPic = "../../img/h.png";
  55. break;
  56. case 5:
  57. // 搜索成功
  58. _this.deviceItem = {
  59. "pic": "../../img/g.png",
  60. "address": "xxxxxx",
  61. "name": "猫王钢琴音箱",
  62. "devName": "名称1", "state": "online", "power": 40
  63. };
  64. _this.connectTips = "搜索到" + _this.deviceItem.name;
  65. _this.buttonTips = "连接设备";
  66. _this.isScaning = false;
  67. _this.connectText = "搜索成功";
  68. break;
  69. }
  70. _this.setData({
  71. buttonTips: _this.buttonTips,
  72. connectTips: _this.connectTips,
  73. isScaning: _this.isScaning,
  74. connectPic: _this.connectPic,
  75. connectText: _this.connectText,
  76. deviceItem: _this.deviceItem,
  77. connectStatus: _this.connectStatus
  78. })
  79. },
  80. getBluetoothStatusCallck(v) {
  81. this.connectStatus = 1;
  82. this.updateBleStatus();
  83. this.setData({
  84. scopeBluetooth: v,
  85. })
  86. if (v) {
  87. this.scanDevice();
  88. }
  89. },
  90. setBluetooth() {
  91. wx.openSetting({
  92. complete() {
  93. // app.getBluetoothStatus();
  94. }
  95. })
  96. }, connectDeviceTap() {
  97. const _this = this;
  98. if (_this.isScaning) {
  99. console.log("连接中2");
  100. return;
  101. }
  102. // todo 测试
  103. console.log("连接中");
  104. this.connectStatus = 4;
  105. this.updateBleStatus();
  106. setTimeout(() => {
  107. const eventChannel = _this.getOpenerEventChannel();
  108. eventChannel.emit('getBackData', {
  109. backData: _this.deviceItem
  110. });
  111. wx.navigateBack();
  112. }, 1000);
  113. return;
  114. wx.openBluetoothAdapter({
  115. success(res) {
  116. // var _this = this;
  117. // const eventChannel = this.getOpenerEventChannel();
  118. // eventChannel.emit('getBackData', {
  119. // backData: _this.deviceItem
  120. // });
  121. // wx.navigateBack();
  122. },
  123. fail(res) {
  124. console.log("连接失败:" + res);
  125. if (res.errCode === 10001) {
  126. wx.showToast({
  127. title: '蓝牙未打开',
  128. icon: "none",
  129. duration: 2000
  130. })
  131. }
  132. }
  133. });
  134. }, scanDevice() {
  135. this.setData({
  136. isScaning: true,
  137. });
  138. setTimeout(() => {
  139. console.log("2324");
  140. this.connectStatus = 5;
  141. this.updateBleStatus();
  142. }, 3000);
  143. },
  144. /**
  145. * 生命周期函数--监听页面加载
  146. */
  147. onLoad(options) {
  148. if (options.isLognlink) {
  149. }
  150. app.getBluetoothStatus();
  151. },
  152. /**
  153. * 生命周期函数--监听页面初次渲染完成
  154. */
  155. onReady() {
  156. },
  157. /**
  158. * 生命周期函数--监听页面显示
  159. */
  160. onShow() {
  161. },
  162. /**
  163. * 生命周期函数--监听页面隐藏
  164. */
  165. onHide() {
  166. },
  167. /**
  168. * 生命周期函数--监听页面卸载
  169. */
  170. onUnload() {
  171. },
  172. /**
  173. * 页面相关事件处理函数--监听用户下拉动作
  174. */
  175. onPullDownRefresh() {
  176. },
  177. /**
  178. * 页面上拉触底事件的处理函数
  179. */
  180. onReachBottom() {
  181. },
  182. /**
  183. * 用户点击右上角分享
  184. */
  185. onShareAppMessage() {
  186. }
  187. })