ZXCar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. const common = require('../common.js');
  2. const FMBMS = require('./FMBMS.js');
  3. var bluetooth = require('@/common/bluetooth.js');
  4. const readServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  5. const readID = '000036F6-0000-1000-8000-00805F9B34FB';
  6. const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  7. const writeID = '000036F5-0000-1000-8000-00805F9B34FB';
  8. const MTU = 115;
  9. const app = getApp();
  10. let subIndex = -1
  11. let commands = []; // 升级包指令数组,每个指令是一个 ArrayBuffer
  12. let currentCommandIndex = 1;
  13. var totalLength = 0 //总包长度
  14. var joinPack = []//接收到的数据包
  15. var readRepeatTime = 0 //读到完整数据包的时间戳
  16. function acceptDevice(device) {
  17. return device.btid ? true : false;
  18. }
  19. function isSingleBt() {
  20. console.log('是单蓝牙');
  21. return true;
  22. }
  23. function haveBms() {
  24. console.log('是单蓝牙并且带bms');
  25. return true;
  26. }
  27. function isDevice(device, data) {
  28. // console.log(device,data,'device111');
  29. const advertisData = new Uint8Array(data.advertisData);
  30. // console.log(advertisData,'判断返回第几个字段是正常模式还是升级模式');
  31. //打印返回广播 判断返回字段是正常模式还是升级模式
  32. // console.log(advertisData.slice(4, 10).toString(),'device2222');
  33. const mac = device.btid
  34. .split('')
  35. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  36. .filter((p, i) => i % 2 == 0);
  37. // if ( advertisData.slice(4, 10).toString() == "095A5832") {
  38. // return true;
  39. // }
  40. //判断是否是智寻的蓝牙
  41. const result = data.name.startsWith("ZX");
  42. // if (data.name === "865416038002201") {
  43. // if (data.name === "ZX2503150000000") {
  44. if (result) {
  45. return true
  46. }
  47. return false;
  48. }
  49. //连接蓝牙后 发送登录指令
  50. function alterConnect(device, deviceId) {
  51. readRepeatTime=0
  52. // console.log(device,app.globalData.connectionState[deviceId],'deviceliuwei');
  53. //登录 crc[0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00]
  54. //登录 [[0x22,0x22,0x01,0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00,crc_data,0xAA,0xAA]]
  55. //时间戳 0x67AC53E7 0x65,0xB3,0xED,0x6F 16:00:05
  56. //切换正常,工厂下发指令 [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  57. //OTA 升级指令 [[0x22,0x22,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,n,0xCB,0xAA,0xAA]]
  58. // const data = [0x1F, 0x0F, ,0x5A,0x58, 0x35, 0x32, 0x30, 0x32, 0x32, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  59. let data=[ 0x1F, 0x0F]
  60. const device_name=app.globalData.connectionState[deviceId].deviceName
  61. const device_sn=stringToHexArray(device_name)
  62. data.push(...device_sn)
  63. data.push(0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00)
  64. // const data=[ 0x1F, 0x0F,0x38, 0x36, 0x35, 0x34, 0x31, 0x36, 0x30, 0x33, 0x38, 0x30, 0x30, 0x32, 0x32, 0x30, 0x31,0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00]
  65. // console.log(data,'datatest');
  66. const crc_data = '0x'+crc8IEEE8023(data).toString(16).toUpperCase()
  67. data.push(crc_data,0xAA,0xAA)
  68. data.unshift(0x22,0x22,0x01)
  69. return [data]
  70. }
  71. function crc8IEEE8023(data) {
  72. const poly = 0x07; // IEEE 802.3 polynomial x^8 + x^2 + x + 1
  73. let crc = 0x00;
  74. for (let i = 0; i < data.length; i++) {
  75. crc ^= data[i];
  76. for (let j = 0; j < 8; j++) {
  77. if (crc & 0x80) {
  78. crc = ((crc << 1) ^ poly) & 0xFF;
  79. } else {
  80. crc = (crc << 1) & 0xFF;
  81. }
  82. }
  83. }
  84. return crc;
  85. }
  86. function stringToHexArray(str) {
  87. const result = [];
  88. for (let i = 0; i < str.length; i++) {
  89. const charCode = str.charCodeAt(i); // 获取字符的 ASCII 码值
  90. const hexValue=`0x${charCode.toString(16).toUpperCase()}`
  91. const hexDataValue=parseInt(hexValue,16)
  92. result.push(hexDataValue);
  93. }
  94. return result;
  95. }
  96. //收到硬件方向软件方发送的数据
  97. function readData(device, value, data) {
  98. // console.log(value, 'test1111');
  99. var test = new Uint8Array(value);
  100. const bmsData=Array.from(test);
  101. const normalArray =packBmsData(bmsData)
  102. if(normalArray){
  103. console.log(normalArray[2],'normalArray---------------');
  104. switch(normalArray[2]){
  105. case 0x60:
  106. //登陆指令
  107. loginCmd(device.mac_id,normalArray)
  108. break;
  109. case 0x61||0x62||0x65||0x7F:
  110. //开机指令 关机 寻车 //设置灵敏度回复
  111. trunCarCommand(device.mac_id,normalArray)
  112. break;
  113. case 0x66:
  114. //升级指令
  115. checkSendNextCommand(device.mac_id,normalArray)
  116. break;
  117. case 0x7C:
  118. //接近解锁指令
  119. nearCarCmd(device.mac_id,normalArray)
  120. break;
  121. case 0x7E:
  122. //获取车辆接近解锁 是否配对 灵敏度信息
  123. getNearCarCmdInfo(device.mac_id,normalArray)
  124. break;
  125. }
  126. }
  127. // console.log(normalArray,currentCommandIndex-1,commands[currentCommandIndex],commands[currentCommandIndex-1], 'value**************************');
  128. // if((normalArray[0]==0x00) && (normalArray[1]==0x01) && normalArray.length==2){
  129. // switch (normalArray[1]) {
  130. // case 0x01:
  131. // uni.showModal({
  132. // title: '提示',
  133. // confirmText: '开始升级',
  134. // content: '登陆成功,当前处于升级模式是否开始升级',
  135. // success: function(res) {
  136. // if (res.confirm) {
  137. // currentCommandIndex=0
  138. // otaUpgrade().then(()=>{
  139. // //登陆成功 且当前为升级模式可以发送升级文件
  140. // sendNextCommand(device.mac_id)
  141. // })
  142. // } else {}
  143. // }
  144. // });
  145. // return [1,1,2,3]
  146. // break;
  147. // }
  148. // }else if(normalArray[0]==0x00&&normalArray.length==1){
  149. // // common.simpleToast('开机成功', 2000)
  150. // return '操作成功'
  151. // }else{
  152. // switch(normalArray[3]){
  153. // case 0x00:
  154. // sendNextCommand(device.mac_id)
  155. // break;
  156. // case 0x01:
  157. // uni.hideLoading();
  158. // console.log(normalArray,commands.length,currentCommandIndex-1,commands[currentCommandIndex-3],commands[currentCommandIndex-2],commands[currentCommandIndex-1], 'value**************************');
  159. // common.simpleToast('升级文件回复失败', 2000)
  160. // // test() 升级文件回复失败
  161. // break;
  162. // }
  163. // }
  164. return data;
  165. }
  166. function loginCmd(mac_id,data){
  167. const len=data.length-4
  168. if(data[len-1]==0x00 && data[len]==0x01){
  169. uni.showModal({
  170. title: '提示',
  171. confirmText: '开始升级',
  172. content: '登陆成功,当前处于升级模式是否开始升级',
  173. success: function(res) {
  174. if (res.confirm) {
  175. currentCommandIndex=0
  176. otaUpgrade().then(()=>{
  177. //登陆成功 且当前为升级模式可以发送升级文件
  178. sendNextCommand(device.mac_id)
  179. })
  180. } else {}
  181. }
  182. });
  183. }else if(data[4]==0x00 && data[4]==0x00){
  184. // uni.hideLoading();
  185. //正常模式
  186. console.log('登陆成功');
  187. if('lockType' in app.globalData.nearLockInfo ){
  188. if(app.globalData.nearLockInfo.lockType==0){
  189. console.log('获取接近解锁指令为关闭');
  190. }else{
  191. uni.hideLoading();
  192. console.log('获取接近解锁指令为成功');
  193. }
  194. }else{
  195. const getNearUnlockSetCmd=getNearUnlockSet()
  196. console.log(getNearUnlockSetCmd,'getNearUnlockSetCmd');
  197. writeData(mac_id,getNearUnlockSetCmd)
  198. }
  199. }
  200. }
  201. function checkSendNextCommand(mac_id,data){
  202. const len=data.length-4
  203. if(data[len]==0x00){
  204. sendNextCommand(device.mac_id)
  205. }else if(data[len]==0x01){
  206. uni.hideLoading();
  207. // console.log(normalArray,commands.length,currentCommandIndex-1,commands[currentCommandIndex-3],commands[currentCommandIndex-2],commands[currentCommandIndex-1], 'value**************************');
  208. common.simpleToast('升级文件回复失败')
  209. }
  210. }
  211. function trunCarCommand(mac_id,data){
  212. uni.hideLoading();
  213. const len=data.length-4
  214. if(data[len]==0x00){
  215. common.simpleToast('操作成功')
  216. }else if(data[len]==0x01){
  217. common.simpleToast('操作失败')
  218. }
  219. }
  220. function nearCarCmd(mac_id,data){
  221. uni.hideLoading();
  222. const len=data.length-4
  223. if(data[len]==0x00){
  224. common.simpleToast('操作成功')
  225. }else if(data[len]==0x01){
  226. common.simpleToast('操作失败')
  227. }
  228. }
  229. //处理蓝牙三段回复 综合成一个数组
  230. function packBmsData(value) {
  231. if (value.length!=0) {
  232. console.log(endTime - readRepeatTime);
  233. if (endTime - readRepeatTime > 8000) {//超过8秒,说明数据有问题,数据清空
  234. readRepeatTime = endTime
  235. joinPack = []
  236. }
  237. // console.log((value[0] == 0x22) && (value[1]== 0x22),joinPack,'000000');
  238. if((value[0] == 0x22) && (value[1]== 0x22)){//当开始符为标志符 代表接收包开始
  239. var endTime = new Date().getTime()
  240. totalLength = value[3]//包长度
  241. joinPack=value
  242. // console.log(joinPack,value,'111111');
  243. }else if(joinPack.length!=0 && (joinPack[0] == 0x22) && (joinPack[1]== 0x22)){
  244. const len=joinPack.length-1
  245. const diffJoinLength =joinPack.length-4
  246. readRepeatTime = endTime
  247. // console.log(totalLength,diffJoinLength,value,'2222222');
  248. if(totalLength > diffJoinLength){//内容长度符合为完整包
  249. joinPack.push(...value)
  250. return joinPack
  251. }else if(totalLength <= diffJoinLength){
  252. joinPack = []
  253. }
  254. }
  255. else{
  256. readRepeatTime = endTime
  257. joinPack = []
  258. return joinPack
  259. }
  260. }
  261. }
  262. function mergeUint8Array(arr1, arr2) {
  263. let len1 = arr1 ? arr1.length : 0;
  264. let len2 = arr2.length;
  265. let arr = new Uint8Array(len1 + len2);
  266. for (let i = 0; i < len1; i++) {
  267. arr[i] = arr1[i];
  268. }
  269. for (let i = 0; i < len2; i++) {
  270. arr[len1 + i] = arr2[i];
  271. }
  272. return arr;
  273. }
  274. //发送指令封装
  275. function sendCommand(cmd, data = []) {
  276. readRepeatTime=0
  277. let cmdData = [0x22, 0x22]
  278. // cmdData.push(cmd)
  279. //协议号 cmd
  280. //包长度 data.length
  281. const dataLen=parseInt(data.length.toString(16).toUpperCase(), 16)
  282. //信息内容 cmd
  283. //crc校验
  284. const pData=[dataLen,...data]
  285. const crcData='0x'+crc8IEEE8023(pData).toString(16).toUpperCase()
  286. cmdData.push(cmd,dataLen,...data,crcData,0xAA, 0xAA)
  287. // console.log(cmdData,'cmdDatacmdData');
  288. return cmdData
  289. }
  290. function turnOnCar(device, deviceId) {
  291. return [sendCommand(0x02,[0x02])];
  292. }
  293. function turnOffCar(device, deviceId) {
  294. return [sendCommand(0x03,[0x03])];
  295. }
  296. function findCarCmd(device, deviceId) {
  297. return [sendCommand(0x06,[0x06])];
  298. }
  299. function openCarSeat(device, deviceId) {
  300. return [sendCommand(0x07,[0x07])];
  301. }
  302. function openCarTrunk(device, deviceId) {
  303. return [sendCommand(0x08,[0x08])];
  304. }
  305. function getCarPressure(device, deviceId) {
  306. return [sendCommand(0x4A,[0x4A])];
  307. }
  308. function nearUnlock(device, deviceId) {
  309. return [sendCommand(0x4B,[0x02])];
  310. }
  311. function nearCloseUnlock(device, deviceId) {
  312. return [sendCommand(0x4B,[0x00])];
  313. }
  314. function getSensitivity(device, deviceId) {
  315. return [sendCommand(0x4F,[0x4F])];
  316. }
  317. function setSensitivity(device, deviceId,type) {
  318. return [sendCommand(0x4E,['0x4'+type])];
  319. }
  320. function setNearUnlock(device, deviceId,type) {
  321. return [sendCommand(0x4D,[0x4D])];
  322. }
  323. function getNearUnlockSet(device, deviceId,type) {
  324. return [sendCommand(0x4D,[0x4D])];
  325. }
  326. function getNearCarCmdInfo(mac_id,data){
  327. const pData={
  328. lockType:data[4],//接近解锁配置
  329. level:data[5],//感应等级
  330. status:data[6]//配对状态
  331. }
  332. console.log(pData,data,pData.status,'pData');
  333. app.globalData.nearLockInfo=pData
  334. if(pData.lockType==0x02||pData.lockType==0x01){
  335. console.log('已开启配对');
  336. common.simpleToast('感应解锁已开启,前往设置灵敏度页面')
  337. uni.navigateTo({ url: '/pages/bluetoothUnlock/unlockset' })
  338. uni.hideLoading();
  339. if( pData.status == 0x01){
  340. common.simpleToast('配对成功')
  341. return
  342. }else if(pData.status==0x00){
  343. common.simpleToast('未配对')
  344. }
  345. }else if(pData.lockType==0x00){
  346. console.log('未开启配对')
  347. const nearUnlockCmd=nearUnlock()
  348. console.log(nearUnlockCmd,'nearUnlockCmd');
  349. writeData(mac_id,nearUnlockCmd)
  350. }
  351. }
  352. function switchFactory(device, deviceId) {
  353. //切换正常,工厂模式指令
  354. //切换工厂模式 [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  355. //切换正常模式 [[0x22,0x22,0x54,0x01,0x00,0x15,0xAA,0xAA]]
  356. return [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  357. // return [sendCommand([0x54, 0x01, 0x01, 0x12])];
  358. }
  359. //升级按钮
  360. function otaUpgrade(device){
  361. const data = readBinBinarayToCommand()
  362. data.then(result=>{commands=result})
  363. // console.log(data,'datatest111');
  364. return data
  365. }
  366. async function readBinBinarayToCommand() {
  367. let res =await uni.request({
  368. url: 'https://opt.bms16.com/ota.BIN', // 文件的网络地址
  369. method: 'GET',
  370. responseType: 'arraybuffer', // 指定响应类型为 arraybuffer
  371. });
  372. const arrayBuffer=res[1].data
  373. const uint8Array = new Uint8Array(arrayBuffer); // 转换为 Uint8Array
  374. const test = splitArrayIntoChunks(uint8Array, 80); // 分割成长度为 80 的数组
  375. const data =makeArr(test)
  376. return data
  377. }
  378. function sendNextCommand(mac_id) {
  379. readRepeatTime=0
  380. // console.log(currentCommandIndex,commands.length,'开始发送指令');
  381. // console.log(commands[currentCommandIndex],'command');
  382. if (currentCommandIndex >= commands.length) {
  383. uni.hideLoading();
  384. common.simpleToast('所有指令发送完成', 2000)
  385. return;
  386. }
  387. const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == mac_id);
  388. if (deviceId == undefined) {
  389. console.log('deviceId == undefined');
  390. return ;
  391. }
  392. const command = [commands[currentCommandIndex]];
  393. if(command.length==0){
  394. return
  395. }
  396. // // 将数据转换为ArrayBuffer类型
  397. var buffer;
  398. buffer = common.toArrayBuffer(command.shift());
  399. // console.log(command,'command');
  400. uni.writeBLECharacteristicValue({
  401. deviceId: deviceId, // 替换为实际的设备 ID
  402. serviceId: writeServiceID, // 替换为实际的服务 ID
  403. characteristicId: writeID, // 替换为可写入的特征值 ID
  404. value: buffer,
  405. success() {
  406. // console.log('指令发送成功,等待设备响应',currentCommandIndex,commands[currentCommandIndex]);
  407. },
  408. fail(err) {
  409. console.error('指令发送失败', err);
  410. }
  411. });
  412. currentCommandIndex++;
  413. }
  414. function splitArrayIntoChunks(array, chunkSize) {
  415. const newArr=array.slice(16383)
  416. const numChunks = Math.ceil(newArr.length / chunkSize);
  417. return Array.from({ length: numChunks }, (_, i) => {
  418. return newArr.slice(i * chunkSize, (i + 1) * chunkSize);
  419. });
  420. }
  421. // 处理分割后的数组
  422. function makeArr(array) {
  423. const result = [];
  424. let endPackage;
  425. for (let i = 0; i < array.length; i ++) {
  426. endPackage = 0x00
  427. if((i + 1) >= array.length){
  428. //当前是最后一次循环
  429. endPackage = 0x01
  430. }
  431. // if(i==2) return
  432. //协议号 30 包长度未固定array.length+8 转16进制 固件类型0x00 升级包大小(总包大小) 125396字节-》0x00,0x01,0xE9,0xD4
  433. let startArray = []
  434. const packageLen=parseInt((array[i].length+8).toString(16).toUpperCase(), 16)
  435. // const totalPackageSize=[0x00,0x00,0x01,0xE9,0xD4]
  436. // const totalPackageSize=[0x00,0x00,0x01,0xeb,0xfc]
  437. const totalPackageSize=[0x00,0x00,0x01,0xAB,0xFC]
  438. startArray.push(packageLen,...totalPackageSize)
  439. //软件包id
  440. const ids=splitNumber(i) // 0x00,0x01
  441. startArray.push(...ids)
  442. //是否为最后一个包
  443. startArray.push(endPackage)
  444. //软件数据包
  445. const formattedChunk = array[i].map(byte => `0x${byte.toString(16).toUpperCase()}`); // 转换为 0xXX 格式
  446. startArray.push(...formattedChunk)
  447. // if(i>95&& i<105){
  448. // console.log(startArray,'startArray-------');
  449. // }
  450. //crc检验
  451. const crcStr='0x'+crc8IEEE8023(startArray).toString(16).toUpperCase()
  452. const testCrc=parseInt(crcStr,16)
  453. startArray.push(crcStr)
  454. startArray.push(0xAA,0xAA)
  455. startArray.unshift(0x22,0x22,0x30)
  456. result.push(startArray);
  457. }
  458. // result.push([0x22,0x22,0x30,0x58,0x00,0x00,0x00,0xE9,0xD4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2C,0xAA,0xAA])
  459. return result;
  460. }
  461. function splitNumber(num) {
  462. let highByte = (num >> 8) & 0xFF; // 对于0-255的数字,结果总是0
  463. // 低字节
  464. let lowByte = num & 0xFF;
  465. // 将每个字节转换为两位的十六进制字符串,并添加'0x'前缀
  466. let highHex = highByte.toString(16).toUpperCase().padStart(2, '0'); // 确保是两位十六进制数
  467. let lowHex = lowByte.toString(16).toUpperCase().padStart(2, '0'); // 确保是两位十六进制数
  468. // 返回结果数组(这里实际上可以返回一个对象或字符串,根据需求调整)
  469. return ['0x' + highHex, '0x' + lowHex];
  470. }
  471. // 定义一个函数,用于向蓝牙设备写入数据
  472. function writeData(mac_id, data, callback = () => {}, fail = () => {}) {
  473. const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == mac_id);
  474. if (deviceId == undefined) {
  475. return ;
  476. }
  477. // 如果数据长度为0,则直接返回
  478. if (data.length == 0) {
  479. return;
  480. }
  481. // 将数据转换为ArrayBuffer类型
  482. var buffer;
  483. buffer = common.toArrayBuffer(data.shift());
  484. // 调用uni.writeBLECharacteristicValue方法,向蓝牙设备写入数据
  485. uni.writeBLECharacteristicValue({
  486. deviceId: deviceId,
  487. serviceId: writeServiceID,
  488. characteristicId: writeID,
  489. value: buffer,
  490. // 成功回调函数
  491. success(res) {
  492. // 如果数据长度为0,则调用回调函数
  493. if (data.length == 0) {
  494. callback(res);
  495. } else {
  496. // 否则,延时500毫秒后再次调用writeData函数
  497. setTimeout(() => {
  498. writeData(device, deviceId, data, callback, fail);
  499. }, 500);
  500. }
  501. },
  502. // 失败回调函数
  503. fail(res) {
  504. console.log(res);
  505. // 调用失败回调函数
  506. fail(res);
  507. }
  508. });
  509. }
  510. module.exports = {
  511. readServiceID: readServiceID,
  512. readID: readID,
  513. writeServiceID: writeServiceID,
  514. writeID: writeID,
  515. MTU: MTU,
  516. acceptDevice: acceptDevice,
  517. isDevice: isDevice,
  518. alterConnect: alterConnect,
  519. readData: readData,
  520. turnOffCar: turnOffCar,
  521. isSingleBt: isSingleBt,
  522. haveBms: haveBms,
  523. switchFactory: switchFactory,
  524. otaUpgrade,
  525. findCarCmd,
  526. turnOffCar,
  527. turnOnCar,
  528. getCarPressure,
  529. nearUnlock,
  530. nearCloseUnlock,
  531. getSensitivity,
  532. setSensitivity,
  533. getNearUnlockSet,
  534. setNearUnlock,
  535. };