connectBle.js 6.7 KB

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