connectBle.js 6.7 KB

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