manager.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. ///当前蓝牙ble连接设备的相关属性
  20. that.writeCharaterId = "";
  21. that.writeServiceId = "";
  22. that.readCharaterId = "";
  23. that.readServiceId = "";
  24. }
  25. ///获取比较的数据
  26. getCompareList() {
  27. var that = this;
  28. return that.compareList;
  29. }
  30. setCompareList(compareList) {
  31. var that = this;
  32. that.compareList = compareList;
  33. }
  34. getDissmissDevice() {
  35. var that = this;
  36. return that.dissmissDevice;
  37. }
  38. setConnectWillDevice(connectWillDevice) {
  39. var that = this;
  40. that.connectWillDevice = connectWillDevice;
  41. }
  42. getCallBackConnect() {
  43. var that = this;
  44. return that.callBackConnect;
  45. }
  46. setCallBackConnect(callBackConnect) {
  47. this.callBackConnect = callBackConnect;
  48. }
  49. /// 监控蓝牙打开状态
  50. initBlueAdapter() {
  51. var that = this;
  52. wx.onBluetoothAdapterStateChange(function (res) {
  53. that.isAvailable = res.available;
  54. if (!that.isAvailable) {
  55. that.compareList = [];
  56. that.dissmissDevice = [];
  57. getCurrentPages()[0].closeBlueResetOffline(false, true);
  58. }
  59. })
  60. }
  61. ///监听搜索设备列表
  62. listenBlueDevices() {
  63. var that = this;
  64. const hex_util = require('./../../utils/hex_util');
  65. wx.onBluetoothDeviceFound(function (res) {
  66. ///第一种情况
  67. if (res.deviceId) {
  68. if (that.callBackConnect != null) {
  69. if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
  70. res.mac = res.advertisData ? hex_util.buf2hex(res.advertisData) : '';
  71. if (that.callBackConnect != null) {
  72. that.callBackConnect(res);
  73. }
  74. }
  75. } else {
  76. if (res.name != "") {
  77. if (that.compareList.length > 0) {
  78. var has = false;
  79. for (var i = 0; i < that.compareList.length; i++) {
  80. if (res.deviceId == that.compareList[i].deviceId) {
  81. has = true;
  82. break;
  83. }
  84. }
  85. if (!has) {
  86. that.compareList.push(res);
  87. }
  88. } else {
  89. that.compareList.push(res);
  90. }
  91. }
  92. }
  93. }
  94. ///第二种情况
  95. else if (res.devices) {
  96. if (that.callBackConnect != null) {
  97. for (var i = 0; i < res.devices.length; i++) {
  98. var temp = res.devices[i];
  99. if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  100. temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
  101. temp.mac2 = that.ab2hex(temp.advertisData ?? "")
  102. if (that.callBackConnect != null) {
  103. that.callBackConnect(temp);
  104. }
  105. break;
  106. }
  107. }
  108. } else {
  109. for (var i = 0; i < res.devices.length; i++) {
  110. if (that.compareList.length > 0) {
  111. var has = false;
  112. for (var j = 0; j < that.compareList.length; j++) {
  113. if (res.devices[i].name != "") {
  114. if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  115. has = true;
  116. break;
  117. }
  118. }
  119. }
  120. if (!has) {
  121. that.compareList.push(res.devices[i]);
  122. }
  123. } else {
  124. that.compareList.push(res.devices[i]);
  125. }
  126. }
  127. }
  128. }
  129. ///第三种情况
  130. else if (res[0]) {
  131. if (that.callBackConnect != null) {
  132. if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
  133. res[0].mac = res[0].advertisData ? hex_util.buf2hex(res[0].advertisData) : '';
  134. if (that.callBackConnect != null) {
  135. that.callBackConnect(res[0]);
  136. }
  137. }
  138. } else {
  139. if (res[0].name != "") {
  140. if (that.compareList.length > 0) {
  141. var has = false;
  142. for (var i = 0; i < that.compareList.length; i++) {
  143. if (res[0].deviceId == that.compareList[i].deviceId) {
  144. has = true;
  145. break;
  146. }
  147. }
  148. if (!has) {
  149. that.compareList.push(res[0]);
  150. }
  151. } else {
  152. that.compareList.push(res[0]);
  153. }
  154. }
  155. }
  156. }
  157. });
  158. ///监听 离线和连接成功设备导入
  159. // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
  160. wx.onBLEConnectionStateChange((result) => {
  161. if (result.connected) {
  162. for (var i = 0; i < that.dissmissDevice.length; i++) {
  163. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  164. that.dissmissDevice.splice(i, 1);
  165. break;
  166. }
  167. }
  168. } else {
  169. var has = false;
  170. for (var i = 0; i < that.dissmissDevice.length; i++) {
  171. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  172. has = true;
  173. break;
  174. }
  175. }
  176. if (!has) {
  177. that.dissmissDevice.push(result);
  178. }
  179. }
  180. });
  181. }
  182. // 开始搜索蓝牙设备
  183. async startScan(connectWillDevice, boolean, callBackConnect) {
  184. var that = this;
  185. ///限制搜索没有打开蓝牙一直询问打开蓝牙
  186. if (!that.isAvailable) {
  187. if (callBackConnect == null) {
  188. return;
  189. }
  190. ///做搜索蓝牙适配器权限
  191. const permission_util = require('../utils/permission_util');
  192. var available = await permission_util.openBluetoothAdapter();
  193. that.isAvailable = available;
  194. if (!available) {
  195. that.doStartScaning = false;
  196. that.requestBlueTime = that.getCurrentMills();
  197. if (boolean != null) {
  198. boolean(false);
  199. }
  200. return;
  201. }
  202. }
  203. const route_util = require('../utils/route_util');
  204. const route_constant = require('../utils/route_constant');
  205. const indexRoot = route_constant.indexRoot;
  206. const connectBleRoot = route_constant.connectBleRoot;
  207. var lastPageRoute = route_util.getLastPageRoute();
  208. if (lastPageRoute != indexRoot && lastPageRoute != connectBleRoot) {
  209. return;
  210. }
  211. ///蓝牙连接 做限制
  212. if (lastPageRoute == indexRoot) {
  213. if (that.doStartScaning == true) {
  214. return;
  215. }
  216. }
  217. const time_util = require('./../../utils/time_util');
  218. that.doStartScaning = true;
  219. var currentMill = time_util.getCurrentMills();
  220. var waitMills = 0;
  221. var reduce = currentMill - that.requestBlueTime;
  222. const dMills = 6 * 1000;
  223. if (reduce > 0 && reduce < dMills) {
  224. waitMills = dMills - reduce;
  225. }
  226. if (waitMills > 0) {
  227. await time_util.delayMills(waitMills);
  228. }
  229. if (callBackConnect == null && lastPageRoute == connectBleRoot) {
  230. that.doStartScaning = false;
  231. return;
  232. }
  233. wx.stopBluetoothDevicesDiscovery({
  234. success: (res) => {
  235. that.search(connectWillDevice, boolean, callBackConnect);
  236. },
  237. fail: (err) => {
  238. that.doStartScaning = false;
  239. that.requestBlueTime = time_util.getCurrentMills();
  240. if (boolean != null) {
  241. boolean(false);
  242. }
  243. }
  244. });
  245. }
  246. ///搜索设备
  247. search(connectWillDevice, boolean, callBackConnect) {
  248. var that = this;
  249. const time_util = require('./../../utils/time_util');
  250. wx.startBluetoothDevicesDiscovery({
  251. allowDuplicatesKey: true,
  252. success: function (res) {
  253. // that.getConnectedDevices();
  254. that.doStartScaning = false;
  255. that.requestBlueTime = time_util.getCurrentMills();
  256. if (boolean != null) {
  257. boolean(true);
  258. }
  259. that.setConnectWillDevice(connectWillDevice);
  260. that.setCallBackConnect(callBackConnect);
  261. that.compareList = [];
  262. },
  263. fail(err) {
  264. that.doStartScaning = false;
  265. that.requestBlueTime = time_util.getCurrentMills();
  266. if (boolean != null) {
  267. boolean(false);
  268. }
  269. },
  270. });
  271. }
  272. // 停止搜索
  273. stopScan() {
  274. var that = this;
  275. that.setCallBackConnect(null);
  276. that.setConnectWillDevice(null);
  277. return new Promise((resolve, reject) => {
  278. wx.stopBluetoothDevicesDiscovery({
  279. success: (res) => {
  280. resolve(res);
  281. },
  282. fail: (err) => {
  283. reject('停止搜索失败');
  284. }
  285. });
  286. });
  287. }
  288. ///开始连接设备
  289. startConnect(device) {
  290. return new Promise((resolve, reject) => {
  291. var deviceId = device.deviceId;
  292. wx.createBLEConnection({
  293. deviceId: deviceId,
  294. success: function (res) {
  295. resolve(true);
  296. },
  297. fail: function (res) {
  298. resolve(false);
  299. }
  300. });
  301. });
  302. }
  303. ///初始化 接收数据 获取特征值
  304. getNotifyServices(device) {
  305. return new Promise((resolve, reject) => {
  306. var deviceId = device.deviceId;
  307. wx.getBLEDeviceServices({
  308. deviceId: deviceId,
  309. success: function (res) {
  310. resolve(res.services);
  311. },
  312. fail: function (res) {
  313. resolve(null);
  314. }
  315. });
  316. });
  317. }
  318. ///获取设备真正的属性特征值
  319. getCharacteristics(device, services, index, notify, write, read, succeed, fail) {
  320. var that = this;
  321. var deviceId = device.deviceId;
  322. wx.getBLEDeviceCharacteristics({
  323. deviceId: deviceId,
  324. serviceId: services[index].uuid,
  325. success: function (res) {
  326. var notifyCharaterId = "";
  327. var notifyServiceId = "";
  328. for (var i = 0; i < res.characteristics.length; i++) {
  329. var properties = res.characteristics[i].properties;
  330. var charaterId = res.characteristics[i].uuid;
  331. if (!notify) {
  332. if (properties.notify) {
  333. notifyCharaterId = charaterId;
  334. notifyServiceId = services[index].uuid;
  335. notify = true;
  336. }
  337. }
  338. if (!write) {
  339. if (properties.write) {
  340. that.writeCharaterId = charaterId;
  341. that.writeServiceId = services[index].uuid;
  342. write = true;
  343. }
  344. }
  345. if (!read) {
  346. if (properties.read) {
  347. that.readCharaterId = charaterId;
  348. that.readServiceId = services[index].uuid;
  349. read = true;
  350. }
  351. }
  352. }
  353. if (!notify) {
  354. index++
  355. if (index == services.length) {
  356. fail();
  357. } else {
  358. that.getCharacteristics(device, services, index, notify, write, read, succeed, fail);
  359. }
  360. } else {
  361. succeed(notifyServiceId, notifyCharaterId);
  362. }
  363. }
  364. })
  365. }
  366. // 监听发送的数据
  367. monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId) {
  368. return new Promise((resolve, reject) => {
  369. var deviceId = device.deviceId;
  370. wx.notifyBLECharacteristicValueChange({
  371. state: true,
  372. deviceId: deviceId,
  373. serviceId: notifyServiceId,
  374. characteristicId: notifyCharaterId,
  375. success: function (res) {
  376. resolve(true);
  377. },
  378. fail: function (res) {
  379. resolve(false);
  380. }
  381. });
  382. });
  383. }
  384. ///收取到的数据转换为文字
  385. onBLECharacteristicValueChange(callback) {
  386. wx.onBLECharacteristicValueChange(function (r) {
  387. callback(r.value);
  388. });
  389. }
  390. // 断开设备的连接
  391. disconnect(device) {
  392. return new Promise((resolve, reject) => {
  393. var deviceId = device.deviceId;
  394. wx.closeBLEConnection({
  395. deviceId: deviceId,
  396. success: (res) => {
  397. resolve(true);
  398. },
  399. fail: (err) => {
  400. resolve(false);
  401. }
  402. });
  403. });
  404. }
  405. ///发送数据
  406. sendData(device, text) {
  407. return new Promise((resolve, reject) => {
  408. var that = this;
  409. var deviceId = device.deviceId;
  410. var buffer = new ArrayBuffer(text.length)
  411. var dataView = new Uint8Array(buffer)
  412. for (var i = 0; i < text.length; i++) {
  413. dataView[i] = text.charCodeAt(i)
  414. }
  415. wx.writeBLECharacteristicValue({
  416. deviceId: deviceId,
  417. serviceId: that.writeServiceId,
  418. characteristicId: that.writeCharaterId,
  419. value: buffer,
  420. success: function (res) {
  421. resolve(true);
  422. },
  423. fail(err) {
  424. resolve(false);
  425. }
  426. });
  427. });
  428. }
  429. }
  430. ///获取 所有搜索过的蓝牙设备
  431. // getConnectedDevices() {
  432. // var that = this;
  433. // const hex_util = require('./../../utils/hex_util');
  434. // wx.getBluetoothDevices({
  435. // success: (res) => {
  436. // if (that.callBackConnect != null) {
  437. // for (var i = 0; i < res.devices.length; i++) {
  438. // var temp = res.devices[i];
  439. // if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  440. // temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
  441. // if (that.callBackConnect != null) {
  442. // that.callBackConnect(temp);
  443. // }
  444. // break;
  445. // }
  446. // }
  447. // } else {
  448. // for (var i = 0; i < res.devices.length; i++) {
  449. // if (that.compareList.length > 0) {
  450. // var has = false;
  451. // for (var j = 0; j < that.compareList.length; j++) {
  452. // if (res.devices[i].name != "") {
  453. // if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  454. // has = true;
  455. // break;
  456. // }
  457. // }
  458. // }
  459. // if (!has) {
  460. // that.compareList.push(res.devices[i]);
  461. // }
  462. // } else {
  463. // that.compareList.push(res.devices[i]);
  464. // }
  465. // }
  466. // }
  467. // },
  468. // fail: (err) => {
  469. // console.error('获取蓝牙设备列表失败', err);
  470. // }
  471. // });
  472. // }