ble_manager.js 23 KB

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