ble_manager.js 23 KB

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