connectBle.js 6.3 KB

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