connectBle.js 8.3 KB

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