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