connectBle.js 8.5 KB

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