connectBle.js 8.0 KB

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