manager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. module.exports = Manager;
  2. class Manager {
  3. constructor() {
  4. var that = this;
  5. ///正在连接中的设备
  6. that.connectWillDevice = null;
  7. ///正在连接的设备,成功后返回的数据
  8. that.callBackConnect = null;
  9. ///搜索过快会失败,给的一定的缓冲时间
  10. that.requestBlueTime = 0;
  11. ///蓝牙是否已打开,是否可用
  12. that.isAvailable = false;
  13. ///蓝牙正在搜索中,则不允许继续搜索
  14. that.doStartScaning = false;
  15. ///蓝牙设备搜索的所有在线设备,用来和现有设备比较
  16. that.compareList = [];
  17. ///离线的所有设备,也可以用来比较
  18. that.dissmissDevice = [];
  19. }
  20. ///获取比较的数据
  21. getCompareList() {
  22. var that = this;
  23. return that.compareList;
  24. }
  25. setCompareList(compareList) {
  26. var that = this;
  27. that.compareList = compareList;
  28. }
  29. getDissmissDevice() {
  30. var that = this;
  31. return that.dissmissDevice;
  32. }
  33. setConnectWillDevice(connectWillDevice) {
  34. var that = this;
  35. that.connectWillDevice = connectWillDevice;
  36. }
  37. getCallBackConnect() {
  38. var that = this;
  39. return that.callBackConnect;
  40. }
  41. setCallBackConnect(callBackConnect) {
  42. this.callBackConnect = callBackConnect;
  43. }
  44. /// 监控蓝牙打开状态
  45. initBlueAdapter() {
  46. var that = this;
  47. wx.onBluetoothAdapterStateChange(function (res) {
  48. that.isAvailable = res.available;
  49. if (!that.isAvailable) {
  50. that.compareList = [];
  51. that.dissmissDevice = [];
  52. getCurrentPages()[0].closeBlueResetOffline(false, true);
  53. }
  54. })
  55. }
  56. ///监听搜索设备列表
  57. listenBlueDevices() {
  58. var that = this;
  59. const hex_util = require('./../../utils/hex_util');
  60. wx.onBluetoothDeviceFound(function (res) {
  61. ///第一种情况
  62. if (res.deviceId) {
  63. if (that.callBackConnect != null) {
  64. if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
  65. res.mac = res.advertisData ? hex_util.buf2hex(res.advertisData) : '';
  66. if (that.callBackConnect != null) {
  67. that.callBackConnect(res);
  68. }
  69. }
  70. } else {
  71. if (res.name != "") {
  72. if (that.compareList.length > 0) {
  73. var has = false;
  74. for (var i = 0; i < that.compareList.length; i++) {
  75. if (res.deviceId == that.compareList[i].deviceId) {
  76. has = true;
  77. break;
  78. }
  79. }
  80. if (!has) {
  81. that.compareList.push(res);
  82. }
  83. } else {
  84. that.compareList.push(res);
  85. }
  86. }
  87. }
  88. }
  89. ///第二种情况
  90. else if (res.devices) {
  91. if (that.callBackConnect != null) {
  92. for (var i = 0; i < res.devices.length; i++) {
  93. var temp = res.devices[i];
  94. if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  95. temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
  96. temp.mac2 = that.ab2hex(temp.advertisData ?? "")
  97. if (that.callBackConnect != null) {
  98. that.callBackConnect(temp);
  99. }
  100. break;
  101. }
  102. }
  103. } else {
  104. for (var i = 0; i < res.devices.length; i++) {
  105. if (that.compareList.length > 0) {
  106. var has = false;
  107. for (var j = 0; j < that.compareList.length; j++) {
  108. if (res.devices[i].name != "") {
  109. if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  110. has = true;
  111. break;
  112. }
  113. }
  114. }
  115. if (!has) {
  116. that.compareList.push(res.devices[i]);
  117. }
  118. } else {
  119. that.compareList.push(res.devices[i]);
  120. }
  121. }
  122. }
  123. }
  124. ///第三种情况
  125. else if (res[0]) {
  126. if (that.callBackConnect != null) {
  127. if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
  128. res[0].mac = res[0].advertisData ? hex_util.buf2hex(res[0].advertisData) : '';
  129. if (that.callBackConnect != null) {
  130. that.callBackConnect(res[0]);
  131. }
  132. }
  133. } else {
  134. if (res[0].name != "") {
  135. if (that.compareList.length > 0) {
  136. var has = false;
  137. for (var i = 0; i < that.compareList.length; i++) {
  138. if (res[0].deviceId == that.compareList[i].deviceId) {
  139. has = true;
  140. break;
  141. }
  142. }
  143. if (!has) {
  144. that.compareList.push(res[0]);
  145. }
  146. } else {
  147. that.compareList.push(res[0]);
  148. }
  149. }
  150. }
  151. }
  152. });
  153. ///监听 离线和连接成功设备导入
  154. // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
  155. wx.onBLEConnectionStateChange((result) => {
  156. if (result.connected) {
  157. for (var i = 0; i < that.dissmissDevice.length; i++) {
  158. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  159. that.dissmissDevice.splice(i, 1);
  160. break;
  161. }
  162. }
  163. } else {
  164. var has = false;
  165. for (var i = 0; i < that.dissmissDevice.length; i++) {
  166. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  167. has = true;
  168. break;
  169. }
  170. }
  171. if (!has) {
  172. that.dissmissDevice.push(result);
  173. }
  174. }
  175. });
  176. }
  177. // 开始搜索蓝牙设备
  178. async startScan(connectWillDevice, boolean, callBackConnect) {
  179. var that = this;
  180. ///限制搜索没有打开蓝牙一直询问打开蓝牙
  181. var isAvailable = that.isAvailable;
  182. if (!isAvailable && callBackConnect == null) {
  183. return;
  184. }
  185. const route_util = require('../utils/route_util');
  186. const route_constant = require('../utils/route_constant');
  187. const indexRoot = route_constant.indexRoot;
  188. const connectBleRoot = route_constant.connectBleRoot;
  189. var lastPageRoute = route_util.getLastPageRoute();
  190. if (lastPageRoute != indexRoot && lastPageRoute != connectBleRoot) {
  191. return;
  192. }
  193. ///蓝牙连接 做限制
  194. if (lastPageRoute == indexRoot) {
  195. if (that.doStartScaning == true) {
  196. return;
  197. }
  198. }
  199. const time_util = require('./../../utils/time_util');
  200. that.doStartScaning = true;
  201. var currentMill = time_util.getCurrentMills();
  202. var waitMills = 0;
  203. var reduce = currentMill - that.requestBlueTime;
  204. const delayMills = 6 * 1000;
  205. if (reduce > 0 && reduce < delayMills) {
  206. waitMills = delayMills - reduce;
  207. }
  208. if (waitMills > 0) {
  209. await time_util.delayMills(waitMills);
  210. }
  211. if (callBackConnect == null && lastPageRoute == connectBleRoot) {
  212. that.doStartScaning = false;
  213. return;
  214. }
  215. wx.openBluetoothAdapter({
  216. success: function (res) {
  217. wx.stopBluetoothDevicesDiscovery({
  218. success: (res) => {
  219. that.search(connectWillDevice, boolean, callBackConnect);
  220. },
  221. fail: (err) => {
  222. that.doStartScaning = false;
  223. that.requestBlueTime = time_util.getCurrentMills();
  224. if (boolean != null) {
  225. boolean(false);
  226. }
  227. }
  228. });
  229. },
  230. fail: function (res) {
  231. that.doStartScaning = false;
  232. that.requestBlueTime = time_util.getCurrentMills();
  233. if (boolean != null) {
  234. boolean(false);
  235. }
  236. }
  237. });
  238. }
  239. ///搜索设备
  240. search(connectWillDevice, boolean, callBackConnect) {
  241. var that = this;
  242. const time_util = require('./../../utils/time_util');
  243. wx.startBluetoothDevicesDiscovery({
  244. allowDuplicatesKey: true,
  245. success: function (res) {
  246. that.getConnectedDevices();
  247. that.doStartScaning = false;
  248. that.requestBlueTime = time_util.getCurrentMills();
  249. if (boolean != null) {
  250. boolean(true);
  251. }
  252. that.setConnectWillDevice(connectWillDevice);
  253. that.setCallBackConnect(callBackConnect);
  254. that.compareList = [];
  255. },
  256. fail(err) {
  257. that.doStartScaning = false;
  258. that.requestBlueTime = time_util.getCurrentMills();
  259. if (boolean != null) {
  260. boolean(false);
  261. }
  262. },
  263. });
  264. }
  265. // 停止搜索
  266. stopScan() {
  267. var that = this;
  268. that.setCallBackConnect(null);
  269. that.setConnectWillDevice(null);
  270. return new Promise((resolve, reject) => {
  271. wx.stopBluetoothDevicesDiscovery({
  272. success: (res) => {
  273. resolve(res);
  274. },
  275. fail: (err) => {
  276. reject('停止搜索失败');
  277. }
  278. });
  279. });
  280. }
  281. ///获取 所有搜索到的蓝牙设备
  282. getConnectedDevices() {
  283. var that = this;
  284. const hex_util = require('./../../utils/hex_util');
  285. wx.getBluetoothDevices({
  286. success: (res) => {
  287. if (that.callBackConnect != null) {
  288. for (var i = 0; i < res.devices.length; i++) {
  289. var temp = res.devices[i];
  290. if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  291. temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
  292. if (that.callBackConnect != null) {
  293. that.callBackConnect(temp);
  294. }
  295. break;
  296. }
  297. }
  298. } else {
  299. for (var i = 0; i < res.devices.length; i++) {
  300. if (that.compareList.length > 0) {
  301. var has = false;
  302. for (var j = 0; j < that.compareList.length; j++) {
  303. if (res.devices[i].name != "") {
  304. if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  305. has = true;
  306. break;
  307. }
  308. }
  309. }
  310. if (!has) {
  311. that.compareList.push(res.devices[i]);
  312. }
  313. } else {
  314. that.compareList.push(res.devices[i]);
  315. }
  316. }
  317. }
  318. },
  319. fail: (err) => {
  320. console.error('获取蓝牙设备列表失败', err);
  321. }
  322. });
  323. }
  324. }