ble_manager.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // 引入必要的微信小程序 API
  2. // const wx = require('wx');
  3. class bleManager {
  4. constructor() {
  5. var that = this;
  6. that.hasPermission = false;
  7. that.publicDevice = null;
  8. that.connectWillDevice = null;
  9. that.callBackConnect = null;
  10. that.requestBlueTime = 0;
  11. ///正在执行扫描中
  12. that.isAvailable = false;
  13. that.doStartScaning = false;
  14. that.compareList = [];
  15. that.dissmissDevice = [];
  16. }
  17. ///获取比较的数据
  18. getCompareList() {
  19. return this.compareList;
  20. }
  21. setCompareList(compareList) {
  22. this.compareList = compareList;
  23. }
  24. getDissmissDevice() {
  25. return this.dissmissDevice;
  26. }
  27. setConnectWillDevice(connectWillDevice) {
  28. this.connectWillDevice = connectWillDevice;
  29. }
  30. getCallBackConnect() {
  31. return this.callBackConnect;
  32. }
  33. setCallBackConnect(callBackConnect) {
  34. this.callBackConnect = callBackConnect;
  35. }
  36. /// 监控蓝牙打开状态
  37. initBluetoothAdapter() {
  38. var that = this;
  39. wx.onBluetoothAdapterStateChange(function (res) {
  40. that.isAvailable = res.available;
  41. if (!that.isAvailable) {
  42. that.compareList = [];
  43. that.dissmissDevice = [];
  44. getCurrentPages()[0].closeBlueResetOffline(false, true);
  45. }
  46. });
  47. }
  48. ///监听搜索设备列表
  49. getBluetoothDevices() {
  50. var that = this;
  51. wx.onBluetoothDeviceFound(function (res) {
  52. ///第一种情况
  53. if (res.deviceId) {
  54. if (that.callBackConnect != null) {
  55. if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
  56. res.mac = res.advertisData ? that.buf2hex(res.advertisData) : '';
  57. // if (that.callBackConnect != null) {
  58. console.log("1111", res.mac);
  59. if (that.callBackConnect != null) {
  60. that.callBackConnect(res);
  61. }
  62. }
  63. } else {
  64. if (res.name != "") {
  65. if (that.compareList.length > 0) {
  66. var has = false;
  67. for (var i = 0; i < that.compareList.length; i++) {
  68. if (res.deviceId == that.compareList[i].deviceId) {
  69. has = true;
  70. break;
  71. }
  72. }
  73. if (!has) {
  74. that.compareList.push(res);
  75. }
  76. } else {
  77. that.compareList.push(res);
  78. }
  79. }
  80. }
  81. }
  82. ///第二种情况
  83. else if (res.devices) {
  84. if (that.callBackConnect != null) {
  85. for (var i = 0; i < res.devices.length; i++) {
  86. var temp = res.devices[i];
  87. // if (temp.name == "MW-SR1(4G_WIFI)") {
  88. // }
  89. // if (temp.name != null) {
  90. // console.log("2222", temp.name);
  91. // }
  92. if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  93. temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
  94. temp.mac2 = that.ab2hex(temp.advertisData ?? "")
  95. // if (that.callBackConnect != null) {
  96. console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp);
  97. if (that.callBackConnect != null) {
  98. // if (that.callBackConnect != null && (temp.deviceId != "6E:66:C2:CA:74:F8" || temp.deviceId != "1A:B4:E0:40:22:8B")) {
  99. that.callBackConnect(temp);
  100. }
  101. break;
  102. }
  103. }
  104. } else {
  105. for (var i = 0; i < res.devices.length; i++) {
  106. if (res.devices[i].name != "") {
  107. if (that.compareList.length > 0) {
  108. var has = false;
  109. for (var j = 0; j < that.compareList.length; j++) {
  110. if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  111. has = true;
  112. break;
  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. ///第三种情况
  126. else if (res[0]) {
  127. if (that.callBackConnect != null) {
  128. if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
  129. res[0].mac = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
  130. // if (that.callBackConnect != null) {
  131. console.log("3333", res.mac);
  132. if (that.callBackConnect != null) {
  133. that.callBackConnect(res[0]);
  134. }
  135. }
  136. } else {
  137. if (res[0].name != "") {
  138. if (that.compareList.length > 0) {
  139. var has = false;
  140. for (var i = 0; i < that.compareList.length; i++) {
  141. if (res[0].deviceId == that.compareList[i].deviceId) {
  142. has = true;
  143. break;
  144. }
  145. }
  146. if (!has) {
  147. that.compareList.push(res[0]);
  148. }
  149. } else {
  150. that.compareList.push(res[0]);
  151. }
  152. }
  153. }
  154. }
  155. });
  156. }
  157. onBLEConnectionStateChange(callback) {
  158. ///监听已连接或没有连接 连接和断开的时候会回调
  159. // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
  160. let that = this;
  161. wx.onBLEConnectionStateChange((result) => {
  162. if (result.connected) {
  163. for (var i = 0; i < that.dissmissDevice.length; i++) {
  164. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  165. that.dissmissDevice.splice(i, 1);
  166. break;
  167. }
  168. }
  169. } else {
  170. var has = false;
  171. for (var i = 0; i < that.dissmissDevice.length; i++) {
  172. if (result.deviceId == that.dissmissDevice[i].deviceId) {
  173. has = true;
  174. break;
  175. }
  176. }
  177. if (!has) {
  178. that.dissmissDevice.push(result);
  179. }
  180. }
  181. // that.disconnect(result)
  182. //断开连接是失败的, 只能监听到下线。
  183. if (callback != null) {
  184. callback(result);
  185. }
  186. // ///是否已配对
  187. // wx.isBluetoothDevicePaired({
  188. // deviceId: result.deviceId,
  189. // ///{"isPaired":false,"errno":0,"errMsg":"isBluetoothDevicePaired:ok"}
  190. // success: (res) => {
  191. // },
  192. // fail: (err) => {
  193. // }
  194. // })
  195. });
  196. }
  197. ///获取 所有搜索到的蓝牙设备
  198. getConnectedDevices() {
  199. var that = this;
  200. wx.getBluetoothDevices({
  201. success: (res) => {
  202. if (that.callBackConnect != null) {
  203. for (var i = 0; i < res.devices.length; i++) {
  204. var temp = res.devices[i];
  205. if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
  206. temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
  207. if (temp.mac) {
  208. console.log("搜索到的" + JSON.stringify(temp));
  209. }
  210. // if (that.callBackConnect != null) {
  211. if (that.callBackConnect != null) {
  212. // if (that.callBackConnect != null && temp.mac.includes("CF:CA")) {
  213. that.callBackConnect(temp);
  214. }
  215. break;
  216. }
  217. }
  218. } else {
  219. for (var i = 0; i < res.devices.length; i++) {
  220. if (that.compareList.length > 0) {
  221. var has = false;
  222. for (var j = 0; j < that.compareList.length; j++) {
  223. if (res.devices[i].name != "") {
  224. // if (res.devices[i].name == "MW-SR1(4G_WIFI)") {
  225. // console.log("gadsfqewrqewrqwerqrqr==000==" + JSON.stringify(res.devices[i]));
  226. // }
  227. if (res.devices[i].deviceId == that.compareList[j].deviceId) {
  228. has = true;
  229. break;
  230. }
  231. }
  232. }
  233. if (!has) {
  234. that.compareList.push(res.devices[i]);
  235. }
  236. } else {
  237. that.compareList.push(res.devices[i]);
  238. }
  239. }
  240. }
  241. },
  242. fail: (err) => {
  243. console.error('获取蓝牙设备列表失败', err);
  244. }
  245. });
  246. }
  247. ///获取数据
  248. buf2hex(buffer) {
  249. return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
  250. }
  251. ///获取毫秒
  252. getCurrentMills() {
  253. var currentDate = new Date();
  254. var currentTimeMillis = currentDate.getTime();
  255. // return Math.floor(currentTimeMillis / 1000);
  256. return currentTimeMillis;
  257. }
  258. // 等待多少秒
  259. delay(ms) {
  260. return new Promise(resolve => setTimeout(resolve, ms));
  261. }
  262. // 开始搜索蓝牙设备
  263. async startScan(connectWillDevice, boolean, callBackConnect) {
  264. var that = this;
  265. ///限制搜索没有打开蓝牙一直询问打开蓝牙
  266. if (!that.isAvailable) {
  267. if (callBackConnect == null) {
  268. return;
  269. }
  270. ///连接设备
  271. var isAvailable = await that.openBluetoothAdapter();
  272. if (!isAvailable) {
  273. boolean(false);
  274. return;
  275. }
  276. }
  277. const route_util = require('../utils/route_util');
  278. const route_constant = require('../utils/route_constant');
  279. const indexRoot = route_constant.indexRoot;
  280. const connectBleRoot = route_constant.connectBleRoot;
  281. var lastPageRoute = route_util.getLastPageRoute();
  282. if (lastPageRoute != indexRoot && lastPageRoute != connectBleRoot) {
  283. console.log("搜索蓝牙设备失败,请返回首页0");
  284. return;
  285. }
  286. ///蓝牙连接 做限制
  287. if (lastPageRoute == indexRoot) {
  288. if (that.doStartScaning == true) {
  289. console.log("搜索蓝牙设备失败,请返回首页1");
  290. return;
  291. }
  292. }
  293. that.doStartScaning = true;
  294. var currentMill = that.getCurrentMills();
  295. var waitMills = 0;
  296. var reduce = currentMill - that.requestBlueTime;
  297. const delayMiliis = 6 * 1000;
  298. if (reduce > 0 && reduce < delayMiliis) {
  299. waitMills = delayMiliis - reduce;
  300. }
  301. if (waitMills > 0) {
  302. await that.delay(waitMills);
  303. }
  304. if (callBackConnect == null && lastPageRoute == connectBleRoot) {
  305. that.doStartScaning = false;
  306. return;
  307. }
  308. wx.stopBluetoothDevicesDiscovery({
  309. success: (res) => {
  310. console.log("停止搜索设备0", res);
  311. that.search(connectWillDevice, boolean, callBackConnect);
  312. },
  313. fail: (err) => {
  314. console.log("停止搜索设备1", err);
  315. that.doStartScaning = false;
  316. that.requestBlueTime = that.getCurrentMills();
  317. if (boolean != null) {
  318. boolean(false);
  319. }
  320. }
  321. });
  322. }
  323. ///打开蓝牙适配
  324. openBluetoothAdapter() {
  325. var that = this;
  326. return new Promise((resolve, reject) => {
  327. wx.openBluetoothAdapter({
  328. success: function (res) {
  329. that.isAvailable = res.available;
  330. console.log("sdfasdfadsf==00==" + that.isAvailable + "===" + JSON.stringify(res));
  331. resolve(that.isAvailable);
  332. },
  333. fail: function (res) {
  334. console.log("sdfasdfadsf==11==" + that.isAvailable + "===" + JSON.stringify(res));
  335. if (res.errno == 103) {
  336. wx.showModal({
  337. title: '提示',
  338. content: '需要您的蓝牙权限才能使用此功能,是否前往设置授权?',
  339. showCancel: true,
  340. success: (modalRes) => {
  341. if (modalRes.confirm) {
  342. wx.openSetting({
  343. success: (settingRes) => {
  344. console.log("sdfasdfadsf==222==" + that.isAvailable + "===" + JSON.stringify(settingRes));
  345. if (settingRes.authSetting['scope.bluetooth']) {
  346. that.isAvailable = true;
  347. resolve(that.isAvailable);
  348. } else {
  349. that.isAvailable = false;
  350. resolve(that.isAvailable);
  351. }
  352. }
  353. });
  354. } else if (modalRes.cancel) {
  355. that.isAvailable = false;
  356. resolve(that.isAvailable);
  357. }
  358. }
  359. });
  360. } else {
  361. that.isAvailable = false;
  362. resolve(that.isAvailable);
  363. }
  364. }
  365. })
  366. });
  367. }
  368. search(connectWillDevice, boolean, callBackConnect) {
  369. var that = this;
  370. wx.startBluetoothDevicesDiscovery({
  371. allowDuplicatesKey: true,
  372. success: function (res) {
  373. console.log("开始搜索设备", res);
  374. that.getConnectedDevices();
  375. that.doStartScaning = false;
  376. that.requestBlueTime = that.getCurrentMills();
  377. if (boolean != null) {
  378. boolean(true);
  379. }
  380. that.setConnectWillDevice(connectWillDevice);
  381. that.setCallBackConnect(callBackConnect);
  382. that.compareList = [];
  383. },
  384. fail(err) {
  385. console.log("开始搜索设备失败", res);
  386. that.doStartScaning = false;
  387. that.requestBlueTime = that.getCurrentMills();
  388. if (boolean != null) {
  389. boolean(false);
  390. }
  391. },
  392. })
  393. }
  394. // 停止搜索
  395. stopSearch() {
  396. var that = this;
  397. that.setCallBackConnect(null);
  398. that.setConnectWillDevice(null);
  399. return new Promise((resolve, reject) => {
  400. wx.stopBluetoothDevicesDiscovery({
  401. success: (res) => {
  402. resolve(res);
  403. },
  404. fail: (err) => {
  405. reject('停止搜索失败');
  406. }
  407. });
  408. });
  409. }
  410. closeBle() {
  411. console.log('关闭蓝牙了')
  412. closeBluetoothAdapter();
  413. }
  414. // 断开与指定设备的连接
  415. disconnect(mDevice) {
  416. var that = this;
  417. const strings = require('../utils/strings');
  418. let device = that.publicDevice;
  419. if (mDevice != null) {
  420. device = mDevice;
  421. }
  422. if (strings.isEmpty(device)) {
  423. return;
  424. }
  425. let deviceId = device.deviceId
  426. if (strings.isEmpty(deviceId)) {
  427. return;
  428. }
  429. console.log('开始断开连接', device);
  430. return new Promise((resolve, reject) => {
  431. wx.closeBLEConnection({
  432. deviceId: deviceId,
  433. success: (res) => {
  434. console.log('成功断开连接', res);
  435. that.publicDevice = null;
  436. resolve(res);
  437. },
  438. fail: (err) => {
  439. console.log('断开连接失败', err);
  440. resolve(err);
  441. }
  442. });
  443. });
  444. }
  445. // 连接到指定设备
  446. async connectToDevice(device) {
  447. var that = this;
  448. return new Promise((resolve, reject) => {
  449. console.log("开始连接蓝牙:", device.deviceId)
  450. wx.createBLEConnection({
  451. deviceId: device.deviceId,
  452. success: (res) => {
  453. that.publicDevice = device
  454. console.log('连接成功:', res);
  455. resolve(true);
  456. },
  457. fail: (err) => {
  458. that.publicDevice = null
  459. console.error('连接失败:', err);
  460. resolve(false);
  461. }
  462. });
  463. });
  464. }
  465. // 发送数据到指定设备
  466. async sendData(data, callback) {
  467. var that = this
  468. if (that.publicDevice == null) {
  469. console.log("没有连接设备");
  470. return;
  471. }
  472. return new Promise((resolve, reject) => {
  473. var buffer = null;
  474. // todo 判断是否是buffer
  475. // if (needChange) {
  476. // buffer = data;
  477. // } else {
  478. buffer = new ArrayBuffer(data.length);
  479. // 下面是赋值,不能删
  480. const dataView = new DataView(buffer);
  481. data.forEach((value, index) => {
  482. dataView.setUint8(index, value); // 将每个16进制数值写入到 buffer 中
  483. });
  484. // }
  485. let logData = new Uint8Array(buffer);
  486. console.log('开始发送数据:', logData);
  487. wx.writeBLECharacteristicValue({
  488. deviceId: that.publicDevice.deviceId,
  489. serviceId: that.publicDevice.serviceId,
  490. characteristicId: that.publicDevice.characteristicId,
  491. value: buffer,
  492. success: (res) => {
  493. if (callback) {
  494. callback(true)
  495. }
  496. resolve(true);
  497. },
  498. fail: (err) => {
  499. if (callback) {
  500. callback(false)
  501. }
  502. console.log('数据发送失败:', err);
  503. that.errorDisconnect()
  504. resolve(false);
  505. }
  506. });
  507. });
  508. }
  509. ab2hex(buffer) {
  510. var hexArr = Array.prototype.map.call(
  511. new Uint8Array(buffer),
  512. function (bit) {
  513. return ('00' + bit.toString(16)).slice(-2)
  514. }
  515. )
  516. return hexArr.join(':');
  517. }
  518. fiterDevice(res) {
  519. var that = this;
  520. var devices = res.devices.filter(device => {
  521. const name = device.name || '';
  522. const localName = device.localName || '';
  523. let isNot = that.isNotEmpty(name) || that.isNotEmpty(localName);
  524. if (isNot) {
  525. // console.log('是猫王设备名称:', device.advertisData, device.serviceData)
  526. let mac = that.ab2hex(device.advertisData)
  527. // console.log(mac)
  528. device.mac = mac
  529. }
  530. return isNot
  531. });
  532. let newDevices = devices.map((device) => {
  533. let uuid = device.advertisServiceUUIDs[0] ?? ""
  534. return {
  535. deviceId: device.deviceId,
  536. name: device.name,
  537. localName: device.localName,
  538. uuid: uuid,
  539. mac: device.mac,
  540. connectable: device.connectable
  541. }
  542. });
  543. return newDevices
  544. }
  545. isNotEmpty(name) {
  546. let isNot = (name !== '' &&
  547. (name.startsWith("MW_") ||
  548. name.startsWith("MW-") ||
  549. // name.startsWith("猫王") ||
  550. // name.startsWith("妙播") ||
  551. // name.startsWith("AirSmart") ||
  552. name === "le")
  553. )
  554. // if (!isNot && name !== '') {
  555. // console.log('不是猫王设备名称:', name)
  556. // }
  557. return isNot;
  558. }
  559. // 发现服务
  560. discoverServices(deviceId) {
  561. var that = this;
  562. console.log('发现服务:', deviceId);
  563. return new Promise((resolve, reject) => {
  564. wx.getBLEDeviceServices({
  565. deviceId: deviceId,
  566. success: (res) => {
  567. // that.publicDevice .services = res.services;
  568. let service_id = "";
  569. for (let i = 0; i < res.services.length; i++) {
  570. if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1 ||
  571. res.services[i].uuid.toUpperCase().indexOf("FFC0") != -1
  572. // res.services[i].uuid.toUpperCase().indexOf("ae800") != -1
  573. ) {
  574. service_id = res.services[i].uuid;
  575. break;
  576. }
  577. console.log('发现服务1:', service_id);
  578. service_id = res.services[i].uuid;
  579. }
  580. that.publicDevice.serviceId = service_id;
  581. console.log('发现服务2:', service_id);
  582. resolve(service_id);
  583. // resolve(res.services);
  584. },
  585. fail: (err) => {
  586. that.publicDevice.serviceId = null;
  587. console.error('发现服务失败:', err);
  588. reject([]);
  589. }
  590. });
  591. });
  592. }
  593. connect() {}
  594. // 发现特征值 read / write
  595. discoverCharacteristics(deviceId, serviceId) {
  596. var that = this;
  597. console.log('发现特征值:' + deviceId + " , " + serviceId);
  598. // if (deviceId !== that.publicDevice .deviceId) {
  599. // console.log('设备id不匹配')
  600. // return false
  601. // }
  602. return new Promise((resolve, reject) => {
  603. wx.getBLEDeviceCharacteristics({
  604. deviceId: deviceId,
  605. serviceId: serviceId,
  606. success: (res) => {
  607. // that.characteristics[serviceId] = res.characteristics;
  608. console.log('发现特征值2:', res);
  609. // that.publicDevice .characteristics = res.characteristics;
  610. resolve(res.characteristics);
  611. },
  612. fail: (err) => {
  613. that.publicDevice.characteristics = null;
  614. console.error('发现特征值失败:', err);
  615. reject("");
  616. }
  617. });
  618. });
  619. }
  620. // 读取特征值
  621. readCharacteristicValue(characteristicId) {
  622. var that = ths;
  623. console.log('开始读取特征值', characteristicId)
  624. return new Promise((resolve, reject) => {
  625. wx.readBLECharacteristicValue({
  626. deviceId: that.publicDevice.deviceId,
  627. serviceId: that.publicDevice.serviceId,
  628. characteristicId: characteristicId,
  629. success: (res) => {
  630. const name = that.parseBLEValue(res.value); // 解析读取到的值
  631. console.log('读取特征值成功:', name, res);
  632. resolve(res);
  633. },
  634. fail: (err) => {
  635. console.error('读取特征值失败:', err);
  636. reject("");
  637. }
  638. });
  639. });
  640. }
  641. // 解析读取到的设备名称
  642. parseBLEValue(buffer) {
  643. return String.fromCharCode.apply(null, new Uint8Array(buffer));
  644. }
  645. // 监听特征值变化
  646. notifyCharacteristicValueChange(characteristicId, callback) {
  647. var that = this;
  648. console.log('监听特征值变化:', characteristicId, that.publicDevice.deviceId, that.publicDevice.serviceId);
  649. wx.notifyBLECharacteristicValueChange({
  650. deviceId: that.publicDevice.deviceId, //设备mac IOS和安卓系统不一样
  651. serviceId: that.publicDevice.serviceId, //服务通道,这里主要是notify
  652. characteristicId: characteristicId, //notify uuid
  653. state: true,
  654. success: function (res) {
  655. console.log("开启notify 成功")
  656. //TODO onBLECharacteristicValueChange 监听特征值 设备的数据在这里获取到
  657. wx.onBLECharacteristicValueChange(function (characteristic) {
  658. let buffer = characteristic.value
  659. let dataView = new DataView(buffer)
  660. let dataResult = []
  661. for (let i = 0; i < dataView.byteLength; i++) {
  662. // console.log("0x" + dataView.getUint8(i).toString(16))
  663. // dataResult.push("0x" + dataView.getUint8(i).toString(16))
  664. dataResult.push(dataView.getUint8(i))
  665. }
  666. const result = dataResult
  667. // console.log("拿到的数据:", result)
  668. if (callback) {
  669. callback(result)
  670. }
  671. })
  672. },
  673. fail: function (res) {
  674. console.log("订阅特征失败:", res)
  675. }
  676. })
  677. }
  678. setWrite(wirte, characteristicId) {
  679. var that = this;
  680. console.log('写入特征值:', characteristicId)
  681. // that.publicDevice .wirte = wirte
  682. that.publicDevice.characteristicId = characteristicId;
  683. }
  684. errorDisconnect() {
  685. this.disconnect();
  686. }
  687. }
  688. // const ble = new bleManager();
  689. // 导出 bleManager 类
  690. module.exports = bleManager;