connectBle.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // pages/connectBle/connectBle.js
  2. const {
  3. BtHelper
  4. } = require('../../devices/bt_helper');
  5. const toastUtil = require('../../utils/toast_util');
  6. import routeUtil from '../../utils/route_util'
  7. import route_constant from '../../utils/route_constant.js'
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. connectDevice: {},
  14. nvabarData: {
  15. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  16. title: '连接设备', //导航栏 中间的标题
  17. callback() {
  18. wx.navigateBack({
  19. delta: 1,
  20. })
  21. }
  22. },
  23. connectStatus: 0,
  24. searchTips: "正在搜索设备,请保持开机状态…",
  25. subTips: "确认手机蓝牙已打开",
  26. buttonTips: "正在搜索设备",
  27. btHelper: null
  28. },
  29. setStatus(bleType) {
  30. var searchTips = ""
  31. var subTips = ""
  32. var buttonTips = ""
  33. let name = this.data.connectDevice.name;
  34. console.log("搜索状态", bleType)
  35. switch (bleType) {
  36. case 0:
  37. // 搜索中
  38. searchTips = "正在搜索设备,请保持开机状态…"
  39. subTips = "确认手机蓝牙已打开"
  40. buttonTips = "正在搜索设备"
  41. break;
  42. case 1:
  43. // 搜索失败
  44. searchTips = "未搜索到" + name
  45. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  46. buttonTips = "重新搜索"
  47. break;
  48. case 2:
  49. // 搜索到
  50. searchTips = "搜索到" + name
  51. subTips = ""
  52. buttonTips = "连接"
  53. break;
  54. case 3:
  55. // 连接成功
  56. searchTips = "连接到" + name
  57. subTips = ""
  58. buttonTips = "连接成功"
  59. break;
  60. case 4:
  61. // 连接失败
  62. searchTips = "未连接到" + name
  63. subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
  64. buttonTips = "重新搜索"
  65. break;
  66. }
  67. this.setData({
  68. searchTips: searchTips,
  69. subTips: subTips,
  70. buttonTips: buttonTips,
  71. connectStatus: bleType
  72. })
  73. },
  74. async connectDeviceTap() {
  75. let _this = this
  76. console.log("点击搜索状态", this.data.connectStatus)
  77. switch (this.data.connectStatus) {
  78. case 0:
  79. // 搜索中
  80. break;
  81. case 1:
  82. case 4:
  83. // 搜索失败,点击重新搜索
  84. _this.startSearch()
  85. break;
  86. case 2:
  87. _this.connectToDvice()
  88. break;
  89. case 3:
  90. // 连接成功
  91. break;
  92. }
  93. },
  94. connectToDvice() {
  95. toastUtil.show("开始连接设备")
  96. let _this = this
  97. // 搜索到
  98. this.data.btHelper.connect(_this.data.connectDevice, function (data) {
  99. console.log("连接成功:", data)
  100. _this.setStatus(data ? 3 : 4)
  101. if (data) {
  102. // 蓝牙模式
  103. _this.data.connectDevice.connectType = 1
  104. // 在线
  105. _this.data.connectDevice.state = 'online'
  106. // getApp().globalData.device = _this.data.connectDevice
  107. // {"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",
  108. var connectDevice = _this.data.connectDevice;
  109. getCurrentPages()[0].addConnectBlueDevice(connectDevice);
  110. setTimeout(() => {
  111. wx.redirectTo({
  112. url: '/pages/index/index'
  113. });
  114. }, 200);
  115. }
  116. })
  117. },
  118. getConnectedDevices: async function () {
  119. let _this = this
  120. const connectedDevices = await this.data.btHelper.getConnectedDevices()
  121. console.log("全部设备", connectedDevices)
  122. if (connectedDevices.length > 0) {
  123. connectedDevices.forEach(element => {
  124. console.log('已连接的蓝牙设备:', element);
  125. _this.didFindDevice(element)
  126. });
  127. } else { }
  128. return connectedDevices;
  129. },
  130. async startSearch() {
  131. this.setStatus(0)
  132. let _this = this
  133. this.data.btHelper.search(async function () {
  134. const connectedDevices = await _this.getConnectedDevices()
  135. if (connectedDevices.length == 0) {
  136. _this.setStatus(1)
  137. }
  138. })
  139. },
  140. findAllDevice() {
  141. let _this = this
  142. this.data.btHelper.findDevices(function (devices) {
  143. devices.forEach(element => {
  144. console.log('发现设备2:', element);
  145. _this.didFindDevice(element)
  146. });
  147. })
  148. }, didFindDevice(element) {
  149. let deviceId = element.deviceId
  150. let _this = this
  151. // todo 暂定这样
  152. if (
  153. // deviceId.includes("D8:24:07:89:31") ||
  154. // // 2axk
  155. element.deviceId.includes("F5:A5:43:70:C8:F1") ||
  156. // 黑色2x
  157. // element.deviceId.includes("F6:61:D8:24:E5:98")
  158. // /// mac: D7:92:84:87:09:7D
  159. // ||
  160. // element.deviceId.includes("F6:61:D8:24:E5:98") ||
  161. // 黑胶
  162. deviceId.includes("00:33:52:A7:3E:D0")
  163. ) {
  164. if (element.connectable == true) {
  165. console.log("找到设备ble", element);
  166. _this.data.connectDevice.mac = element.mac
  167. _this.data.connectDevice.deviceId = element.deviceId
  168. _this.data.btHelper.stopSearch()
  169. _this.setStatus(2)
  170. }
  171. }
  172. },
  173. /**
  174. * 生命周期函数--监听页面加载
  175. */
  176. onLoad(options) {
  177. let _this = this
  178. const btHelper = BtHelper.getInstance();
  179. this.data.btHelper = btHelper;
  180. // getApp().globalData.btHelper = btHelper.getInstance();
  181. btHelper.initBluetooth(function (adapterState, hasPermission) {
  182. console.log("蓝牙状态", adapterState, hasPermission)
  183. if (adapterState && hasPermission) {
  184. // wx.getSystemInfo({
  185. // success: function (res) {
  186. // // { "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" }
  187. // console.log('UUID: ' + JSON.stringify(res));
  188. // console.log('MAC: ' + res.macaddress);
  189. // }
  190. // });
  191. _this.setStatus(0)
  192. _this.startSearch()
  193. } else {
  194. _this.setStatus(1)
  195. }
  196. })
  197. _this.findAllDevice()
  198. let json = JSON.parse(options.param)
  199. console.log("要连接设备:", json)
  200. this.setData({
  201. connectDevice: json
  202. })
  203. },
  204. /**
  205. * 生命周期函数--监听页面初次渲染完成
  206. */
  207. onReady() {
  208. },
  209. /**
  210. * 生命周期函数--监听页面显示
  211. */
  212. onShow() {
  213. },
  214. /**
  215. * 生命周期函数--监听页面隐藏
  216. */
  217. onHide() {
  218. },
  219. /**
  220. * 生命周期函数--监听页面卸载
  221. */
  222. onUnload() {
  223. // todo 要关闭吗
  224. // this.data.btHelper.closeBle()
  225. },
  226. /**
  227. * 页面相关事件处理函数--监听用户下拉动作
  228. */
  229. onPullDownRefresh() {
  230. },
  231. /**
  232. * 页面上拉触底事件的处理函数
  233. */
  234. onReachBottom() {
  235. },
  236. /**
  237. * 用户点击右上角分享
  238. */
  239. onShareAppMessage() {
  240. }
  241. })