connectBle.js 6.2 KB

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