bleconnect.js 4.5 KB

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