connectBle.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /**
  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. route_util.goBackRoute(route_constant.indexRoot);
  111. // setTimeout(() => {
  112. // // wx.redirectTo({
  113. // // url: '/pages/index/index'
  114. // // });
  115. // route_util.goBackRoute("");
  116. // console.log("gadfasdfasdfaf==22222222222=");
  117. // }, 200);
  118. }
  119. })
  120. },
  121. getConnectedDevices: async function () {
  122. let _this = this
  123. const connectedDevices = await this.data.btHelper.getConnectedDevices()
  124. console.log("全部设备", connectedDevices)
  125. if (connectedDevices.length > 0) {
  126. connectedDevices.forEach(element => {
  127. console.log('已连接的蓝牙设备:', element);
  128. _this.didFindDevice(element)
  129. });
  130. } else {}
  131. return connectedDevices;
  132. },
  133. async startSearch() {
  134. this.setStatus(0)
  135. let _this = this
  136. this.data.btHelper.search(async function () {
  137. const connectedDevices = await _this.getConnectedDevices()
  138. if (connectedDevices.length == 0) {
  139. _this.setStatus(1)
  140. }
  141. })
  142. },
  143. findAllDevice() {
  144. let _this = this
  145. this.data.btHelper.findDevices(function (devices) {
  146. devices.forEach(element => {
  147. console.log('发现设备2:', element);
  148. _this.didFindDevice(element)
  149. });
  150. })
  151. },
  152. didFindDevice(element) {
  153. let deviceId = element.deviceId
  154. let _this = this
  155. // todo 暂定这样
  156. if (
  157. // deviceId.includes("D8:24:07:89:31") ||
  158. // // 2axk
  159. element.deviceId.includes("F5:A5:43:70:C8:F1") ||
  160. // 黑色2x
  161. // element.deviceId.includes("F6:61:D8:24:E5:98")
  162. // /// mac: D7:92:84:87:09:7D
  163. // ||
  164. // element.deviceId.includes("F6:61:D8:24:E5:98") ||
  165. // 黑胶
  166. deviceId.includes("00:33:52:A7:3E:D0")
  167. ) {
  168. if (element.connectable == true) {
  169. console.log("找到设备ble", element);
  170. _this.data.connectDevice.mac = element.mac
  171. _this.data.connectDevice.deviceId = element.deviceId
  172. _this.data.btHelper.stopSearch()
  173. _this.setStatus(2)
  174. }
  175. }
  176. },
  177. /**
  178. * 生命周期函数--监听页面加载
  179. */
  180. onLoad(options) {
  181. let _this = this
  182. const btHelper = BtHelper.getInstance();
  183. this.data.btHelper = btHelper;
  184. // getApp().globalData.btHelper = btHelper.getInstance();
  185. btHelper.initBluetooth(function (adapterState, hasPermission) {
  186. console.log("蓝牙状态", adapterState, hasPermission)
  187. if (adapterState && hasPermission) {
  188. // wx.getSystemInfo({
  189. // success: function (res) {
  190. // // { "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" }
  191. // console.log('UUID: ' + JSON.stringify(res));
  192. // console.log('MAC: ' + res.macaddress);
  193. // }
  194. // });
  195. _this.setStatus(0)
  196. _this.startSearch()
  197. } else {
  198. _this.setStatus(1)
  199. }
  200. })
  201. _this.findAllDevice()
  202. let json = JSON.parse(options.param)
  203. console.log("要连接设备:", json)
  204. this.setData({
  205. connectDevice: json
  206. })
  207. },
  208. /**
  209. * 生命周期函数--监听页面初次渲染完成
  210. */
  211. onReady() {
  212. },
  213. /**
  214. * 生命周期函数--监听页面显示
  215. */
  216. onShow() {
  217. },
  218. /**
  219. * 生命周期函数--监听页面隐藏
  220. */
  221. onHide() {
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload() {
  227. // todo 要关闭吗
  228. // this.data.btHelper.closeBle()
  229. },
  230. /**
  231. * 页面相关事件处理函数--监听用户下拉动作
  232. */
  233. onPullDownRefresh() {
  234. },
  235. /**
  236. * 页面上拉触底事件的处理函数
  237. */
  238. onReachBottom() {
  239. },
  240. /**
  241. * 用户点击右上角分享
  242. */
  243. onShareAppMessage() {
  244. }
  245. })