xBlufi-wx-impl.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. const {
  2. isCN
  3. } = require('./../requestUtil.js');
  4. const {
  5. TextEncoder
  6. } = require('text-encoding');
  7. let tempTimer = 0;
  8. let client = null;
  9. let util = null
  10. let mDeviceEvent = null
  11. let crypto = null
  12. let md5 = null
  13. let aesjs = null
  14. const timeOut = 20; //超时时间
  15. var timeId = "";
  16. let sequenceControl = 0;
  17. let sequenceNumber = -1;
  18. let itue = true;
  19. let self = {
  20. data: {
  21. deviceId: null,
  22. isConnected: false,
  23. failure: false,
  24. value: 0,
  25. desc: "请耐心等待...",
  26. isChecksum: true,
  27. isEncrypt: true,
  28. flagEnd: false,
  29. defaultData: 1,
  30. ssidType: 2,
  31. passwordType: 3,
  32. meshIdType: 3,
  33. deviceId: "",
  34. ssid: "",
  35. uuid: "",
  36. serviceId: "",
  37. password: "",
  38. meshId: "",
  39. processList: [],
  40. result: [],
  41. service_uuid: "0000FFFF-0000-1000-8000-00805F9B34FB",
  42. characteristic_write_uuid: "0000FF01-0000-1000-8000-00805F9B34FB",
  43. characteristic_read_uuid: "0000FF02-0000-1000-8000-00805F9B34FB",
  44. customData: null,
  45. md5Key: 0,
  46. }
  47. }
  48. function buf2hex(buffer) {
  49. return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
  50. }
  51. function buf2string(buffer) {
  52. var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x);
  53. var str = '';
  54. for (var i = 0; i < arr.length; i++) {
  55. str += String.fromCharCode(arr[i]);
  56. }
  57. return str;
  58. }
  59. function getSsids(str) {
  60. var list = [],
  61. strs = str.split(":");
  62. for (var i = 0; i < strs.length; i++) {
  63. list.push(parseInt(strs[i], 16));
  64. }
  65. return list;
  66. }
  67. function getCharCodeat(str) {
  68. var list = [];
  69. const encoder = new TextEncoder('utf8');
  70. console.log(encoder);
  71. for (var i = 0; i < str.length; i++) {
  72. if (isCN(str[i])) {
  73. const t = encoder.encode(str[i]);
  74. for (var j = 0; j < t.length; j++) {
  75. list.push(t[j]);
  76. }
  77. } else {
  78. list.push(str.charCodeAt(i));
  79. }
  80. }
  81. return list;
  82. }
  83. //判断返回的数据是否加密
  84. function isEncrypt(fragNum, list, md5Key) {
  85. var checksum = [],
  86. checkData = [];
  87. if (fragNum[7] == "1") { //返回数据加密
  88. if (fragNum[6] == "1") {
  89. var len = list.length - 2;
  90. list = list.slice(0, len);
  91. }
  92. var iv = this.generateAESIV(parseInt(list[2], 16));
  93. if (fragNum[3] == "0") { //未分包
  94. list = list.slice(4);
  95. self.data.flagEnd = true
  96. } else { //分包
  97. list = list.slice(6);
  98. }
  99. } else { //返回数据未加密
  100. if (fragNum[6] == "1") {
  101. var len = list.length - 2;
  102. list = list.slice(0, len);
  103. }
  104. if (fragNum[3] == "0") { //未分包
  105. list = list.slice(4);
  106. self.data.flagEnd = true
  107. } else { //分包
  108. list = list.slice(6);
  109. }
  110. }
  111. return list;
  112. }
  113. function getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, data) {
  114. var obj = [],
  115. frameControl = 0;
  116. sequenceControl = parseInt(sequenceControl) + 1;
  117. if (!util._isEmpty(data)) {
  118. obj = util.isSubcontractor(data, true, sequenceControl);
  119. frameControl = util.getFrameCTRLValue(false, true, util.DIRECTION_OUTPUT, false, obj.flag);
  120. } else {
  121. data = [];
  122. data.push(util.NEG_SET_SEC_ALL_DATA);
  123. var pLength = pBytes.length;
  124. var pLen1 = (pLength >> 8) & 0xff;
  125. var pLen2 = pLength & 0xff;
  126. data.push(pLen1);
  127. data.push(pLen2);
  128. data = data.concat(pBytes);
  129. var gLength = gBytes.length;
  130. var gLen1 = (gLength >> 8) & 0xff;
  131. var gLen2 = gLength & 0xff;
  132. data.push(gLen1);
  133. data.push(gLen2);
  134. data = data.concat(gBytes);
  135. var kLength = kBytes.length;
  136. var kLen1 = (kLength >> 8) & 0xff;
  137. var kLen2 = kLength & 0xff;
  138. data.push(kLen1);
  139. data.push(kLen2);
  140. data = data.concat(kBytes);
  141. obj = util.isSubcontractor(data, true, sequenceControl);
  142. frameControl = util.getFrameCTRLValue(false, true, util.DIRECTION_OUTPUT, false, obj.flag);
  143. }
  144. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_NEG, frameControl, sequenceControl, obj.len, obj.lenData);
  145. var typedArray = new Uint8Array(value);
  146. wx.writeBLECharacteristicValue({
  147. deviceId: deviceId,
  148. serviceId: serviceId,
  149. characteristicId: characteristicId,
  150. value: typedArray.buffer,
  151. success: function (res) {
  152. if (obj.flag) {
  153. getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, obj.laveData);
  154. }
  155. },
  156. fail: function (res) {}
  157. })
  158. }
  159. function writeDeviceRouterInfoStart(deviceId, serviceId, characteristicId, data) {
  160. var obj = {},
  161. frameControl = 0;
  162. sequenceControl = parseInt(sequenceControl) + 1;
  163. if (!util._isEmpty(data)) {
  164. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  165. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  166. } else {
  167. obj = util.isSubcontractor([self.data.defaultData], self.data.isChecksum, sequenceControl, true);
  168. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  169. }
  170. var defaultData = util.encrypt(aesjs, self.data.md5Key, sequenceControl, obj.lenData, true);
  171. var value = util.writeData(util.PACKAGE_CONTROL_VALUE, util.SUBTYPE_WIFI_MODEl, frameControl, sequenceControl, obj.len, defaultData);
  172. var typedArray = new Uint8Array(value);
  173. wx.writeBLECharacteristicValue({
  174. deviceId: deviceId,
  175. serviceId: serviceId,
  176. characteristicId: characteristicId,
  177. value: typedArray.buffer,
  178. success: function (res) {
  179. if (obj.flag) {
  180. writeDeviceRouterInfoStart(deviceId, serviceId, characteristicId, obj.laveData);
  181. } else {
  182. writeRouterSsid(deviceId, serviceId, characteristicId, null);
  183. }
  184. },
  185. fail: function (res) {}
  186. })
  187. }
  188. function writeCutomsData(deviceId, serviceId, characteristicId, data) {
  189. var obj = {},
  190. frameControl = 0;
  191. sequenceControl = parseInt(sequenceControl) + 1;
  192. if (!util._isEmpty(data)) {
  193. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  194. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  195. } else {
  196. var ssidData = getCharCodeat(self.data.customData);
  197. obj = util.isSubcontractor(ssidData, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  198. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  199. }
  200. var defaultData = util.encrypt(aesjs, self.data.md5Key, sequenceControl, obj.lenData, true);
  201. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_CUSTOM_DATA, frameControl, sequenceControl, obj.len, defaultData);
  202. var typedArray = new Uint8Array(value);
  203. wx.writeBLECharacteristicValue({
  204. deviceId: deviceId,
  205. serviceId: serviceId,
  206. characteristicId: characteristicId,
  207. value: typedArray.buffer,
  208. success: function (res) {
  209. if (obj.flag) {
  210. writeCutomsData(deviceId, serviceId, characteristicId, obj.laveData);
  211. }
  212. },
  213. fail: function (res) {
  214. //console.log(257);
  215. }
  216. })
  217. }
  218. function writeRouterSsid(deviceId, serviceId, characteristicId, data) {
  219. var obj = {},
  220. frameControl = 0;
  221. sequenceControl = parseInt(sequenceControl) + 1;
  222. if (!util._isEmpty(data)) {
  223. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  224. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  225. } else {
  226. var ssidData = getCharCodeat(self.data.ssid);
  227. obj = util.isSubcontractor(ssidData, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  228. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  229. }
  230. var defaultData = util.encrypt(aesjs, self.data.md5Key, sequenceControl, obj.lenData, true);
  231. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_SET_SSID, frameControl, sequenceControl, obj.len, defaultData);
  232. var typedArray = new Uint8Array(value);
  233. wx.writeBLECharacteristicValue({
  234. deviceId: deviceId,
  235. serviceId: serviceId,
  236. characteristicId: characteristicId,
  237. value: typedArray.buffer,
  238. success: function (res) {
  239. if (obj.flag) {
  240. writeRouterSsid(deviceId, serviceId, characteristicId, obj.laveData);
  241. } else {
  242. writeDevicePwd(deviceId, serviceId, characteristicId, null);
  243. }
  244. },
  245. fail: function (res) {
  246. //console.log(257);
  247. }
  248. })
  249. }
  250. function writeDevicePwd(deviceId, serviceId, characteristicId, data) {
  251. var obj = {},
  252. frameControl = 0;
  253. sequenceControl = parseInt(sequenceControl) + 1;
  254. if (!util._isEmpty(data)) {
  255. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  256. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  257. } else {
  258. var pwdData = getCharCodeat(self.data.password);
  259. obj = util.isSubcontractor(pwdData, self.data.isChecksum, sequenceControl, self.data.isEncrypt);
  260. frameControl = util.getFrameCTRLValue(self.data.isEncrypt, self.data.isChecksum, util.DIRECTION_OUTPUT, false, obj.flag);
  261. }
  262. var defaultData = util.encrypt(aesjs, self.data.md5Key, sequenceControl, obj.lenData, true);
  263. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_SET_PWD, frameControl, sequenceControl, obj.len, defaultData);
  264. var typedArray = new Uint8Array(value);
  265. wx.writeBLECharacteristicValue({
  266. deviceId: deviceId,
  267. serviceId: serviceId,
  268. characteristicId: characteristicId,
  269. value: typedArray.buffer,
  270. success: function (res) {
  271. if (obj.flag) {
  272. writeDevicePwd(deviceId, serviceId, characteristicId, obj.laveData);
  273. } else {
  274. writeDeviceEnd(deviceId, serviceId, characteristicId, null);
  275. }
  276. },
  277. fail: function (res) {}
  278. })
  279. }
  280. function writeDeviceEnd(deviceId, serviceId, characteristicId) {
  281. sequenceControl = parseInt(sequenceControl) + 1;
  282. var frameControl = util.getFrameCTRLValue(self.data.isEncrypt, false, util.DIRECTION_OUTPUT, false, false);
  283. var value = util.writeData(self.data.PACKAGE_CONTROL_VALUE, util.SUBTYPE_END, frameControl, sequenceControl, 0, null);
  284. var typedArray = new Uint8Array(value);
  285. wx.writeBLECharacteristicValue({
  286. deviceId: deviceId,
  287. serviceId: serviceId,
  288. characteristicId: characteristicId,
  289. value: typedArray.buffer,
  290. success: function (res) {
  291. },
  292. fail: function (res) {
  293. }
  294. })
  295. }
  296. function init() {
  297. let mOnFire = require("other/onfire.js");
  298. mDeviceEvent = require('xBlufi.js');
  299. util = require('../../utils/blufi/util.js');
  300. crypto = require('../../utils/blufi/crypto/crypto-dh.js');
  301. md5 = require('../../utils/blufi/crypto/md5.min.js');
  302. aesjs = require('../../utils/blufi/crypto/aes.js');
  303. wx.onBLEConnectionStateChange(function (res) {
  304. let obj = {
  305. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_STATUS_CONNECTED,
  306. 'result': res.connected,
  307. 'data': res
  308. }
  309. mDeviceEvent.notifyDeviceMsgEvent(obj);
  310. })
  311. mDeviceEvent.listenStartDiscoverBle(true, function (options) {
  312. if (options.isStart) {
  313. //第一步检查蓝牙适配器是否可用
  314. wx.onBluetoothAdapterStateChange(function (res) {
  315. if (!res.available) {}
  316. });
  317. if (!itue) {
  318. return;
  319. };
  320. itue = false;
  321. //第二步关闭适配器,重新来搜索
  322. wx.closeBluetoothAdapter({
  323. complete: function (res) {
  324. wx.openBluetoothAdapter({
  325. success: function (res) {
  326. wx.getBluetoothAdapterState({
  327. success: function (res) {
  328. wx.stopBluetoothDevicesDiscovery({
  329. success: function (res) {
  330. itue = true;
  331. let devicesList = [];
  332. let countsTimes = 0;
  333. wx.onBluetoothDeviceFound(function (devices) {
  334. //剔除重复设备,兼容不同设备API的不同返回值
  335. var isnotexist = true;
  336. if (devices.deviceId) {
  337. if (devices.advertisData) {
  338. devices.advertisData = buf2hex(devices.advertisData)
  339. } else {
  340. devices.advertisData = ''
  341. }
  342. for (var i = 0; i < devicesList.length; i++) {
  343. if (devices.deviceId === devicesList[i].deviceId) {
  344. isnotexist = false
  345. }
  346. }
  347. if (isnotexist) {
  348. devicesList.push(devices)
  349. }
  350. } else if (devices.devices) {
  351. if (devices.devices[0].advertisData) {
  352. devices.devices[0].advertisData = buf2hex(devices.devices[0].advertisData)
  353. } else {
  354. devices.devices[0].advertisData = ''
  355. }
  356. for (var i = 0; i < devicesList.length; i++) {
  357. if (devices.devices[0].deviceId == devicesList[i].deviceId) {
  358. isnotexist = false
  359. }
  360. }
  361. if (isnotexist) {
  362. devicesList.push(devices.devices[0])
  363. }
  364. } else if (devices[0]) {
  365. if (devices[0].advertisData) {
  366. devices[0].advertisData = buf2hex(devices[0].advertisData)
  367. } else {
  368. devices[0].advertisData = ''
  369. }
  370. for (var i = 0; i < devices_list.length; i++) {
  371. if (devices[0].deviceId == devicesList[i].deviceId) {
  372. isnotexist = false
  373. }
  374. }
  375. if (isnotexist) {
  376. devicesList.push(devices[0])
  377. }
  378. }
  379. let obj = {
  380. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS,
  381. 'result': true,
  382. 'data': devicesList
  383. }
  384. mDeviceEvent.notifyDeviceMsgEvent(obj);
  385. })
  386. wx.startBluetoothDevicesDiscovery({
  387. allowDuplicatesKey: true,
  388. success: function (res) {
  389. let obj = {
  390. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  391. 'result': true,
  392. 'data': res
  393. }
  394. mDeviceEvent.notifyDeviceMsgEvent(obj);
  395. //开始扫码,清空列表
  396. devicesList.length = 0;
  397. },
  398. fail: function (res) {
  399. let obj = {
  400. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  401. 'result': false,
  402. 'data': res
  403. }
  404. mDeviceEvent.notifyDeviceMsgEvent(obj);
  405. }
  406. });
  407. },
  408. fail: function (res) {
  409. itue = true;
  410. let obj = {
  411. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  412. 'result': false,
  413. 'data': res
  414. }
  415. mDeviceEvent.notifyDeviceMsgEvent(obj);
  416. }
  417. });
  418. },
  419. fail: function (res) {
  420. itue = true;
  421. let obj = {
  422. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  423. 'result': false,
  424. 'data': res
  425. }
  426. mDeviceEvent.notifyDeviceMsgEvent(obj);
  427. }
  428. });
  429. },
  430. fail: function (res) {
  431. itue = true;
  432. let obj = {
  433. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  434. 'result': false,
  435. 'data': res
  436. }
  437. mDeviceEvent.notifyDeviceMsgEvent(obj);
  438. }
  439. });
  440. }
  441. });
  442. } else {
  443. wx.stopBluetoothDevicesDiscovery({
  444. success: function (res) {
  445. clearInterval(tempTimer);
  446. let obj = {
  447. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
  448. 'result': true,
  449. 'data': res
  450. }
  451. mDeviceEvent.notifyDeviceMsgEvent(obj);
  452. },
  453. fail: function (res) {
  454. let obj = {
  455. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
  456. 'result': false,
  457. 'data': res
  458. }
  459. mDeviceEvent.notifyDeviceMsgEvent(obj);
  460. }
  461. })
  462. }
  463. })
  464. mDeviceEvent.listenConnectBle(true, function (options) {
  465. console.log("我要连接====" + options.isStart);
  466. if (options.isStart)
  467. wx.createBLEConnection({
  468. deviceId: options.deviceId,
  469. success: function (res) {
  470. self.data.deviceId = options.deviceId
  471. mDeviceEvent.notifyDeviceMsgEvent({
  472. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
  473. 'result': true,
  474. 'data': {
  475. deviceId: options.deviceId,
  476. name: options.name
  477. },
  478. });
  479. },
  480. fail: function (res) {
  481. self.data.deviceId = null
  482. mDeviceEvent.notifyDeviceMsgEvent({
  483. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
  484. 'result': false,
  485. 'data': res,
  486. });
  487. }
  488. });
  489. else wx.closeBLEConnection({
  490. deviceId: options.deviceId,
  491. success: function (res) {
  492. console.log('断开成功')
  493. self.data.deviceId = null
  494. mDeviceEvent.notifyDeviceMsgEvent({
  495. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
  496. 'result': true,
  497. 'data': {
  498. deviceId: options.deviceId,
  499. name: options.name
  500. }
  501. });
  502. },
  503. fail: function (res) {
  504. self.data.deviceId = null
  505. mDeviceEvent.notifyDeviceMsgEvent({
  506. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
  507. 'result': false,
  508. 'data': res,
  509. });
  510. }
  511. })
  512. })
  513. mDeviceEvent.listenInitBleEsp32(true, function (options) {
  514. sequenceControl = 0;
  515. sequenceNumber = -1;
  516. self = null
  517. self = {
  518. data: {
  519. deviceId: null,
  520. isConnected: false,
  521. failure: false,
  522. value: 0,
  523. desc: "请耐心等待...",
  524. isChecksum: true,
  525. isEncrypt: true,
  526. flagEnd: false,
  527. defaultData: 1,
  528. ssidType: 2,
  529. passwordType: 3,
  530. meshIdType: 3,
  531. deviceId: "",
  532. ssid: "",
  533. uuid: "",
  534. serviceId: "",
  535. password: "",
  536. meshId: "",
  537. processList: [],
  538. result: [],
  539. service_uuid: "0000FFFF-0000-1000-8000-00805F9B34FB",
  540. characteristic_write_uuid: "0000FF01-0000-1000-8000-00805F9B34FB",
  541. characteristic_read_uuid: "0000FF02-0000-1000-8000-00805F9B34FB",
  542. customData: null,
  543. md5Key: 0,
  544. }
  545. }
  546. let deviceId = options.deviceId
  547. self.data.deviceId = options.deviceId
  548. wx.getBLEDeviceServices({
  549. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  550. deviceId: deviceId,
  551. success: function (res) {
  552. var services = res.services;
  553. if (services.length > 0) {
  554. for (var i = 0; i < services.length; i++) {
  555. if (services[i].uuid === self.data.service_uuid) {
  556. var serviceId = services[i].uuid;
  557. wx.getBLEDeviceCharacteristics({
  558. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  559. deviceId: deviceId,
  560. serviceId: serviceId,
  561. success: function (res) {
  562. var list = res.characteristics;
  563. if (list.length > 0) {
  564. for (var i = 0; i < list.length; i++) {
  565. var uuid = list[i].uuid;
  566. if (uuid == self.data.characteristic_write_uuid) {
  567. self.data.serviceId = serviceId;
  568. self.data.uuid = uuid;
  569. wx.notifyBLECharacteristicValueChange({
  570. state: true, // 启用 notify 功能
  571. deviceId: deviceId,
  572. serviceId: serviceId,
  573. characteristicId: list[1].uuid,
  574. success: function (res) {
  575. let characteristicId = self.data.characteristic_write_uuid
  576. //通知设备交互方式(是否加密) start
  577. client = util.blueDH(util.DH_P, util.DH_G, crypto);
  578. var kBytes = util.uint8ArrayToArray(client.getPublicKey());
  579. var pBytes = util.hexByInt(util.DH_P);
  580. var gBytes = util.hexByInt(util.DH_G);
  581. var pgkLength = pBytes.length + gBytes.length + kBytes.length + 6;
  582. var pgkLen1 = (pgkLength >> 8) & 0xff;
  583. var pgkLen2 = pgkLength & 0xff;
  584. var data = [];
  585. data.push(util.NEG_SET_SEC_TOTAL_LEN);
  586. data.push(pgkLen1);
  587. data.push(pgkLen2);
  588. var frameControl = util.getFrameCTRLValue(false, false, util.DIRECTION_OUTPUT, false, false);
  589. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_NEG, frameControl, sequenceControl, data.length, data);
  590. var typedArray = new Uint8Array(value);
  591. wx.writeBLECharacteristicValue({
  592. deviceId: deviceId,
  593. serviceId: serviceId,
  594. characteristicId: characteristicId,
  595. value: typedArray.buffer,
  596. success: function (res) {
  597. getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, null);
  598. },
  599. fail: function (res) {
  600. let obj = {
  601. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  602. 'result': false,
  603. 'data': res
  604. }
  605. mDeviceEvent.notifyDeviceMsgEvent(obj);
  606. }
  607. })
  608. //通知设备交互方式(是否加密) end
  609. wx.onBLECharacteristicValueChange(function (res) {
  610. let list2 = (util.ab2hex(res.value));
  611. // start
  612. let result = self.data.result;
  613. if (list2.length < 4) {
  614. cosnole.log(407);
  615. return false;
  616. }
  617. var val = parseInt(list2[0], 16),
  618. type = val & 3,
  619. subType = val >> 2;
  620. var dataLength = parseInt(list2[3], 16);
  621. if (dataLength == 0) {
  622. return false;
  623. }
  624. var fragNum = util.hexToBinArray(list2[1]);
  625. list2 = isEncrypt(fragNum, list2, self.data.md5Key);
  626. result = result.concat(list2);
  627. self.data.result = result
  628. if (self.data.flagEnd) {
  629. self.data.flagEnd = false
  630. if (type == 1) {
  631. let what = [];
  632. switch (subType) {
  633. case 15:
  634. if (result.length == 3) {
  635. mDeviceEvent.notifyDeviceMsgEvent({
  636. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT,
  637. 'result': false,
  638. 'data': {
  639. 'progress': 0,
  640. 'ssid': what.join('')
  641. }
  642. });
  643. } else {
  644. for (var i = 0; i <= result.length; i++) {
  645. var num = parseInt(result[i], 16) + "";
  646. if (i > 12) what.push(String.fromCharCode(parseInt(result[i], 16)));
  647. }
  648. mDeviceEvent.notifyDeviceMsgEvent({
  649. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT,
  650. 'result': true,
  651. 'data': {
  652. 'progress': 100,
  653. 'ssid': what.join('')
  654. }
  655. });
  656. }
  657. break;
  658. case 19: //自定义数据
  659. let customData = [];
  660. for (var i = 0; i <= result.length; i++) {
  661. customData.push(String.fromCharCode(parseInt(result[i], 16)));
  662. }
  663. let obj = {
  664. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA,
  665. 'result': true,
  666. 'data': customData.join('')
  667. }
  668. mDeviceEvent.notifyDeviceMsgEvent(obj);
  669. break;
  670. case util.SUBTYPE_NEGOTIATION_NEG:
  671. var arr = util.hexByInt(result.join(""));
  672. var clientSecret = client.computeSecret(new Uint8Array(arr));
  673. var md5Key = md5.array(clientSecret);
  674. self.data.md5Key = md5Key;
  675. mDeviceEvent.notifyDeviceMsgEvent({
  676. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  677. 'result': true,
  678. 'data': {
  679. deviceId,
  680. serviceId,
  681. characteristicId
  682. }
  683. });
  684. break;
  685. default:
  686. console.log(468);
  687. //self.setFailProcess(true, util.descFailList[4])
  688. console.log("入网失败 468 :", util.failList[4]);
  689. break;
  690. }
  691. self.data.result = []
  692. } else {
  693. //console.log(472);
  694. console.log("入网失败 472:", util.failList[4]);
  695. }
  696. }
  697. // end
  698. })
  699. },
  700. fail: function (res) {
  701. let obj = {
  702. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  703. 'result': false,
  704. 'data': res
  705. }
  706. mDeviceEvent.notifyDeviceMsgEvent(obj);
  707. }
  708. })
  709. }
  710. }
  711. }
  712. },
  713. fail: function (res) {
  714. let obj = {
  715. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  716. 'result': false,
  717. 'data': res
  718. }
  719. mDeviceEvent.notifyDeviceMsgEvent(obj);
  720. console.log("fail getBLEDeviceCharacteristics:" + JSON.stringify(res))
  721. }
  722. })
  723. break;
  724. }
  725. }
  726. }
  727. },
  728. fail: function (res) {
  729. let obj = {
  730. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  731. 'result': false,
  732. 'data': res
  733. }
  734. mDeviceEvent.notifyDeviceMsgEvent(obj);
  735. console.log("fail getBLEDeviceServices:" + JSON.stringify(res))
  736. }
  737. })
  738. })
  739. mDeviceEvent.listenSendRouterSsidAndPassword(true, function (options) {
  740. self.data.password = options.password
  741. self.data.ssid = options.ssid
  742. writeDeviceRouterInfoStart(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
  743. })
  744. mDeviceEvent.listenSendCustomData(true, function (options) {
  745. self.data.customData = options.customData
  746. writeCutomsData(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
  747. })
  748. }
  749. function ab2hex(buffer) {
  750. var hexArr = Array.prototype.map.call(
  751. new Uint8Array(buffer),
  752. function (bit) {
  753. return ('00' + bit.toString(16)).slice(-2)
  754. }
  755. )
  756. return hexArr.join('');
  757. }
  758. /****************************** 对外 ***************************************/
  759. module.exports = {
  760. init: init,
  761. };