ble_manager.js 23 KB

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