HeartBLEDevice.m 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. //
  2. // HeartBLEDevice.m
  3. // HeartTestDemo
  4. //
  5. // Created by 郭志奇 on 2019/8/7.
  6. // Copyright © 2019 郭志奇. All rights reserved.
  7. //
  8. #import "HeartBLEDevice.h"
  9. #import "HeartBLEDriver.h"
  10. #import <CoreBluetooth/CoreBluetooth.h>
  11. @interface HeartBLEDevice()<BLEDeviceDelegate,CBPeripheralDelegate>{
  12. HeartBLEDriver *HeartDriver;
  13. id __weak theDelegate;
  14. int ComeInDingyue;
  15. int WriteCount;
  16. NSString *DeviceNameStr;
  17. int ComeHRCount;
  18. int ComeRRCount;
  19. NSString *HisHRUTCStr;
  20. NSString *HisRRUTCStr;
  21. BOOL isStopGetParam;
  22. // int JionHRCount;
  23. int JionRRCount;
  24. int SleepCount;
  25. NSString *SleepUTCStr;
  26. int HRVCount;
  27. BOOL ISHeart;
  28. BOOL ISRRCount;
  29. NSString *modelNameStr;
  30. int FiveMinCount;
  31. int OnceMinCount;
  32. BOOL is1542Mode;
  33. BOOL isCL833Mode;
  34. }
  35. @property (nonatomic, strong) CBPeripheral *peripheral;
  36. @property (nonatomic, strong) CBCharacteristic *characteristic;
  37. @property (nonatomic,strong)NSMutableArray *HisHRArr;
  38. @property (nonatomic,strong)NSMutableArray *HisRRArr;
  39. @property (nonatomic,strong)NSMutableArray *GetUTCHRArr;
  40. @property (nonatomic,strong)NSMutableArray *GetUTCRRArr;
  41. @property (nonatomic,strong)NSMutableArray *Get7dayArr;
  42. @property (nonatomic,strong)NSMutableArray *Get7dayDataArr;
  43. @property (nonatomic,strong)NSMutableArray *GetTimStepArr;
  44. @property (nonatomic,strong)NSMutableArray *GetSingleTimArr;
  45. @property (nonatomic, copy)NSString *FitChenSunStr;
  46. @end
  47. @implementation HeartBLEDevice
  48. - (void)setDelegate:(id)delegate
  49. {
  50. theDelegate = delegate;
  51. }
  52. - (void)connect
  53. {
  54. [HeartDriver connectDevice:self.peripheral];
  55. }
  56. - (void)disconncet
  57. {
  58. [HeartDriver disConnectDevice:self.peripheral];
  59. }
  60. - (instancetype)initWithPeriphral:(id)thePeripheal
  61. {
  62. self = [super init];
  63. if (self) {
  64. _peripheral = thePeripheal;
  65. HeartDriver = [HeartBLEDriver sharedInstance];
  66. self.HisHRArr = [NSMutableArray arrayWithCapacity:0];
  67. self.HisRRArr = [NSMutableArray arrayWithCapacity:0];
  68. self.GetUTCHRArr = [NSMutableArray arrayWithCapacity:0];
  69. self.GetUTCRRArr = [NSMutableArray arrayWithCapacity:0];
  70. self.Get7dayArr = [NSMutableArray arrayWithCapacity:0];
  71. self.Get7dayDataArr = [NSMutableArray arrayWithCapacity:0];
  72. self.GetTimStepArr = [NSMutableArray arrayWithCapacity:0];
  73. self.GetSingleTimArr = [NSMutableArray arrayWithCapacity:0];
  74. //SendBuffCount = 0;
  75. HRVCount = 0;
  76. is1542Mode = NO;
  77. isCL833Mode = NO;
  78. }
  79. return self;
  80. }
  81. #pragma mark - CBPeripheralDelegate
  82. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
  83. {
  84. // 遍历出外设中所有的服务
  85. for (CBService *service in peripheral.services)
  86. {
  87. NSLog(@"所有服务的UUID:%@",service.UUID);
  88. if ([service.UUID isEqual:[CBUUID UUIDWithString:@"AAE28F00-71B5-42A1-8C3C-F9CF6AC969D0"]])//FD00
  89. {
  90. [peripheral discoverCharacteristics:nil forService:service];
  91. }
  92. if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]]) //心率服务
  93. {
  94. [peripheral discoverCharacteristics:nil forService:service];
  95. }
  96. if ([service.UUID isEqual:[CBUUID UUIDWithString:@"1814"]]) //步频服务
  97. {
  98. [peripheral discoverCharacteristics:nil forService:service];
  99. }
  100. if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180F"]]) //电池服务
  101. {
  102. [peripheral discoverCharacteristics:nil forService:service];
  103. }
  104. if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) //设备服务
  105. {
  106. [peripheral discoverCharacteristics:nil forService:service];
  107. }
  108. }
  109. }
  110. -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error
  111. {
  112. // 遍历出所需要的特征
  113. for (CBCharacteristic *characteristic in service.characteristics) {
  114. NSLog(@"所有特征:%@", characteristic);
  115. // 从外设开发人员那里拿到不同特征的UUID,不同特征做不同事情,比如有读取数据的特征,也有写入数据的特征
  116. NSLog(@"特征的UUID:%@",characteristic.UUID);
  117. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"AAE28F01-71B5-42A1-8C3C-F9CF6AC969D0"]])//从设备读数据
  118. {
  119. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  120. // [peripheral readValueForCharacteristic:characteristic];
  121. // 订阅通知
  122. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  123. }
  124. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"AAE28F02-71B5-42A1-8C3C-F9CF6AC969D0"]])//写数据到设备
  125. {
  126. self.characteristic = characteristic;
  127. }
  128. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]])//心率
  129. {
  130. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  131. //[peripheral readValueForCharacteristic:characteristic];
  132. // 订阅通知
  133. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  134. }
  135. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A24"]])//从Model Name
  136. {
  137. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  138. [peripheral readValueForCharacteristic:characteristic];
  139. // 订阅通知
  140. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  141. }
  142. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]])//电池
  143. {
  144. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  145. [peripheral readValueForCharacteristic:characteristic];
  146. // 订阅通知
  147. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  148. }
  149. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A28"]])//版本
  150. {
  151. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  152. [peripheral readValueForCharacteristic:characteristic];
  153. // 订阅通知
  154. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  155. }
  156. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"AAE21541-71B5-42A1-8C3C-F9CF6AC969D0"]])//读取PPG值
  157. {
  158. //直接读取这个特征数据,会调用didUpdateValueForCharacteristic
  159. // [peripheral readValueForCharacteristic:characteristic];
  160. // 订阅通知
  161. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  162. is1542Mode = YES;
  163. }
  164. }
  165. HeartBLEDevice *device = [HeartDriver getDeviceWithPeripheral:peripheral];
  166. [theDelegate isConnected:YES withDevice:device];
  167. }
  168. -(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
  169. {
  170. if (error)
  171. {
  172. NSLog(@"error:%@",error);
  173. }
  174. // if (characteristic.isNotifying)
  175. if (peripheral.state == CBPeripheralStateConnected)
  176. {
  177. ComeInDingyue++;
  178. if (ComeInDingyue == 1)
  179. {
  180. //838
  181. NSString *ModelNameStrs = [self HEXToASSCI:characteristic.value];
  182. if ([ModelNameStrs isEqualToString:@"CL880N"] || [peripheral.name hasPrefix:@"CL837"]) {
  183. //设置UTC
  184. NSString *DateStr = [self dateTransformToTimeSp];
  185. int NowActualTime = [DateStr intValue];//Gets the current actual timex
  186. NSString *Data16Str = [self ToHex:NowActualTime];
  187. NSString *UTCStr = @"ff0808";
  188. NSString *UTCStr1 = [UTCStr stringByAppendingString:Data16Str];
  189. NSLog(@"UTC时间同步%@", UTCStr1);
  190. [self BLEReadData:UTCStr1];
  191. }
  192. }
  193. }
  194. else
  195. {
  196. NSLog(@"Fail...");
  197. }
  198. }
  199. -(void)asynUTCTime {
  200. NSDate *date = [NSDate date];
  201. NSTimeInterval ti = [date timeIntervalSince1970];
  202. NSString *timeStr = [NSString stringWithFormat:@"%f",ti];
  203. NSLog(@"timeStr:%@",timeStr);
  204. int time = [timeStr intValue] + 28800;
  205. NSLog(@"time:%d",time);
  206. NSString *Data16Str = [self ToHex:time];
  207. NSString *UTCStr = @"ff0808";
  208. NSString *UTCStr1 = [UTCStr stringByAppendingString:Data16Str];
  209. NSLog(@"设置UTC时间:%@",UTCStr1);
  210. [self BLEReadData:UTCStr1];
  211. }
  212. - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
  213. {
  214. // SendBuffCount++;
  215. NSData *data = characteristic.value;
  216. NSLog(@"data:%@",data);
  217. HeartBLEDevice *device = [HeartDriver getDeviceWithPeripheral:peripheral];
  218. if ([theDelegate respondsToSelector:@selector(SDKgetInfo:withDevice:)])
  219. {
  220. [theDelegate SDKgetInfo:data withDevice:device];
  221. }
  222. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A28"]])//设备版本
  223. {
  224. NSString *IpStr = [self HEXToASSCI:data];
  225. if ([theDelegate respondsToSelector:@selector(SDKDeviceBanBen:)])
  226. {
  227. [theDelegate SDKDeviceBanBen:IpStr];
  228. }
  229. }
  230. //判断Model Name
  231. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A24"]])
  232. {
  233. NSString *ModelNameStrs = [self HEXToASSCI:data];
  234. modelNameStr = ModelNameStrs;
  235. if ([modelNameStr isEqualToString:@"CL837"] || [modelNameStr isEqualToString:@"CL880N"])
  236. {
  237. isCL833Mode = YES;
  238. }
  239. }
  240. uint8_t *buffer_ = (uint8_t *)[data bytes];
  241. if (data != nil)
  242. {
  243. //电池
  244. NSString *Hex16StrDianCi = [self convertDataToHexStr:data];
  245. if (Hex16StrDianCi.length == 2)
  246. {
  247. NSString *Hex16StrDianCiSS = [NSString stringWithFormat:@"%lu",strtoul(Hex16StrDianCi.UTF8String, 0, 16)];
  248. if ([theDelegate respondsToSelector:@selector(SDKDianciStr:)])
  249. {
  250. [theDelegate SDKDianciStr:Hex16StrDianCiSS];
  251. }
  252. }
  253. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]])//Heart Rate
  254. {
  255. if (buffer_[0] == 0x00) //心率
  256. {
  257. if (buffer_[1] != 0)
  258. {
  259. NSString *Hex16Str = [self convertDataToHexStr:data];
  260. NSString *strA = [Hex16Str substringWithRange:NSMakeRange(2,2)];
  261. NSString *Para1Str = [NSString stringWithFormat:@"%lu",strtoul(strA.UTF8String, 0, 16)];
  262. NSMutableArray *rris = [NSMutableArray array];
  263. UInt16 length = data.length - 2; //包头和
  264. int index = 2;
  265. if (length >= 2) {
  266. for (int i = 0; i < length; i++) {
  267. if (index > length) {
  268. break;
  269. }
  270. int rri = buffer_[index] + buffer_[index+1]*256;
  271. [rris addObject:@(rri)];
  272. index = index + 2;
  273. }
  274. }
  275. if ([theDelegate respondsToSelector:@selector(SDKFitHeartParamter:RRIs:)])
  276. {
  277. [theDelegate SDKFitHeartParamter:Para1Str RRIs:[rris copy]];
  278. }
  279. }
  280. else if (buffer_[0] == 0x55) //心率
  281. {
  282. NSLog(@"心率数据为0啊");
  283. }
  284. }
  285. else
  286. {
  287. NSString *Hex16Str = [self convertDataToHexStr:data];
  288. NSString *strA = [Hex16Str substringWithRange:NSMakeRange(2,2)];
  289. NSString *Para1Str = [NSString stringWithFormat:@"%lu",strtoul(strA.UTF8String, 0, 16)];
  290. NSMutableArray *rris = [NSMutableArray array];
  291. UInt16 length = data.length - 2; //包头和
  292. int index = 2;
  293. if (length >= 2) {
  294. for (int i = 0; i < length; i++) {
  295. if (index > length) {
  296. break;
  297. }
  298. int rri = buffer_[index] + buffer_[index+1]*256;
  299. [rris addObject:@(rri)];
  300. index = index + 2;
  301. }
  302. }
  303. if ([theDelegate respondsToSelector:@selector(SDKFitHeartParamter:RRIs:)])
  304. {
  305. [theDelegate SDKFitHeartParamter:Para1Str RRIs:[rris copy]];
  306. }
  307. }
  308. }
  309. else if (buffer_[0] == 0xff)
  310. {
  311. if (buffer_[2] == 0x03)
  312. {
  313. NSString *Hex16Str = [self convertDataToHexStr:data];
  314. if (Hex16Str.length >= 30)
  315. {
  316. //年龄
  317. NSString *OldStr = [Hex16Str substringWithRange:NSMakeRange(10,2)];
  318. NSString *OldStrs = [NSString stringWithFormat:@"%lu",strtoul(OldStr.UTF8String, 0, 16)];//十六进制转普通字符串
  319. //性别
  320. NSString *SexStr = [Hex16Str substringWithRange:NSMakeRange(12,2)];
  321. NSString *SexStrs = [NSString stringWithFormat:@"%lu",strtoul(SexStr.UTF8String, 0, 16)];//十六进制转普通字符串
  322. //体重
  323. NSString *WeiStr = [Hex16Str substringWithRange:NSMakeRange(14,2)];
  324. NSString *WeiStrs = [NSString stringWithFormat:@"%lu",strtoul(WeiStr.UTF8String, 0, 16)];//十六进制转普通字符串
  325. //身高
  326. NSString *HeiStr = [Hex16Str substringWithRange:NSMakeRange(16,2)];
  327. NSString *HeiStrs = [NSString stringWithFormat:@"%lu",strtoul(HeiStr.UTF8String, 0, 16)];//十六进制转普通字符串
  328. //手机号
  329. NSString *PhNumStr = [Hex16Str substringWithRange:NSMakeRange(18,10)];
  330. NSString *PhNumStrs = [NSString stringWithFormat:@"%lu",strtoul(PhNumStr.UTF8String, 0, 16)];//十六进制转普通字符串
  331. if ([theDelegate respondsToSelector:@selector(SDKUserInfo:andSex:andWeight:andHeight:andPhoneNum:)])
  332. {
  333. [theDelegate SDKUserInfo:OldStrs andSex:SexStrs andWeight:WeiStrs andHeight:HeiStrs andPhoneNum:PhNumStrs];
  334. }
  335. }
  336. else
  337. {
  338. NSLog(@"数据长度出错!");
  339. }
  340. }
  341. else if (buffer_[2] == 0x04)
  342. {
  343. NSLog(@"设置成功了");
  344. if ([theDelegate respondsToSelector:@selector(SDKGetUserInfoState:)])
  345. {
  346. [theDelegate SDKGetUserInfoState:@"1"];
  347. }
  348. }
  349. else if (buffer_[2] == 0x15)
  350. {
  351. //运动实时数据
  352. NSString *Hex16Str = [self convertDataToHexStr:data];
  353. if (Hex16Str.length >= 24)
  354. {
  355. //步数
  356. NSString *strA = [Hex16Str substringWithRange:NSMakeRange(6,6)];
  357. NSString *Para1Str = [NSString stringWithFormat:@"%lu",strtoul(strA.UTF8String, 0, 16)];//十六进制转普通字符串
  358. //距离
  359. NSString *strB = [Hex16Str substringWithRange:NSMakeRange(12, 6)];
  360. NSString *Para2Str = [NSString stringWithFormat:@"%lu",strtoul(strB.UTF8String, 0, 16)];
  361. float a = [Para2Str floatValue] / 100;
  362. //卡路里
  363. NSString *strC = [Hex16Str substringWithRange:NSMakeRange(18, 6)];
  364. NSString *Para3Str = [NSString stringWithFormat:@"%lu",strtoul(strC.UTF8String, 0, 16)];
  365. float b = [Para3Str floatValue]/10;
  366. if ([theDelegate respondsToSelector:@selector(SDKFitRunSParamter:andFitKM:andFitCalor:)]) {
  367. [theDelegate SDKFitRunSParamter:[Para1Str intValue] andFitKM:a andFitCalor:b];
  368. }
  369. }
  370. }
  371. else if (buffer_[2] == 0x16)
  372. {
  373. if (isCL833Mode == YES && is1542Mode == YES)
  374. {
  375. // 7天历史记录 步数 里程 卡路里
  376. NSString *Hex16Str = [self convertDataToHexStr:data];
  377. NSString *StrLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  378. NSString *StrLenS = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  379. NSString *DataStr = [Hex16Str substringWithRange:NSMakeRange(6, [StrLenS intValue] * 2 - 8)];
  380. NSMutableArray *DataArr = [[NSMutableArray alloc]init];
  381. for (int i = 0; i < DataStr.length; i+=20)
  382. {
  383. NSRange range1 = NSMakeRange(i, 20);
  384. NSString *str1 = [DataStr substringWithRange:range1];
  385. [DataArr addObject:str1];
  386. }
  387. NSMutableArray *MainArr = [[NSMutableArray alloc]init];
  388. for (NSString *DateStr in DataArr)
  389. {
  390. NSMutableArray *HisArrSS = [[NSMutableArray alloc]init];
  391. //UTC
  392. NSString *StrLen = [DateStr substringWithRange:NSMakeRange(0, 8)];
  393. NSString *UTCStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  394. NSString *UTCStr1 = [self transformTime1:[UTCStr intValue]];
  395. [HisArrSS addObject:UTCStr1];
  396. //步数
  397. NSString *StrLen1 = [DateStr substringWithRange:NSMakeRange(8, 6)];
  398. NSString *StepStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen1.UTF8String, 0, 16)];
  399. [HisArrSS addObject:StepStr];
  400. //c卡路里
  401. NSString *StrLen2 = [DateStr substringWithRange:NSMakeRange(14, 6)];
  402. NSString *CalorStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen2.UTF8String, 0, 16)];
  403. [HisArrSS addObject:CalorStr];
  404. [MainArr addObject:HisArrSS];
  405. }
  406. MainArr = (NSMutableArray *)[[MainArr reverseObjectEnumerator]allObjects];
  407. if ([theDelegate respondsToSelector:@selector(SDKGet7DaysHisParam:)])
  408. {
  409. [theDelegate SDKGet7DaysHisParam:MainArr];
  410. }
  411. }
  412. else
  413. {
  414. if (buffer_[3] == 0xff)
  415. {
  416. for (NSString *DateStr in self.Get7dayArr)
  417. {
  418. NSMutableArray *HisArrSS = [[NSMutableArray alloc]init];
  419. if (DateStr.length >= 20)
  420. {
  421. //UTC
  422. NSString *StrLen = [DateStr substringWithRange:NSMakeRange(0, 8)];
  423. NSString *UTCStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  424. NSString *UTCStr1 = [self transformTime1:[UTCStr intValue]];
  425. [HisArrSS addObject:UTCStr1];
  426. //步数
  427. NSString *StrLen1 = [DateStr substringWithRange:NSMakeRange(8, 6)];
  428. NSString *StepStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen1.UTF8String, 0, 16)];
  429. [HisArrSS addObject:StepStr];
  430. //c卡路里
  431. NSString *StrLen2 = [DateStr substringWithRange:NSMakeRange(14, 6)];
  432. NSString *CalorStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen2.UTF8String, 0, 16)];
  433. [HisArrSS addObject:CalorStr];
  434. [self.Get7dayDataArr addObject:HisArrSS];
  435. }
  436. }
  437. if ([theDelegate respondsToSelector:@selector(SDKGet7DaysHisParam:)])
  438. {
  439. [theDelegate SDKGet7DaysHisParam:self.Get7dayDataArr];
  440. }
  441. self.Get7dayArr = [[NSMutableArray alloc]init];
  442. self.Get7dayDataArr = [[NSMutableArray alloc]init];
  443. }
  444. else
  445. {
  446. // 7天历史记录 步数 里程 卡路里
  447. NSString *Hex16Str = [self convertDataToHexStr:data];
  448. NSString *StrLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  449. NSString *StrLenS = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  450. if (Hex16Str.length >= 6+[StrLenS intValue] * 2 - 8)
  451. {
  452. NSString *DataStr = [Hex16Str substringWithRange:NSMakeRange(6, [StrLenS intValue] * 2 - 8)];
  453. for (int i = 0; i < DataStr.length; i+=20)
  454. {
  455. NSRange range1 = NSMakeRange(i, 20);
  456. NSString *str1 = [DataStr substringWithRange:range1];
  457. [self.Get7dayArr addObject:str1];
  458. }
  459. }
  460. }
  461. }
  462. }
  463. else if (buffer_[2] == 0x21)
  464. {
  465. if (buffer_[3] == 0xff)
  466. {
  467. if ([theDelegate respondsToSelector:@selector(SDKGetHisHRUTCArr:)])
  468. {
  469. [theDelegate SDKGetHisHRUTCArr:self.GetUTCHRArr];
  470. }
  471. self.GetUTCHRArr = [[NSMutableArray alloc]init];
  472. // JionHRCount = 0;
  473. }
  474. else
  475. {
  476. //心率历史数据清单
  477. NSString *Hex16Str = [self convertDataToHexStr:data];
  478. NSString *DataLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  479. NSString *DataLenS = [NSString stringWithFormat:@"%lu",strtoul(DataLen.UTF8String, 0, 16)];//十六进制转普通字符;
  480. if (Hex16Str.length >= 6 + [DataLenS intValue]*2 - 8)
  481. {
  482. NSString *StrLen = [Hex16Str substringWithRange:NSMakeRange(6, [DataLenS intValue]*2 - 8)];
  483. // NSMutableArray *arrA = [[NSMutableArray alloc]init];
  484. for (int i = 0; i < StrLen.length; i+=8)
  485. {
  486. NSRange range1 = NSMakeRange(i, 8);
  487. NSString *str1 = [StrLen substringWithRange:range1];
  488. if (self.GetUTCHRArr.count == 0)
  489. {
  490. [self.GetUTCHRArr addObject:str1];
  491. }
  492. else
  493. {
  494. if (![self.GetUTCHRArr containsObject:str1])
  495. {
  496. [self.GetUTCHRArr addObject:str1];
  497. }
  498. }
  499. }
  500. }
  501. }
  502. }
  503. else if (buffer_[2] == 0x22)
  504. {
  505. ComeHRCount++;
  506. //心率历史数据
  507. NSString *Hex16Str = [self convertDataToHexStr:data];
  508. NSString *DataLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  509. NSString *DataLenS = [NSString stringWithFormat:@"%lu",strtoul(DataLen.UTF8String, 0, 16)];//数据包长度
  510. if (ComeHRCount == 1)
  511. {
  512. if (Hex16Str.length >= 14)
  513. {
  514. NSString *UTCStr = [Hex16Str substringWithRange:NSMakeRange(6, 8)];//UTC时间
  515. HisHRUTCStr = [NSString stringWithFormat:@"%lu",strtoul(UTCStr.UTF8String, 0, 16)];//UTC时间
  516. }
  517. }
  518. else
  519. {
  520. NSString *UTCStr = [Hex16Str substringWithRange:NSMakeRange(6, 8)];//UTC时间
  521. NSString *NumStr = [NSString stringWithFormat:@"%lu",strtoul(UTCStr.UTF8String, 0, 16)];//UTC时间
  522. if (ComeHRCount == [NumStr intValue])
  523. {
  524. }
  525. else
  526. {
  527. NSLog(@"出现了丢包的情况:%d",[NumStr intValue]);
  528. }
  529. }
  530. if (Hex16Str.length >= 14+[DataLenS intValue]*2 - 16)
  531. {
  532. NSString *HeartParamStr = [Hex16Str substringWithRange:NSMakeRange(14, [DataLenS intValue]*2 - 16)];
  533. @autoreleasepool {
  534. [self.HisHRArr addObject:HeartParamStr];
  535. }
  536. }
  537. }
  538. else if (buffer_[2] == 0x23)
  539. {
  540. NSLog(@"self.HisHRArr:%ld",self.HisHRArr.count);
  541. NSString *str = @"1";
  542. NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:str,@"OverGetParaStr", nil];
  543. NSNotification *notification1 =[NSNotification notificationWithName:@"OverGetParaStrNofication" object:nil userInfo:dict1];
  544. [[NSNotificationCenter defaultCenter] postNotification:notification1];
  545. //心率历史数据结束包
  546. NSMutableArray *HeartHisArray = [[NSMutableArray alloc]init];
  547. NSMutableArray *HeartUTCArray = [[NSMutableArray alloc]init];
  548. NSString *HeartParamStr = nil;
  549. @autoreleasepool {
  550. HeartParamStr = [self.HisHRArr componentsJoinedByString:@""];
  551. }
  552. for (int i = 0; i < HeartParamStr.length; i+=2)
  553. {
  554. @autoreleasepool {
  555. NSRange range1 = NSMakeRange(i, 2);
  556. NSString *str1 = [HeartParamStr substringWithRange:range1];
  557. [HeartHisArray addObject:str1];
  558. }
  559. }
  560. for (int j = 0; j < HeartHisArray.count; j++)
  561. {
  562. @autoreleasepool {
  563. int aw = [HisHRUTCStr intValue] + j;
  564. [HeartUTCArray addObject:[NSString stringWithFormat:@"%d",aw]];
  565. }
  566. }
  567. NSMutableArray *HeartHisArrayS = [[NSMutableArray alloc]init];
  568. NSMutableArray *HeartUTCArrayS = [[NSMutableArray alloc]init];
  569. for (int i = 0; i < HeartHisArray.count; i++)
  570. {
  571. @autoreleasepool {
  572. NSString *HexStr = HeartHisArray[i];
  573. NSString *Para1Str = [NSString stringWithFormat:@"%lu",strtoul(HexStr.UTF8String, 0, 16)];
  574. [HeartHisArrayS addObject:Para1Str];
  575. }
  576. }
  577. for (NSString *TimeStr in HeartUTCArray)
  578. {
  579. @autoreleasepool {
  580. NSString *TextUTC = [self transformTime:[TimeStr intValue]];
  581. [HeartUTCArrayS addObject:TextUTC];
  582. }
  583. }
  584. if ([theDelegate respondsToSelector:@selector(SDKGetHisHRParaArr:andHisHRArr:)])
  585. {
  586. [theDelegate SDKGetHisHRParaArr:HeartUTCArrayS andHisHRArr:HeartHisArrayS];
  587. }
  588. ComeHRCount = 0;
  589. self.HisHRArr = [[NSMutableArray alloc]init];
  590. } else if (buffer_[2] == 0x37) {
  591. if (buffer_[3] == 0x00) {
  592. NSLog(@"退出血氧模式");
  593. } else {
  594. NSLog(@"进入血氧模式");
  595. int oxygen = buffer_[4];
  596. BOOL posture = buffer_[5] == 1 ? true : false;
  597. int pi = buffer_[6];
  598. int onwrist = buffer_[7] == 1 ? true : false;
  599. if ([theDelegate respondsToSelector:@selector(SDKGetBloodOxygen:wristPosture:redPI:onWrist:)])
  600. {
  601. [theDelegate SDKGetBloodOxygen:oxygen wristPosture:posture redPI:pi onWrist:onwrist];
  602. }
  603. }
  604. } else if (buffer_[2] == 0x38) {
  605. //(单位:10℃)温度数据数值扩大了10倍
  606. int environmentTemp = [self bytes2ToInt:buffer_[3] byte2:buffer_[4]];
  607. int wristTemp = [self bytes2ToInt:buffer_[5] byte2:buffer_[6]];
  608. int bodyTemp = [self bytes2ToInt:buffer_[7] byte2:buffer_[8]];
  609. if ([theDelegate respondsToSelector:@selector(SDKGetRealTimeTemp:wrist:body:)])
  610. {
  611. [theDelegate SDKGetRealTimeTemp:environmentTemp/10.0 wrist:wristTemp/10.0 body:bodyTemp/10.0];
  612. }
  613. }
  614. // else if (buffer_[2] == 0x3f)
  615. // {
  616. // NSString *Hex16Str = [self convertDataToHexStr:data];
  617. // NSString *DataLen = [Hex16Str substringWithRange:NSMakeRange(6, 2)];
  618. // NSString *DataLenS = [NSString stringWithFormat:@"%lu",strtoul(DataLen.UTF8String, 0, 16)];//数据包长度
  619. //
  620. // if ([theDelegate respondsToSelector:@selector(SDKGetBLEStauts:)])
  621. // {
  622. // [theDelegate SDKGetBLEStauts:DataLenS];
  623. // }
  624. // }
  625. else if (buffer_[2] == 0x40)
  626. {
  627. FiveMinCount++;
  628. NSLog(@"-----进来的次数-----:%d",FiveMinCount);
  629. if ([theDelegate respondsToSelector:@selector(SDKBagCountStr:)])
  630. {
  631. [theDelegate SDKBagCountStr:[NSString stringWithFormat:@"%d",FiveMinCount]];
  632. }
  633. NSString *Hex16Str = [self convertDataToHexStr:data];
  634. NSString *StrLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  635. NSString *StrLenS = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  636. NSString *DataStr = [Hex16Str substringWithRange:NSMakeRange(6, [StrLenS intValue] * 2 - 8)];
  637. for (int i = 0; i < DataStr.length; i+=16)
  638. {
  639. NSRange range1 = NSMakeRange(i, 16);
  640. NSString *str1 = [DataStr substringWithRange:range1];
  641. [self.GetTimStepArr addObject:str1];
  642. }
  643. }
  644. else if (buffer_[2] == 0x41)
  645. {
  646. NSMutableArray *HISUTCStepArr = [[NSMutableArray alloc]init];
  647. for (NSString *DataStr in self.GetTimStepArr)
  648. {
  649. NSMutableArray *HisArrSS = [[NSMutableArray alloc]init];
  650. //UTC
  651. NSString *StrLen = [DataStr substringWithRange:NSMakeRange(0, 8)];
  652. NSString *UTCStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  653. NSString *UTCStr1 = [self transformTime1:[UTCStr intValue]];
  654. [HisArrSS addObject:UTCStr1];
  655. //Steps
  656. NSString *StrLen1 = [DataStr substringWithRange:NSMakeRange(8, 8)];
  657. NSString *StepStr = [NSString stringWithFormat:@"%lu",strtoul(StrLen1.UTF8String, 0, 16)];
  658. [HisArrSS addObject:StepStr];
  659. [HISUTCStepArr addObject:HisArrSS];
  660. }
  661. if ([theDelegate respondsToSelector:@selector(SDKGetIntertTimeArr:)])
  662. {
  663. [theDelegate SDKGetIntertTimeArr:HISUTCStepArr];
  664. }
  665. self.GetTimStepArr = [[NSMutableArray alloc]init];
  666. }
  667. else if (buffer_[2] == 0x42)
  668. {
  669. // OnceMinCount++;
  670. // NSLog(@"-----进来的次数1-----:%d",OnceMinCount);
  671. NSString *Hex16Str = [self convertDataToHexStr:data];
  672. NSString *StrLen = [Hex16Str substringWithRange:NSMakeRange(2, 2)];
  673. NSString *StrLenS = [NSString stringWithFormat:@"%lu",strtoul(StrLen.UTF8String, 0, 16)];
  674. NSString *DataStr = [Hex16Str substringWithRange:NSMakeRange(6, [StrLenS intValue] * 2 - 8)];
  675. for (int i = 0; i < DataStr.length; i+=8)
  676. {
  677. NSRange range1 = NSMakeRange(i, 8);
  678. NSString *str1 = [DataStr substringWithRange:range1];
  679. [self.GetSingleTimArr addObject:str1];
  680. if ([theDelegate respondsToSelector:@selector(SDKBag1CountStr:)])
  681. {
  682. [theDelegate SDKBag1CountStr:[NSString stringWithFormat:@"%ld",self.GetSingleTimArr.count]];
  683. }
  684. }
  685. }
  686. else if (buffer_[2] == 0x43)
  687. {
  688. NSMutableArray *HISUTCSingleArr = [[NSMutableArray alloc]init];
  689. for (NSString *DataStr in self.GetSingleTimArr)
  690. {
  691. NSString *UTCStr = [NSString stringWithFormat:@"%lu",strtoul(DataStr.UTF8String, 0, 16)];
  692. NSString *UTCStr1 = [self transformTime1:[UTCStr intValue]];
  693. [HISUTCSingleArr addObject:UTCStr1];
  694. }
  695. if ([theDelegate respondsToSelector:@selector(SDKGetSingleUTCArr:)])
  696. {
  697. [theDelegate SDKGetSingleUTCArr:HISUTCSingleArr];
  698. }
  699. self.GetSingleTimArr = [[NSMutableArray alloc]init];
  700. }
  701. }
  702. }
  703. }
  704. - (void)enterOxygenMode {
  705. NSString *StrLen = @"ff053701";
  706. [self BLEReadData:StrLen];
  707. }
  708. - (void)quitOxygenMode {
  709. NSString *StrLen = @"ff053700";
  710. [self BLEReadData:StrLen];
  711. }
  712. - (void)requestRealTimeTemperature {
  713. NSString *StrLen = @"ff0438";
  714. [self BLEReadData:StrLen];
  715. }
  716. #pragma mark - APP写入参数命令
  717. - (void)BLEReadData:(NSString *)dataStr
  718. {
  719. [self CheckSun:dataStr];
  720. [self CheckSun28:dataStr andCheckStr:self.FitChenSunStr];
  721. }
  722. - (void)CheckSun28:(NSString *)DateLenStr andCheckStr:(NSString *)CheckStr
  723. {
  724. unsigned long num1 = strtoul([CheckStr UTF8String], 0, 16);
  725. uint8_t sss = 0x00 - num1;
  726. uint8_t SSS = sss ^ 0x3a;//异或0x3a
  727. NSString *DataStrA = [DateLenStr stringByAppendingString:[NSString stringWithFormat:@"%02x",SSS]];
  728. const char *ChataStr = [DataStrA UTF8String];
  729. //延迟发送
  730. usleep(170 * 1000);
  731. [self BLESendYinDaBuff:ChataStr];
  732. }
  733. - (void)BLESendYinDaBuff:(const char *)Buff
  734. {
  735. NSMutableData *data = [NSMutableData data];
  736. if (Buff)
  737. {
  738. uint32_t len = (uint32_t)strlen(Buff);
  739. char singleNumberString[3] = {'\0', '\0', '\0'};
  740. uint32_t singleNumber = 0;
  741. for(uint32_t i = 0 ; i < len; i+=2)
  742. {
  743. if ( ((i+1) < len) && isxdigit(Buff[i]) && (isxdigit(Buff[i+1])) )
  744. {
  745. singleNumberString[0] = Buff[i];
  746. singleNumberString[1] = Buff[i + 1];
  747. sscanf(singleNumberString, "%x", &singleNumber);
  748. uint8_t tmp = (uint8_t)(singleNumber & 0x000000FF);
  749. [data appendBytes:(void *)(&tmp) length:1];
  750. }
  751. else
  752. {
  753. break;
  754. }
  755. }
  756. NSLog(@"App Send Data:%@",data);
  757. FiveMinCount = 0;
  758. OnceMinCount = 0;
  759. if (self.peripheral != nil)
  760. {
  761. [self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse];
  762. }
  763. }
  764. }
  765. - (void)CheckSun:(NSString *)CountString
  766. {
  767. NSMutableArray *originalArr = [self getArrWithString:CountString];
  768. uint8_t checksum = 0x00;
  769. for (NSInteger i = 0; i < originalArr.count; i++) {
  770. NSString *str = [NSString stringWithFormat:@"0x%@",originalArr[i]];
  771. const char *temChar = [str UTF8String];
  772. int temW;
  773. sscanf(temChar, "0x%2x",&temW);
  774. checksum += temW;
  775. }
  776. // NSLog(@"%x", checksum);
  777. self.FitChenSunStr = [NSString stringWithFormat:@"%02x",checksum];
  778. }
  779. - (NSMutableArray *)getArrWithString:(NSString *)keysource {
  780. NSMutableArray *keysourceArr = [NSMutableArray array];
  781. for (NSInteger i = 0; i < keysource.length/2; i++) {
  782. NSString *str = [keysource substringWithRange:NSMakeRange(2*i, 2)];
  783. str = [NSString stringWithFormat:@"%@",str];
  784. [keysourceArr addObject:str];
  785. }
  786. return keysourceArr;
  787. }
  788. - (void)tongxinMa28
  789. {
  790. int i = (int)(random()%255)+1;
  791. NSString *str1 = [self ToHex:i];
  792. unsigned long num1 = strtoul([str1 UTF8String], 0, 16);
  793. uint8_t cc = num1;
  794. //Byte cc = 0x37;
  795. uint8_t a = cc^0xd5;
  796. NSString *yihouStr = [self ToHex:a];
  797. uint8_t b1 = 0x43^cc;
  798. uint8_t b2 = 0x68^cc;
  799. uint8_t b3 = 0x69^cc;
  800. uint8_t b4 = 0x6c^cc;
  801. uint8_t b5 = 0x65^cc;
  802. uint8_t b6 = 0x61^cc;
  803. uint8_t b7 = 0x66^cc;
  804. uint8_t b8 = 0x5f^cc;
  805. uint8_t b9 = 0x32^cc;
  806. uint8_t b10 = 0x30^cc;
  807. uint8_t b11 = 0x31^cc;
  808. uint8_t b12 = 0x39^cc;
  809. uint8_t b13 = 0x31^cc;
  810. uint8_t b14 = 0x31^cc;
  811. NSString *ChileafStr = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14];
  812. NSString *strLen = @"ff2813";
  813. NSString *DatalenStr = [strLen stringByAppendingString:ChileafStr];
  814. NSString *DatalenStr1 = [DatalenStr stringByAppendingString:yihouStr];
  815. [self BLEReadData:DatalenStr1];
  816. }
  817. -(NSString *)ToHex:(long long int)tmpid
  818. {
  819. NSString *nLetterValue;
  820. NSString *str =@"";
  821. long long int ttmpig;
  822. for (int i = 0; i<9; i++) {
  823. ttmpig=tmpid%16;
  824. tmpid=tmpid/16;
  825. switch (ttmpig)
  826. {
  827. case 10:
  828. nLetterValue =@"A";break;
  829. case 11:
  830. nLetterValue =@"B";break;
  831. case 12:
  832. nLetterValue =@"C";break;
  833. case 13:
  834. nLetterValue =@"D";break;
  835. case 14:
  836. nLetterValue =@"E";break;
  837. case 15:
  838. nLetterValue =@"F";break;
  839. default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
  840. }
  841. str = [nLetterValue stringByAppendingString:str];
  842. if (tmpid == 0) {
  843. break;
  844. }
  845. }
  846. return str;
  847. }
  848. - (NSString *)dateTransformToTimeSp
  849. {
  850. NSDate *date = [NSDate date];
  851. // NSTimeInterval timeIn = [date timeIntervalSince1970];
  852. NSTimeZone *zone = [NSTimeZone systemTimeZone];
  853. NSInteger interval = [zone secondsFromGMTForDate:date];
  854. NSDate *localDate = [date dateByAddingTimeInterval:interval];
  855. NSTimeInterval timeIn2 = [localDate timeIntervalSince1970];
  856. NSString *timeSp = [NSString stringWithFormat:@"%.0f",timeIn2];
  857. return timeSp;
  858. }
  859. - (NSString *)convertDataToHexStr:(NSData *)data
  860. {
  861. if (!data || [data length] == 0) {
  862. return @"";
  863. }
  864. NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
  865. [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
  866. unsigned char *dataBytes = (unsigned char*)bytes;
  867. for (NSInteger i = 0; i < byteRange.length; i++) {
  868. NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
  869. if ([hexStr length] == 2) {
  870. [string appendString:hexStr];
  871. } else {
  872. [string appendFormat:@"0%@", hexStr];
  873. }
  874. }
  875. }];
  876. return string;
  877. }
  878. - (NSString *)transformTime1:(long long)timestamp
  879. {
  880. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  881. NSString *template = @"yyyy-MM-dd HH:mm:ss";
  882. NSString *formatStr = [NSDateFormatter dateFormatFromTemplate:template options:0 locale:[NSLocale currentLocale]];
  883. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  884. [formatter setDateFormat:formatStr];
  885. [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
  886. NSString *time = [formatter stringFromDate:date];
  887. return time;
  888. }
  889. #pragma mark - UTC转本地时间
  890. - (NSString *)transformTime:(long long)timestamp
  891. {
  892. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  893. NSString *template = @"HH:mm:ss";
  894. NSString *formatStr = [NSDateFormatter dateFormatFromTemplate:template options:0 locale:[NSLocale currentLocale]];
  895. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  896. [formatter setDateFormat:formatStr];
  897. [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
  898. NSString *time = [formatter stringFromDate:date];
  899. return time;
  900. }
  901. #pragma mark - 16进制转ASSCI
  902. - (NSString *)HEXToASSCI:(NSData *)data
  903. {
  904. NSString *Hex16Str = [self convertDataToHexStr:data];
  905. //NSLog(@"Hex16Str:%@",Hex16Str);
  906. NSMutableArray *WifArr = [[NSMutableArray alloc]init];
  907. for (int i = 0; i < Hex16Str.length; i+=2)
  908. {
  909. NSRange range1 = NSMakeRange(i, 2);
  910. NSString *str1 = [Hex16Str substringWithRange:range1];
  911. [WifArr addObject:str1];
  912. }
  913. // NSLog(@"WifArr:%@",WifArr);
  914. NSMutableArray *arra = [[NSMutableArray alloc]init];
  915. for (int i = 0; i < WifArr.count; i++)
  916. {
  917. NSString *strAA = WifArr[i];
  918. NSString *Para1Str = [NSString stringWithFormat:@"%lu",strtoul(strAA.UTF8String, 0, 16)];
  919. char st = [Para1Str intValue];
  920. NSString *IpStr = [NSString stringWithFormat:@"%c",st];
  921. [arra addObject:IpStr];
  922. }
  923. // NSLog(@"arra:%@",arra);
  924. if (arra.count != 0)
  925. {
  926. NSString *string = [arra componentsJoinedByString:@"/"];
  927. NSString *str = [string stringByReplacingOccurrencesOfString:@"/" withString:@""];
  928. return str;
  929. }
  930. return 0;
  931. }
  932. - (int)bytes4ToInt:(uint8_t *)bytes {
  933. int addr = bytes[0] & 0xFF;
  934. addr |= ((bytes[1] << 8) & 0xFF00);
  935. addr |= ((bytes[2] << 16) & 0xFF0000);
  936. addr |= ((bytes[3] << 24) & 0xFF000000);
  937. return addr;
  938. }
  939. - (int)bytes2ToInt:(uint8_t *)bytes {
  940. int addr = bytes[0] & 0xFF;
  941. addr |= ((bytes[1] << 8) & 0xFF00);
  942. return addr;
  943. }
  944. - (int)bytes2ToInt:(uint8_t)byte1 byte2:(uint8_t)byte2 {
  945. int addr = byte1 * 256 + byte2;
  946. return addr;
  947. }
  948. @end