connectBle.js 8.3 KB

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