xBlufi-wx-impl.js 30 KB

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