connectBle.js 6.6 KB

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