connectBle.js 7.7 KB

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