connectBle.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. that.startSearch();
  37. },
  38. startSearch() {
  39. var that = this;
  40. that.setStatus(0);
  41. ///搜索到对应的设备
  42. BtHelper.getInstance().search(
  43. function (c) {
  44. that.setStatus(2);
  45. BtHelper.getInstance().stopSearch();
  46. that.data.connectDevice.deviceId = c.deviceId;
  47. that.data.connectDevice.connectable = c.connectable;
  48. },
  49. function (b) {
  50. that.setStatus(1);
  51. }
  52. );
  53. },
  54. connectToDvice() {
  55. toastUtil.show("开始连接设备")
  56. var that = this;
  57. // 搜索到
  58. BtHelper.getInstance().stopSearch();
  59. BtHelper.getInstance().connect(that.data.connectDevice, function (data) {
  60. console.log("连接成功:", data)
  61. that.setStatus(data ? 3 : 4)
  62. if (data) {
  63. // 蓝牙模式
  64. that.data.connectDevice.connectType = 1
  65. // 在线
  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. setStatus(bleType) {
  74. var that = this;
  75. var searchTips = ""
  76. var subTips = ""
  77. var buttonTips = ""
  78. let name = that.data.connectDevice.name;
  79. let typeList = that.data.connectDevice.typeList ?? []
  80. let bleTypes = typeList.find(v => v.connectType == 1)
  81. let deviceLinkResp = bleTypes.deviceLinkResp ?? {}
  82. console.log("搜索状态", bleType)
  83. switch (bleType) {
  84. case 0:
  85. // 搜索中
  86. searchTips = "正在搜索设备,请保持开机状态…"
  87. subTips = "确认手机蓝牙已打开"
  88. buttonTips = "正在搜索设备"
  89. that.data.deviceImg = deviceLinkResp.icon1
  90. break;
  91. case 1:
  92. // 搜索失败
  93. searchTips = "未搜索到" + name
  94. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  95. buttonTips = "重新搜索"
  96. that.data.deviceImg = deviceLinkResp.icon1
  97. break;
  98. case 2:
  99. // 搜索到
  100. searchTips = "搜索到" + name
  101. subTips = ""
  102. buttonTips = "连接"
  103. that.data.deviceImg = deviceLinkResp.icon2
  104. break;
  105. case 3:
  106. // 连接成功
  107. searchTips = "连接到" + name
  108. subTips = ""
  109. buttonTips = "连接成功"
  110. that.data.deviceImg = deviceLinkResp.icon2
  111. break;
  112. case 4:
  113. // 连接失败
  114. searchTips = "未连接到" + name
  115. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  116. buttonTips = "重新搜索"
  117. that.data.deviceImg = deviceLinkResp.icon1
  118. break;
  119. }
  120. that.setData({
  121. searchTips: searchTips,
  122. subTips: subTips,
  123. buttonTips: buttonTips,
  124. connectStatus: bleType,
  125. deviceImg: that.data.deviceImg
  126. })
  127. },
  128. ///连接设备按钮
  129. async connectDeviceTap() {
  130. var that = this;
  131. console.log("点击搜索状态", that.data.connectStatus)
  132. switch (that.data.connectStatus) {
  133. case 0:
  134. // 搜索中
  135. break;
  136. case 1:
  137. case 4:
  138. // 搜索失败,点击重新搜索了
  139. that.startSearch()
  140. break;
  141. case 2:
  142. that.connectToDvice()
  143. break;
  144. case 3:
  145. // 连接成功
  146. break;
  147. }
  148. },
  149. getConnectedDevices: async function () {
  150. var that = this;
  151. const connectedDevices = await BtHelper.getInstance().getConnectedDevices()
  152. console.log("全部设备", connectedDevices)
  153. if (connectedDevices.length > 0) {
  154. connectedDevices.forEach(element => {
  155. console.log('已连接的蓝牙设备:', element);
  156. that.didFindDevice(element)
  157. });
  158. } else {}
  159. return connectedDevices;
  160. },
  161. findAllDevice() {
  162. var that = this;
  163. BtHelper.getInstance().findDevices(function (devices) {
  164. let hasFind = false;
  165. devices.forEach(element => {
  166. console.log('发现设备2:', element);
  167. if (that.didFindDevice(element)) {
  168. hasFind = true
  169. }
  170. });
  171. if (!hasFind) {
  172. that.setStatus(1)
  173. }
  174. });
  175. },
  176. didFindDevice(element) {
  177. let deviceId = element.deviceId
  178. var that = this;
  179. // todo 暂定这样
  180. if (
  181. // deviceId.includes("D8:24:07:89:31") ||
  182. // // 2axk
  183. // element.deviceId.includes("F5:A5:43:70:C8:F1") ||
  184. /// sr1
  185. element.deviceId.includes("E4:9F:80:09:40:EC") ||
  186. // 黑色2x
  187. // element.deviceId.includes("F6:61:D8:24:E5:98")
  188. // /// mac: D7:92:84:87:09:7D
  189. // ||
  190. // element.deviceId.includes("F6:61:D8:24:E5:98") ||
  191. // 黑胶
  192. deviceId.includes("00:33:52:A7:3E:D0")
  193. ) {
  194. if (element.connectable == true) {
  195. console.log("找到设备ble", element);
  196. that.data.connectDevice.mac = element.mac
  197. that.data.connectDevice.deviceId = element.deviceId
  198. BtHelper.getInstance().stopSearch()
  199. that.setStatus(2)
  200. return true
  201. }
  202. }
  203. return false
  204. },
  205. ///关闭界面时候触发
  206. onUnload: function () {
  207. var that = this;
  208. BtHelper.getInstance().stopSearch();
  209. }
  210. })
  211. // wx.getSystemInfo({
  212. // success: function (res) {
  213. // // { "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" }
  214. // console.log('UUID: ' + JSON.stringify(res));
  215. // console.log('MAC: ' + res.macaddress);
  216. // }
  217. // });