connectBle.js 6.8 KB

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