connectBle.js 6.6 KB

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