BLE_WIFI_Scale_Server_Api 服务器与wifi秤交互只需要实现3个接口:设备注册、获取用户、上传记录
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.inet.ailink.receiver.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. public class Base64TeaUitls {
  4. //加密
  5. public static String encrypt(String dataValue){
  6. byte[] old = BytesUtils.getBytes(Integer.parseInt(dataValue));
  7. String result = "";
  8. try {
  9. //tea加密
  10. byte[] teaDscrypt =TeaUtils.encrypt(old,null);
  11. //base编码
  12. result= Base64Util.encoderString(teaDscrypt);
  13. } catch (UnsupportedEncodingException e) {
  14. return result;
  15. }
  16. return result;
  17. }
  18. //解密
  19. public static byte[] decrypt(String base64Text){
  20. //base解码
  21. String base64TextEn;
  22. try {
  23. base64TextEn = Base64Util.decoderString(base64Text);
  24. //解码数据转换为byte数组
  25. byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn);
  26. //tea解密byte数组
  27. byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
  28. //将负数转换为整数
  29. /*if(base64TeaByte != null){
  30. for(int i=0;i<base64TeaByte.length;i++){
  31. if(base64TeaByte[i]<0){
  32. base64TeaByte[i] = (byte) (256 + base64TeaByte[i]);
  33. }
  34. }
  35. }*/
  36. return base64TeaByte;
  37. } catch (UnsupportedEncodingException e) {
  38. return null;
  39. }
  40. }
  41. /**
  42. * 十进制转16进制
  43. * @param n
  44. * @return
  45. */
  46. private static String intToHex(int n) {
  47. StringBuilder sb = new StringBuilder(8);
  48. String a;
  49. char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  50. if(n==0){
  51. return "00";
  52. }
  53. while(n != 0){
  54. if(n<0){
  55. n= 256 +n;
  56. }
  57. sb = sb.append(b[n%16]);
  58. n = n/16;
  59. }
  60. a = sb.reverse().toString();
  61. if(a.length() == 1){
  62. a= "0"+a;
  63. }
  64. return a;
  65. }
  66. /**
  67. * 获取开始下标到结束下标的16进制的值
  68. * @param start
  69. * @param end
  70. * @param params
  71. * @return
  72. */
  73. public static String getStartToEndForHex(int start,int end,byte[] params){
  74. String result = "";
  75. for(int i=start;i<=end;i++){
  76. if(i==end){
  77. result = result+intToHex(params[i]);
  78. }else{
  79. result = result+intToHex(params[i])+":";
  80. }
  81. // System.out.println(params[i]+":"+intToHex(params[i]));
  82. }
  83. return result;
  84. }
  85. /**
  86. * 获取开始下标到结束下标的10进制的值
  87. * @param start
  88. * @param end
  89. * @param params
  90. * @return
  91. */
  92. public static String getStartToEndForInt(int start,int end,byte[] params){
  93. String result = "";
  94. for(int i=start;i<=end;i++){
  95. int temp = params[i];
  96. if(temp < 0){
  97. temp = 256 + temp;
  98. }
  99. if(i==end){
  100. result = result+temp;
  101. }else{
  102. result = result+temp+":";
  103. }
  104. }
  105. return result;
  106. }
  107. /*public static String getLowToHighEndForInt(int indexLow,int indexHigh,byte[] params){
  108. String result = "";
  109. int tempLow = params[indexLow];
  110. if(tempLow < 0){
  111. tempLow = 256 + tempLow;
  112. }
  113. int tempHigh = params[indexHigh];
  114. if(tempHigh < 0){
  115. tempHigh = 256 + tempHigh;
  116. }
  117. int tempResult = (tempLow << 8) | tempHigh;
  118. return result+tempResult;
  119. }*/
  120. /**
  121. * 获取低位到高位的移位+或运算的值
  122. * @param indexLow
  123. * @param indexHigh
  124. * @param params
  125. * @return
  126. */
  127. public static String getLowConactHighEndForInt(int indexLow,int indexHigh,byte[] params){
  128. String result = "";
  129. int tempResult = 0;
  130. for(int i = indexLow;i<=indexHigh;i++){
  131. int temp = (params[i] < 0 ? 256 + params[i] : params[i]);
  132. int tempmov = (indexHigh-i)+2;
  133. tempmov = (i == indexHigh ? 0 : (int) Math.pow(2, tempmov));
  134. tempResult = (temp << tempmov) | tempResult;
  135. }
  136. return result+tempResult;
  137. }
  138. public static String byteToHex(byte b){
  139. String hex = Integer.toHexString(b & 0xFF);
  140. if(hex.length() < 2){
  141. hex = "0" + hex;
  142. }
  143. return hex;
  144. }
  145. /**
  146. * 获取开始下标到结束下标的ASIII对应的值
  147. * @param start
  148. * @param end
  149. * @param params
  150. * @return
  151. */
  152. public static String getStartToEndForASIII(int start,int end,byte[] params){
  153. String result = "";
  154. for(int i=start;i<=end;i++){
  155. if(i==end){
  156. result = result+asciiToString(params[i]+"");
  157. }else{
  158. result = result+asciiToString(params[i]+"")+":";
  159. }
  160. }
  161. return result;
  162. }
  163. /**
  164. * ASCII转字符
  165. * @param value
  166. * @return
  167. */
  168. public static String asciiToString(String value)
  169. {
  170. value = value.replaceAll(":", "");
  171. char c= (char) Integer.parseInt(value);
  172. //System.out.println(value+":"+c);
  173. return c+"";
  174. }
  175. public static void main(String args[]) throws UnsupportedEncodingException
  176. {
  177. // String str = "H4CpBA/xguYaSidJuBnCEydjVVdwDBKAfgvuaUpSEuU=";
  178. // byte[] paramsByte = Base64TeaUitls.decrypt(str);
  179. //
  180. // for(byte i : paramsByte)
  181. // System.out.print(byteToHex(i) + " ");
  182. // System.out.println();
  183. //01 01 16 b7 28 32 18 4a 88 00 0e 00 00 00 00 42 4d 10 01 0a 00 13 05 07 e9
  184. //注册设备数据
  185. // byte[] old =new byte[]{
  186. // (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址
  187. // ,(byte)0x00 ,(byte)0x00 //cid
  188. // ,(byte)0x00 ,(byte)0x01 //pid
  189. // ,(byte)0x00 ,(byte)0x02 //vid
  190. // ,(byte)0x42 ,(byte)0x4d //产品名称
  191. // ,(byte)0x01 //产品型号
  192. // ,(byte)0x01 //硬件版本
  193. // ,(byte)0x42 //软件版本
  194. // ,(byte)0x4d //用户版本
  195. // ,(byte)0x13 //年
  196. // ,(byte)0x0c //月
  197. // ,(byte)0x12 //日
  198. // ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id
  199. // ,(byte)0x09 //设备单位
  200. // ,(byte)0x03 //设备IMEI长度
  201. // ,(byte)0x01,(byte)0x02,(byte)0x20 }; //设备IMEI
  202. //上报称重数据(原始数据)
  203. //5781
  204. // byte[] old =new byte[]{
  205. // (byte)0x00,(byte)0x00 ,(byte)0x0f,(byte)0x71 //设备SN号
  206. // ,(byte)0x15 //年
  207. // ,(byte)0x03 //月
  208. // ,(byte)0x04 //日
  209. // ,(byte)0x0c //时
  210. // ,(byte)0x00 //分
  211. // ,(byte)0x00 //秒
  212. // ,(byte)0x00 //是否离线记录
  213. // ,(byte)0x00 //体重数据高
  214. // ,(byte)0x00 //体重数据中
  215. // ,(byte)0x3C //体重数据低
  216. // ,(byte)0x00 //体重单位
  217. // ,(byte)0x00 //体重精度
  218. // ,(byte)0x01 //阻抗高
  219. // ,(byte)0xff //阻抗低
  220. // ,(byte)0x5f //心率
  221. // ,(byte)0x01 //算法
  222. // ,(byte)0x00,(byte)0x00,(byte)0x03,(byte)0x97}; //用户id
  223. //最新OTA升级地址
  224. // byte[] old =new byte[]{
  225. // (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址
  226. // ,(byte)0x00 ,(byte)0x00 //cid
  227. // ,(byte)0x00 ,(byte)0x01 //pid
  228. // ,(byte)0x00 ,(byte)0x02 //vid
  229. // ,(byte)0x42 ,(byte)0x4d //产品名称
  230. // ,(byte)0x01 //产品型号
  231. // ,(byte)0x01 //硬件版本
  232. // ,(byte)0x09 //软件版本
  233. // ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id
  234. // ,(byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF //设备id
  235. // ,(byte)0x00 //客户定制版本
  236. // ,(byte)0x00 ,(byte)0x00,(byte)0x00,(byte)0x00 //保留位
  237. // };
  238. // //通过cid获取设备访问地址
  239. byte[] old =new byte[]{
  240. (byte)0x00 ,(byte)0x00,(byte)0x00, //模块名称
  241. (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //mac地址
  242. (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //芯片id
  243. (byte)0x00 ,(byte)0x11, //cid
  244. (byte)0x00 ,(byte)0x01, //pid
  245. (byte)0x00 ,(byte)0x1B, //vid
  246. (byte)0x00,(byte)0x00 ,(byte)0x00 //保留位
  247. };
  248. //通过cid,pid,vid获取设备访问地址
  249. //57 4d 05 88 4a 18 32 28 a4 02 38 6f a5 dc 12 00 11 00 00 00 00 00 00 00
  250. // byte[] old =new byte[]{
  251. // (byte)0x57 ,(byte)0x4d,(byte)0x05, //模块名称
  252. // (byte)0x88,(byte)0x4a,(byte)0x08 ,(byte)0x32 ,(byte)0x28 ,(byte)0xa4, //mac地址
  253. // (byte)0x02,(byte)0x38,(byte)0x6f ,(byte)0xa5 ,(byte)0xdc ,(byte)0x12, //芯片id
  254. // (byte)0x00 ,(byte)0x0e, //cid
  255. // (byte)0x00 ,(byte)0x01, //pid
  256. // (byte)0x00 ,(byte)0x32, //vid
  257. // (byte)0x00,(byte)0x00 ,(byte)0x00 //保留位
  258. // };
  259. //刷牙数据上传
  260. // byte[] old =new byte[]{
  261. // (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号
  262. // (byte)0x01, //牙刷工作模式
  263. // (byte)0x00, // 刷牙时长高字节
  264. // (byte)0x03, // 刷牙时长低字节
  265. // (byte)0x00, //左边刷牙时长高字节
  266. // (byte)0x02, //左边刷牙时长低字节
  267. // (byte)0x00, //右边刷牙时长低字节
  268. // (byte)0x01, //右边刷牙时长低字节
  269. // (byte)0x09, //剩余电量
  270. // (byte)0x00,(byte)0x04, //默认刷牙时长
  271. // (byte)0x00,(byte)0x00,//保留位
  272. // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
  273. // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00 //保留位
  274. // };
  275. //血糖数据
  276. // byte[] old =new byte[]{
  277. // (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号
  278. // (byte)0x00, //血糖数据高字节
  279. // (byte)0x00, //血糖数据中字节
  280. // (byte)0x12, //血糖数据低字节
  281. // (byte)0x01, //血糖单位
  282. // (byte)0x01, //小数点
  283. // (byte)0x00, //状态码
  284. // (byte)0x10, //剩余电量
  285. // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
  286. // (byte)0x00 //保留位
  287. // };
  288. /*int a= 1846;
  289. System.out.println("原始数字:"+a);
  290. byte[] old = BytesUtils.getBytes(a);*/
  291. //通过设备id,获取设备单位
  292. // byte[] old =new byte[]{
  293. // (byte)0x00,(byte)0x00 ,(byte)0x0F,(byte)0x72, //设备id
  294. // (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位
  295. // };
  296. //通过设备id,修改设备单位
  297. // byte[] old =new byte[]{
  298. // (byte)0x00,(byte)0x00 ,(byte)0x0B,(byte)0x31, //设备id
  299. // (byte)0x01 //设备单位
  300. // };
  301. //注册设备数据
  302. // byte[] old =new byte[]{
  303. // (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00, (byte)0x00 //设备MAC
  304. // , (byte)0x00, (byte)0x2c//CID
  305. // , (byte)0x00 , (byte)0x00//PID
  306. // , (byte)0x00 , (byte)0x00//VID
  307. // , (byte)0x4c//模块名称
  308. // , (byte)0x4d
  309. // , (byte)0x09
  310. // , (byte)0x00
  311. // , (byte)0x0a
  312. // , (byte)0x00 , (byte)0x15 , (byte)0x04 , (byte)0x07 , (byte)0x00 , (byte)0x00
  313. // , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00
  314. // , (byte)0x0f //设备IMEI长度
  315. // , (byte)0x08 , (byte)0x06 , (byte)0x09 , (byte)0x03 , (byte)0x02 , (byte)0x04 , (byte)0x00 , (byte)0x05 //设备IMEI 86932405
  316. // , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x02 , (byte)0x07 , (byte)0x06 , (byte)0x09 //设备IMEI 0002769
  317. // , (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 }; //备用字段
  318. //原始数据
  319. System.out.println( "原始数据:");
  320. for(byte i : old)
  321. System.out.print(byteToHex(i) + " ");
  322. System.out.println();
  323. //tea加密
  324. byte[] teaDscrypt =TeaUtils.encrypt(old,null);
  325. System.out.println( "tea加密:");
  326. for(byte i : teaDscrypt)
  327. System.out.print(byteToHex(i) + " ");
  328. System.out.println();
  329. //base编码
  330. String base64Text = Base64Util.encoderString(teaDscrypt);
  331. base64Text = "rvXiSvrWrR+/p1ZhAPlXr7x1gxbvrurA";
  332. System.out.println("base64编码后的数据长度:"+Base64Util.getLength(base64Text)+":"+base64Text);
  333. //base解码
  334. String base64TextEn= Base64Util.decoderString(base64Text);
  335. //String base64TextEn= Base64Util.decoderString("BCc0vnfr9kd5");
  336. // System.out.println("base64解码后的数据长度:"+Base64Util.getLength(base64TextEn)+":"+base64TextEn);
  337. /*//转称byte数组(每两位一个16进制byte)
  338. List<String> base64TeaByteOldList = new ArrayList<String>();
  339. for(int i=0;i<base64TextEn.length();i++){
  340. if(i%2 == 0){
  341. base64TeaByteOldList.add("0x"+base64TextEn.charAt(i)+base64TextEn.charAt(i+1));
  342. }
  343. }
  344. //解码后转换得到的list
  345. System.out.println("解码后转换得到的list:");
  346. for(String byte16:base64TeaByteOldList){
  347. System.out.print(byte16+" ");
  348. }
  349. System.out.println();*/
  350. //解码数据转换为byte数组
  351. byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn);
  352. System.out.println( "解码后得到的tea加密的byte数组:");
  353. for(byte i : base64TeaByteOld)
  354. System.out.print(byteToHex(i) + " ");
  355. System.out.println();
  356. byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
  357. System.out.println( "tea解密:");
  358. for(byte i : base64TeaByte)
  359. System.out.print(byteToHex(i) + " ");
  360. System.out.println();
  361. //int imeiLength = base64TeaByte[28] & 0xFF;
  362. int imeiLength = Integer.parseInt(Base64TeaUitls.getStartToEndForInt(28, 28, base64TeaByte));
  363. System.out.println("imeiLength:"+imeiLength);
  364. System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForHex(28+1, 28+imeiLength, base64TeaByte));
  365. System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForInt(28+1, 28+imeiLength, base64TeaByte));
  366. System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForASIII(28+1, 28+imeiLength, base64TeaByte));
  367. }
  368. }