connectBle.js 7.9 KB

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