xBlufi-wx-impl.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. const {
  2. isCN
  3. } = require('./../util.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. });
  318. if (!itue) {
  319. return;
  320. };
  321. itue = false;
  322. //第二步关闭适配器,重新来搜索
  323. wx.closeBluetoothAdapter({
  324. complete: function (res) {
  325. wx.openBluetoothAdapter({
  326. success: function (res) {
  327. wx.getBluetoothAdapterState({
  328. success: function (res) {
  329. wx.stopBluetoothDevicesDiscovery({
  330. success: function (res) {
  331. itue = true;
  332. let devicesList = [];
  333. let countsTimes = 0;
  334. wx.onBluetoothDeviceFound(function (devices) {
  335. //剔除重复设备,兼容不同设备API的不同返回值
  336. var isnotexist = true;
  337. if (devices.deviceId) {
  338. if (devices.advertisData) {
  339. devices.advertisData = buf2hex(devices.advertisData)
  340. } else {
  341. devices.advertisData = ''
  342. }
  343. for (var i = 0; i < devicesList.length; i++) {
  344. if (devices.deviceId === devicesList[i].deviceId) {
  345. isnotexist = false
  346. }
  347. }
  348. if (isnotexist) {
  349. devicesList.push(devices)
  350. }
  351. } else if (devices.devices) {
  352. if (devices.devices[0].advertisData) {
  353. devices.devices[0].advertisData = buf2hex(devices.devices[0].advertisData)
  354. } else {
  355. devices.devices[0].advertisData = ''
  356. }
  357. for (var i = 0; i < devicesList.length; i++) {
  358. if (devices.devices[0].deviceId == devicesList[i].deviceId) {
  359. isnotexist = false
  360. }
  361. }
  362. if (isnotexist) {
  363. devicesList.push(devices.devices[0])
  364. }
  365. } else if (devices[0]) {
  366. if (devices[0].advertisData) {
  367. devices[0].advertisData = buf2hex(devices[0].advertisData)
  368. } else {
  369. devices[0].advertisData = ''
  370. }
  371. for (var i = 0; i < devices_list.length; i++) {
  372. if (devices[0].deviceId == devicesList[i].deviceId) {
  373. isnotexist = false
  374. }
  375. }
  376. if (isnotexist) {
  377. devicesList.push(devices[0])
  378. }
  379. }
  380. let obj = {
  381. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS,
  382. 'result': true,
  383. 'data': devicesList
  384. }
  385. mDeviceEvent.notifyDeviceMsgEvent(obj);
  386. })
  387. wx.startBluetoothDevicesDiscovery({
  388. allowDuplicatesKey: true,
  389. success: function (res) {
  390. let obj = {
  391. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  392. 'result': true,
  393. 'data': res
  394. }
  395. mDeviceEvent.notifyDeviceMsgEvent(obj);
  396. //开始扫码,清空列表
  397. devicesList.length = 0;
  398. },
  399. fail: function (res) {
  400. let obj = {
  401. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  402. 'result': false,
  403. 'data': res
  404. }
  405. mDeviceEvent.notifyDeviceMsgEvent(obj);
  406. }
  407. });
  408. },
  409. fail: function (res) {
  410. itue = true;
  411. let obj = {
  412. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  413. 'result': false,
  414. 'data': res
  415. }
  416. mDeviceEvent.notifyDeviceMsgEvent(obj);
  417. }
  418. });
  419. },
  420. fail: function (res) {
  421. itue = true;
  422. let obj = {
  423. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  424. 'result': false,
  425. 'data': res
  426. }
  427. mDeviceEvent.notifyDeviceMsgEvent(obj);
  428. }
  429. });
  430. },
  431. fail: function (res) {
  432. itue = true;
  433. let obj = {
  434. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
  435. 'result': false,
  436. 'data': res
  437. }
  438. mDeviceEvent.notifyDeviceMsgEvent(obj);
  439. }
  440. });
  441. }
  442. });
  443. } else {
  444. wx.stopBluetoothDevicesDiscovery({
  445. success: function (res) {
  446. clearInterval(tempTimer);
  447. let obj = {
  448. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
  449. 'result': true,
  450. 'data': res
  451. }
  452. mDeviceEvent.notifyDeviceMsgEvent(obj);
  453. },
  454. fail: function (res) {
  455. let obj = {
  456. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
  457. 'result': false,
  458. 'data': res
  459. }
  460. mDeviceEvent.notifyDeviceMsgEvent(obj);
  461. }
  462. })
  463. }
  464. })
  465. mDeviceEvent.listenConnectBle(true, function (options) {
  466. console.log("我要连接====" + options.isStart);
  467. if (options.isStart)
  468. wx.createBLEConnection({
  469. deviceId: options.deviceId,
  470. success: function (res) {
  471. self.data.deviceId = options.deviceId
  472. mDeviceEvent.notifyDeviceMsgEvent({
  473. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
  474. 'result': true,
  475. 'data': {
  476. deviceId: options.deviceId,
  477. name: options.name
  478. },
  479. });
  480. },
  481. fail: function (res) {
  482. self.data.deviceId = null
  483. mDeviceEvent.notifyDeviceMsgEvent({
  484. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
  485. 'result': false,
  486. 'data': res,
  487. });
  488. }
  489. });
  490. else wx.closeBLEConnection({
  491. deviceId: options.deviceId,
  492. success: function (res) {
  493. console.log('断开成功')
  494. self.data.deviceId = null
  495. mDeviceEvent.notifyDeviceMsgEvent({
  496. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
  497. 'result': true,
  498. 'data': {
  499. deviceId: options.deviceId,
  500. name: options.name
  501. }
  502. });
  503. },
  504. fail: function (res) {
  505. self.data.deviceId = null
  506. mDeviceEvent.notifyDeviceMsgEvent({
  507. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
  508. 'result': false,
  509. 'data': res,
  510. });
  511. }
  512. })
  513. })
  514. mDeviceEvent.listenInitBleEsp32(true, function (options) {
  515. sequenceControl = 0;
  516. sequenceNumber = -1;
  517. self = null
  518. self = {
  519. data: {
  520. deviceId: null,
  521. isConnected: false,
  522. failure: false,
  523. value: 0,
  524. desc: "请耐心等待...",
  525. isChecksum: true,
  526. isEncrypt: true,
  527. flagEnd: false,
  528. defaultData: 1,
  529. ssidType: 2,
  530. passwordType: 3,
  531. meshIdType: 3,
  532. deviceId: "",
  533. ssid: "",
  534. uuid: "",
  535. serviceId: "",
  536. password: "",
  537. meshId: "",
  538. processList: [],
  539. result: [],
  540. service_uuid: "0000FFFF-0000-1000-8000-00805F9B34FB",
  541. characteristic_write_uuid: "0000FF01-0000-1000-8000-00805F9B34FB",
  542. characteristic_read_uuid: "0000FF02-0000-1000-8000-00805F9B34FB",
  543. customData: null,
  544. md5Key: 0,
  545. }
  546. }
  547. let deviceId = options.deviceId
  548. self.data.deviceId = options.deviceId
  549. wx.getBLEDeviceServices({
  550. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  551. deviceId: deviceId,
  552. success: function (res) {
  553. var services = res.services;
  554. if (services.length > 0) {
  555. for (var i = 0; i < services.length; i++) {
  556. if (services[i].uuid === self.data.service_uuid) {
  557. var serviceId = services[i].uuid;
  558. wx.getBLEDeviceCharacteristics({
  559. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  560. deviceId: deviceId,
  561. serviceId: serviceId,
  562. success: function (res) {
  563. var list = res.characteristics;
  564. if (list.length > 0) {
  565. for (var i = 0; i < list.length; i++) {
  566. var uuid = list[i].uuid;
  567. if (uuid == self.data.characteristic_write_uuid) {
  568. self.data.serviceId = serviceId;
  569. self.data.uuid = uuid;
  570. wx.notifyBLECharacteristicValueChange({
  571. state: true, // 启用 notify 功能
  572. deviceId: deviceId,
  573. serviceId: serviceId,
  574. characteristicId: list[1].uuid,
  575. success: function (res) {
  576. let characteristicId = self.data.characteristic_write_uuid
  577. //通知设备交互方式(是否加密) start
  578. client = util.blueDH(util.DH_P, util.DH_G, crypto);
  579. var kBytes = util.uint8ArrayToArray(client.getPublicKey());
  580. var pBytes = util.hexByInt(util.DH_P);
  581. var gBytes = util.hexByInt(util.DH_G);
  582. var pgkLength = pBytes.length + gBytes.length + kBytes.length + 6;
  583. var pgkLen1 = (pgkLength >> 8) & 0xff;
  584. var pgkLen2 = pgkLength & 0xff;
  585. var data = [];
  586. data.push(util.NEG_SET_SEC_TOTAL_LEN);
  587. data.push(pgkLen1);
  588. data.push(pgkLen2);
  589. var frameControl = util.getFrameCTRLValue(false, false, util.DIRECTION_OUTPUT, false, false);
  590. var value = util.writeData(util.PACKAGE_VALUE, util.SUBTYPE_NEG, frameControl, sequenceControl, data.length, data);
  591. var typedArray = new Uint8Array(value);
  592. wx.writeBLECharacteristicValue({
  593. deviceId: deviceId,
  594. serviceId: serviceId,
  595. characteristicId: characteristicId,
  596. value: typedArray.buffer,
  597. success: function (res) {
  598. getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, null);
  599. },
  600. fail: function (res) {
  601. let obj = {
  602. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  603. 'result': false,
  604. 'data': res
  605. }
  606. mDeviceEvent.notifyDeviceMsgEvent(obj);
  607. }
  608. })
  609. //通知设备交互方式(是否加密) end
  610. wx.onBLECharacteristicValueChange(function (res) {
  611. let list2 = (util.ab2hex(res.value));
  612. // start
  613. let result = self.data.result;
  614. if (list2.length < 4) {
  615. cosnole.log(407);
  616. return false;
  617. }
  618. var val = parseInt(list2[0], 16),
  619. type = val & 3,
  620. subType = val >> 2;
  621. var dataLength = parseInt(list2[3], 16);
  622. if (dataLength == 0) {
  623. return false;
  624. }
  625. var fragNum = util.hexToBinArray(list2[1]);
  626. list2 = isEncrypt(fragNum, list2, self.data.md5Key);
  627. result = result.concat(list2);
  628. self.data.result = result
  629. if (self.data.flagEnd) {
  630. self.data.flagEnd = false
  631. if (type == 1) {
  632. let what = [];
  633. switch (subType) {
  634. case 15:
  635. if (result.length == 3) {
  636. mDeviceEvent.notifyDeviceMsgEvent({
  637. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT,
  638. 'result': false,
  639. 'data': {
  640. 'progress': 0,
  641. 'ssid': what.join('')
  642. }
  643. });
  644. } else {
  645. for (var i = 0; i <= result.length; i++) {
  646. var num = parseInt(result[i], 16) + "";
  647. if (i > 12) what.push(String.fromCharCode(parseInt(result[i], 16)));
  648. }
  649. mDeviceEvent.notifyDeviceMsgEvent({
  650. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT,
  651. 'result': true,
  652. 'data': {
  653. 'progress': 100,
  654. 'ssid': what.join('')
  655. }
  656. });
  657. }
  658. break;
  659. case 19: //自定义数据
  660. let customData = [];
  661. for (var i = 0; i <= result.length; i++) {
  662. customData.push(String.fromCharCode(parseInt(result[i], 16)));
  663. }
  664. let obj = {
  665. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA,
  666. 'result': true,
  667. 'data': customData.join('')
  668. }
  669. mDeviceEvent.notifyDeviceMsgEvent(obj);
  670. break;
  671. case util.SUBTYPE_NEGOTIATION_NEG:
  672. var arr = util.hexByInt(result.join(""));
  673. var clientSecret = client.computeSecret(new Uint8Array(arr));
  674. var md5Key = md5.array(clientSecret);
  675. self.data.md5Key = md5Key;
  676. mDeviceEvent.notifyDeviceMsgEvent({
  677. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  678. 'result': true,
  679. 'data': {
  680. deviceId,
  681. serviceId,
  682. characteristicId
  683. }
  684. });
  685. break;
  686. default:
  687. console.log(468);
  688. //self.setFailProcess(true, util.descFailList[4])
  689. console.log("入网失败 468 :", util.failList[4]);
  690. break;
  691. }
  692. self.data.result = []
  693. } else {
  694. //console.log(472);
  695. console.log("入网失败 472:", util.failList[4]);
  696. }
  697. }
  698. // end
  699. })
  700. },
  701. fail: function (res) {
  702. let obj = {
  703. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  704. 'result': false,
  705. 'data': res
  706. }
  707. mDeviceEvent.notifyDeviceMsgEvent(obj);
  708. }
  709. })
  710. }
  711. }
  712. }
  713. },
  714. fail: function (res) {
  715. let obj = {
  716. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  717. 'result': false,
  718. 'data': res
  719. }
  720. mDeviceEvent.notifyDeviceMsgEvent(obj);
  721. console.log("fail getBLEDeviceCharacteristics:" + JSON.stringify(res))
  722. }
  723. })
  724. break;
  725. }
  726. }
  727. }
  728. },
  729. fail: function (res) {
  730. let obj = {
  731. 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
  732. 'result': false,
  733. 'data': res
  734. }
  735. mDeviceEvent.notifyDeviceMsgEvent(obj);
  736. console.log("fail getBLEDeviceServices:" + JSON.stringify(res))
  737. }
  738. })
  739. })
  740. mDeviceEvent.listenSendRouterSsidAndPassword(true, function (options) {
  741. self.data.password = options.password
  742. self.data.ssid = options.ssid
  743. writeDeviceRouterInfoStart(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
  744. })
  745. mDeviceEvent.listenSendCustomData(true, function (options) {
  746. self.data.customData = options.customData
  747. writeCutomsData(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
  748. })
  749. }
  750. function ab2hex(buffer) {
  751. var hexArr = Array.prototype.map.call(
  752. new Uint8Array(buffer),
  753. function (bit) {
  754. return ('00' + bit.toString(16)).slice(-2)
  755. }
  756. )
  757. return hexArr.join('');
  758. }
  759. /****************************** 对外 ***************************************/
  760. module.exports = {
  761. init: init,
  762. };