connectBle.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // pages/connectBle/connectBle.js
  2. const app = getApp();
  3. const {
  4. BtHelper
  5. } = require('../../devices/bt_helper');
  6. import route_constant from '../../utils/route_constant.js'
  7. import route_util from '../../utils/route_util.js';
  8. Page({
  9. data: {
  10. connectDevice: {},
  11. nvabarData: {
  12. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  13. title: '连接设备', //导航栏 中间的标题
  14. callback() {
  15. wx.navigateBack({
  16. delta: 1,
  17. })
  18. }
  19. },
  20. connectStatus: 0,
  21. searchTips: "正在搜索设备,请保持开机状态…",
  22. subTips: "确认手机蓝牙已打开",
  23. buttonTips: "正在搜索设备",
  24. deviceImg: null,
  25. },
  26. onLoad(options) {
  27. BtHelper.getInstance().setSearching(true);
  28. var that = this;
  29. var json = JSON.parse(options.param)
  30. console.log("要连接设备:", json)
  31. that.setData({
  32. connectDevice: json
  33. });
  34. ///准备连接的设备
  35. app.globalData.connectWillDevice = that.data.connectDevice;
  36. BtHelper.getInstance().getBluetoothDevices(function (res) {
  37. that.setStatus(2);
  38. BtHelper.getInstance().stopSearch();
  39. that.data.connectDevice.deviceId = res.deviceId;
  40. that.data.connectDevice.connectable = res.connectable;
  41. }, false);
  42. that.startSearch();
  43. },
  44. ///搜索设备
  45. async startSearch() {
  46. var that = this;
  47. that.setStatus(0);
  48. ///搜索到对应的设备
  49. await BtHelper.getInstance().stopSearch();
  50. BtHelper.getInstance().startScan(
  51. function (b) {
  52. if (!b) {
  53. that.setStatus(1);
  54. }
  55. },
  56. );
  57. },
  58. connectToDvice() {
  59. var that = this;
  60. BtHelper.getInstance().connect(that.data.connectDevice, function (data) {
  61. console.log("连接成功:", data)
  62. that.setStatus(data ? 3 : 4)
  63. if (data) {
  64. // 蓝牙模式
  65. that.data.connectDevice.connectType = 1
  66. // 在线
  67. that.data.connectDevice.state = 'online'
  68. var connectDevice = that.data.connectDevice;
  69. getCurrentPages()[0].addConnectBlueDevice(connectDevice);
  70. route_util.goBackRoute(route_constant.indexRoot);
  71. }
  72. })
  73. },
  74. ///连接设备按钮
  75. connectDeviceTap() {
  76. var that = this;
  77. console.log("点击搜索状态", that.data.connectStatus)
  78. switch (that.data.connectStatus) {
  79. case 0:
  80. // 搜索中
  81. break;
  82. case 1:
  83. case 4:
  84. // 搜索失败,点击重新搜索了
  85. that.startSearch()
  86. break;
  87. case 2:
  88. that.connectToDvice()
  89. break;
  90. case 3:
  91. // 连接成功
  92. break;
  93. }
  94. },
  95. setStatus(bleType) {
  96. var that = this;
  97. var searchTips = ""
  98. var subTips = ""
  99. var buttonTips = ""
  100. let name = that.data.connectDevice.name;
  101. let typeList = that.data.connectDevice.typeList ?? []
  102. let bleTypes = typeList.find(v => v.connectType == 1)
  103. let deviceLinkResp = bleTypes.deviceLinkResp ?? {}
  104. console.log("搜索状态", bleType)
  105. switch (bleType) {
  106. case 0:
  107. // 搜索中
  108. searchTips = "正在搜索设备,请保持开机状态…"
  109. subTips = "确认手机蓝牙已打开"
  110. buttonTips = "正在搜索设备"
  111. that.data.deviceImg = deviceLinkResp.icon1
  112. break;
  113. case 1:
  114. // 搜索失败
  115. searchTips = "未搜索到" + name
  116. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  117. buttonTips = "重新搜索"
  118. that.data.deviceImg = deviceLinkResp.icon1
  119. break;
  120. case 2:
  121. // 搜索到
  122. searchTips = "搜索到" + name
  123. subTips = ""
  124. buttonTips = "连接"
  125. that.data.deviceImg = deviceLinkResp.icon2
  126. break;
  127. case 3:
  128. // 连接成功
  129. searchTips = "连接到" + name
  130. subTips = ""
  131. buttonTips = "连接成功"
  132. that.data.deviceImg = deviceLinkResp.icon2
  133. break;
  134. case 4:
  135. // 连接失败
  136. searchTips = "未连接到" + name
  137. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  138. buttonTips = "重新搜索"
  139. that.data.deviceImg = deviceLinkResp.icon1
  140. break;
  141. }
  142. that.setData({
  143. searchTips: searchTips,
  144. subTips: subTips,
  145. buttonTips: buttonTips,
  146. connectStatus: bleType,
  147. deviceImg: that.data.deviceImg
  148. })
  149. },
  150. ///关闭界面时候触发
  151. onUnload: function () {
  152. BtHelper.getInstance().setSearching(false);
  153. BtHelper.getInstance().stopSearch();
  154. }
  155. })
  156. // wx.getSystemInfo({
  157. // success: function (res) {
  158. // // { "screenWidth": 407, "cpuType": "unknown", "phoneCalendarAuthorized": true, "windowHeight": 888, "bluetoothEnabled": true, "bluetoothAuthorized": true, "language": "zh_CN", "microphoneAuthorized": true, "fontSizeScaleFactor": 1, "locationAuthorized": true, "notificationAuthorized": true, "model": "23078RKD5C", "statusBarHeight": 35, "safeArea": { "width": 407, "right": 407, "top": 35, "left": 0, "bottom": 888, "height": 853 }, "brand": "Redmi", "windowWidth": 407, "locationEnabled": true, "benchmarkLevel": 15, "screenHeight": 888, "abi": "arm64-v8a", "version": "8.0.53", "cameraAuthorized": true, "deviceAbi": "arm64-v8a", "system": "Android 14", "memorySize": 15367, "fontSizeSetting": 16, "pixelRatio": 3, "deviceOrientation": "portrait", "wifiEnabled": true, "screenTop": 0, "errMsg": "getSystemInfo:ok", "platform": "android", "SDKVersion": "3.6.6", "enableDebug": false, "devicePixelRatio": 3, "host": { "env": "WeChat", "version": 671102271 }, "mode": "default" }
  159. // console.log('UUID: ' + JSON.stringify(res));
  160. // console.log('MAC: ' + res.macaddress);
  161. // }
  162. // });
  163. // didFindDevice(element) {
  164. // let deviceId = element.deviceId
  165. // var that = this;
  166. // // todo 暂定这样
  167. // if (
  168. // // deviceId.includes("D8:24:07:89:31") ||
  169. // // // 2axk
  170. // // element.deviceId.includes("F5:A5:43:70:C8:F1") ||
  171. // /// sr1
  172. // element.deviceId.includes("E4:9F:80:09:40:EC") ||
  173. // // 黑色2x
  174. // // element.deviceId.includes("F6:61:D8:24:E5:98")
  175. // // /// mac: D7:92:84:87:09:7D
  176. // // ||
  177. // // element.deviceId.includes("F6:61:D8:24:E5:98") ||
  178. // // 黑胶
  179. // deviceId.includes("00:33:52:A7:3E:D0")
  180. // ) {
  181. // if (element.connectable == true) {
  182. // console.log("找到设备ble", element);
  183. // that.data.connectDevice.mac = element.mac
  184. // that.data.connectDevice.deviceId = element.deviceId
  185. // BtHelper.getInstance().stopSearch()
  186. // that.setStatus(2)
  187. // return true
  188. // }
  189. // }
  190. // return false
  191. // },