随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

203 行
5.4KB

  1. // 示例demo
  2. const plugin = requirePlugin('ppScale-plugin')
  3. var app = getApp();
  4. Page({
  5. data: {
  6. deviceslist: [{
  7. name: "LFSmart Scale",
  8. deviceId: "CF:E6:16:22:82:28"
  9. }],
  10. },
  11. onHide() {
  12. },
  13. onLoad() {
  14. let that = this;
  15. //监听蓝牙打开状态
  16. wx.onBluetoothAdapterStateChange(function (res) {
  17. console.log('蓝牙适配器打开状态变化', res)
  18. // 如果蓝牙从关闭到打开并且 deviceslist 为空,则重新初始化蓝牙
  19. if (res.available && !that.data.deviceslist) {
  20. that.initBlue();
  21. }
  22. })
  23. //监听蓝牙连接状态
  24. wx.onBLEConnectionStateChange(function (res) {
  25. // 该方法回调中可以用于处理连接意外断开等异常情况
  26. console.log(`设备蓝牙连接状态`, res);
  27. // 如果蓝牙断开
  28. if (!res.connected) {
  29. // 关闭蓝牙连接事件,再重新初始化连接?
  30. // that.closeBlue();
  31. wx.showToast({
  32. title: '蓝牙已断开',
  33. icon: 'error'
  34. })
  35. /* setTimeout(() => {
  36. that.initBlue();
  37. }, 1500) */
  38. } else {
  39. wx.showToast({
  40. title: '蓝牙已连接',
  41. icon: 'success'
  42. })
  43. }
  44. })
  45. },
  46. onShow() {
  47. // 初始化蓝牙
  48. this.initBlue()
  49. },
  50. onUnload() {
  51. // 业务结束后需要停止蓝牙
  52. this.closeBlue();
  53. },
  54. closeBlue() {
  55. wx.closeBluetoothAdapter();
  56. wx.offBluetoothAdapterStateChange()
  57. },
  58. initBlue() {
  59. let that = this;
  60. /* that.setData({
  61. deviceslist: ''
  62. }) */
  63. console.log("初始化蓝牙");
  64. wx.openBluetoothAdapter({
  65. mode: 'peripheral',
  66. success: (res) => {
  67. console.log('openBluetoothAdapter', res)
  68. // 初始化成功后开始搜索周边蓝牙列表
  69. wx.showLoading({
  70. title: '搜索中',
  71. })
  72. wx.startBluetoothDevicesDiscovery({
  73. services: [], // 可指定服务UUID筛选要搜索的设备
  74. success: function (res) {
  75. if (res.errno === 0) {
  76. console.log('蓝牙设备搜索成功');
  77. setTimeout(() => {
  78. wx.getBluetoothDevices({
  79. success: (resu) => {
  80. console.log('getBluetoothDevices', resu)
  81. // resu.device 为蓝牙列表
  82. wx.hideLoading();
  83. if (resu.devices.length > 0) {
  84. wx.showToast({
  85. title: '搜索成功',
  86. icon: 'success'
  87. })
  88. that.setData({
  89. deviceslist: resu.devices
  90. });
  91. // 此时再监听搜索到新设备的事件
  92. wx.onBluetoothDeviceFound(function(res) {
  93. var devices = res.devices;
  94. console.log('找到一个新设备', devices)
  95. that.data.deviceslist.push(devices[0]);
  96. that.setData({
  97. deviceslist: that.data.deviceslist
  98. });
  99. })
  100. } else {
  101. wx.showModal({
  102. title: '温馨提示',
  103. content: `暂无搜索到可用设备`,
  104. showCancel: false,
  105. })
  106. }
  107. }
  108. })
  109. // // 搜索到设备后停止搜索
  110. // wx.stopBluetoothDevicesDiscovery({
  111. // success: () => {
  112. // console.log("搜索到设备,停止搜索");
  113. // }
  114. // });
  115. }, 1500)
  116. }
  117. },
  118. fail: function (err) {
  119. wx.hideLoading();
  120. wx.showModal({
  121. title: '温馨提示',
  122. content: `蓝牙设备搜索失败`,
  123. showCancel: false,
  124. })
  125. console.error('蓝牙设备搜索失败', err)
  126. }
  127. })
  128. },
  129. fail: (fail) => {
  130. wx.showModal({
  131. title: '温馨提示',
  132. content: `请打开蓝牙`,
  133. showCancel: false,
  134. })
  135. }
  136. });
  137. },
  138. // 选择设备
  139. selItem(res) {
  140. var that = this;
  141. var devItem = res.currentTarget.dataset.item;
  142. var device = this.data.deviceslist.find(item => item.deviceId === devItem.deviceId)
  143. app.globalData.selmac = device;
  144. that.data.selmac = device;
  145. that.setData({
  146. selmac: device
  147. })
  148. // 本地缓存连接的蓝牙设备信息
  149. console.log("this.globalData", app.globalData.selmac)
  150. },
  151. refresh: function (res) {
  152. this.initBlue();
  153. console.log("刷新", );
  154. },
  155. doback(res) {
  156. if (app.globalData.selmac) {
  157. // 进入详情前先停止搜索设备,
  158. wx.stopBluetoothDevicesDiscovery({
  159. success: () => {
  160. console.log("开始连接到设备,停止搜索");
  161. }
  162. });
  163. wx.navigateTo({
  164. url: '/pages/devicedetail/index'
  165. })
  166. } else if (this.data.deviceslist.length <= 0) {
  167. wx.showModal({
  168. title: '温馨提示',
  169. content: `搜索不到设备,请重试获取打开蓝牙`,
  170. showCancel: false,
  171. })
  172. } else {
  173. wx.showModal({
  174. title: '温馨提示',
  175. content: `请选择上面要连接的设备`,
  176. showCancel: false,
  177. })
  178. console.log('请选择设备')
  179. }
  180. },
  181. })