ble_manager.js 23 KB

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