ble_manager.js 22 KB

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