connectBle.js 6.3 KB

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