connectBle.js 6.6 KB

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