xBlufi-wx-impl.js 33 KB

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