ble_manager.js 23 KB

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