BLEServiceHandler.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. //
  2. // BLEServiceHandler.m
  3. // OneCable
  4. //
  5. // Created by KaRam Kim on 2017. 5. 18..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "BLEServiceHandler.h"
  9. #import <CoreBluetooth/Corebluetooth.h>
  10. #import "Definitions.h"
  11. #import "JDJSONModel.h"
  12. #import "JDObject.h"
  13. @interface BLEServiceHandler()<CBCentralManagerDelegate, CBPeripheralDelegate>
  14. {
  15. CBCentralManager *_manager;
  16. NSMutableArray *_devices;
  17. BOOL _scanning;
  18. NSTimer *_scanTimer;
  19. //service characteristic
  20. CBService *lastService;
  21. }
  22. @end
  23. @implementation BLEServiceHandler
  24. + (id)sharedManager
  25. {
  26. static BLEServiceHandler *sharedBLEServiceHandler = nil;
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. sharedBLEServiceHandler = [[self alloc] init];
  30. });
  31. return sharedBLEServiceHandler;
  32. }
  33. - (id) init
  34. {
  35. self = [super init];
  36. if (self) {
  37. _manager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
  38. // *peripheralManager = [[CBPeripheralManager alloc]initWithDelegate:nil queue:nil options:@{CBPeripheralManagerOptionShowPowerAlertKey:@NO}]
  39. _devices = [NSMutableArray array];
  40. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startScan) name:kBLEConnect object:nil];
  41. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startScan) name:kBLEDisConnect object:nil];
  42. }
  43. return self;
  44. }
  45. - (void)connect:(BTLEDeivceModel *)info
  46. {
  47. [_manager connectPeripheral:info.peripheralRef options:nil];
  48. // if (info.peripheralRef.state != CBPeripheralStateDisconnected) {
  49. // [_manager cancelPeripheralConnection:info.peripheralRef];
  50. // } else {
  51. // [_manager connectPeripheral:info.peripheralRef options:nil];
  52. // }
  53. }
  54. - (void)disConnect
  55. {
  56. _conDevice = nil;
  57. }
  58. -(void) openBluetoothSettings{
  59. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Bluetooth"]];
  60. }
  61. - (BOOL) checkBLEStatus {
  62. if (_manager.state == CBManagerStatePoweredOff) {
  63. NSLog(@"_manager.state != CBCentralManagerStatePoweredOn") ;
  64. [[JDFacade facade] alert:@"블루투스를 켜고 다시 시도하세요."];
  65. // [[JDFacade facade] confirmTitle:@"Notice"
  66. // message:@"블루투스가 비활성화 되어 있습니다. 활성화 해주세요."
  67. // btnOKLabel:@"OK"
  68. // btnCancelLabel:@"Cancel"
  69. // completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  70. //
  71. // if (buttonIndex == 1) {
  72. //
  73. // [self openBluetoothSettings];
  74. // }
  75. // }];
  76. return NO;
  77. }
  78. return YES;
  79. }
  80. - (void) startScan
  81. {
  82. if (![self checkBLEStatus])
  83. return;
  84. _devices = [NSMutableArray array];
  85. if (_scanTimer) [_scanTimer invalidate];
  86. _scanning = YES;
  87. [_manager stopScan];
  88. NSLog(@"START SCAN");
  89. [_manager scanForPeripheralsWithServices:nil options:nil];
  90. _scanTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stopScan:) userInfo:@"Y" repeats:NO];
  91. }
  92. - (void)stopScan:(NSString*)timeout
  93. {
  94. NSLog(@"STOP SCAN");
  95. [_manager stopScan] ;
  96. _scanning = NO ;
  97. if( _delegate && [_delegate respondsToSelector:@selector(BLEEndScan:)] ) {
  98. [_delegate BLEEndScan:_devices] ;
  99. }
  100. }
  101. -(NSMutableArray *)getDeviceList
  102. {
  103. return _devices;
  104. }
  105. -(CBCharacteristic *)getChrInfo:(NSString *)name
  106. {
  107. CBCharacteristic *result = nil;
  108. for (CBService *serviceInfo in [_conDevice getServiceList]) {
  109. for (CBCharacteristic *chrInfo in serviceInfo.characteristics) {
  110. if ([[self getStrUUID:chrInfo] isEquestToIgnoreCase:[self getServiceUUID:name]]) {
  111. //NSLog(@"\nname : %@, chrInfo : %@", name, chrInfo.UUID);
  112. result = chrInfo;
  113. break;
  114. }
  115. }
  116. }
  117. return result;
  118. }
  119. -(NSString *)getChrName:(CBCharacteristic *)info
  120. {
  121. NSString *result = nil;
  122. for (NSString *key in [[self getGatewayCharDict] allKeysForObject:[[self getStrUUID:info] lowercaseString]]) {
  123. result = key;
  124. }
  125. return result;
  126. }
  127. -(NSString *)getStrUUID:(CBCharacteristic *)info
  128. {
  129. NSString *uuid =[NSString stringWithFormat:@"%@", info.UUID];
  130. return uuid;
  131. }
  132. - (BTLEDeivceModel *)addPeripheral:(CBPeripheral*)peripheral {
  133. BTLEDeivceModel *device = [[BTLEDeivceModel alloc] init];
  134. device.peripheralRef = peripheral;
  135. device.manager = _manager;
  136. peripheral.delegate = self;
  137. if (![_devices containsObject:device]) {
  138. [_devices addObject:device];
  139. }
  140. // if( _delegate && [_delegate respondsToSelector:@selector(BLEUpldateDevice:)] ) {
  141. // [_delegate BLEUpldateDevice:device] ;
  142. // }
  143. return device;
  144. }
  145. - (NSString*)characteristicName:(CBCharacteristic *)charac {
  146. NSString *title = [self getGatewayDicUUIDForKey:[NSString stringWithFormat:@"%@", charac.UUID]];
  147. NSRange index = [title rangeOfString:@"(<"];
  148. if (index.location != NSNotFound) {
  149. if ([title length] > 20) {
  150. // 128 bit uuid
  151. return [NSString stringWithFormat:@"0x%@", [title substringWithRange:NSMakeRange(index.location+2, 35)]];
  152. } else {
  153. // 16 bit uuid
  154. NSString *key = [NSString stringWithFormat:@"0x%@", [[title substringWithRange:NSMakeRange(index.location+2, 4)] uppercaseString]];
  155. NSString *value = NSLocalizedStringFromTable(key, @"characteristics", @"");
  156. if ([key isEqualToString:value]) value = @"Unknown UUID";
  157. return [NSString stringWithFormat:@"%@: %@", key, value];
  158. }
  159. }
  160. return [title stringByReplacingOccurrencesOfString:@"Unknown" withString:@"Unknown UUID:"];
  161. }
  162. - (NSString*)hexStringValue:(CBCharacteristic *)input {
  163. if (!input.value) return @"-";
  164. NSString *raw = [NSString stringWithFormat:@"0x%@", input.value];
  165. raw = [raw stringByReplacingOccurrencesOfString:@"<" withString:@""];
  166. raw = [raw stringByReplacingOccurrencesOfString:@">" withString:@""];
  167. return raw;
  168. }
  169. - (NSString*)asciiStringValue:(CBCharacteristic *)input {
  170. if (!input.value) return @"-";
  171. NSString *ascii = [[NSString alloc] initWithData:input.value encoding:NSASCIIStringEncoding];
  172. return ascii;
  173. }
  174. -(void)sendData:(CBCharacteristic *)chr str:(NSString *)str
  175. {
  176. NSData *data = [NSData data];
  177. if ([str isDigit]) {
  178. int dataToWrite = [str intValue];
  179. data = [NSData dataWithBytes:&dataToWrite length:1];
  180. } else {
  181. data = [str dataUsingEncoding:NSASCIIStringEncoding];
  182. }
  183. if (chr.properties & CBCharacteristicPropertyWriteWithoutResponse)
  184. [chr.service.peripheral writeValue:data forCharacteristic:chr type:CBCharacteristicWriteWithoutResponse];
  185. else if (chr.properties & CBCharacteristicPropertyWrite)
  186. [chr.service.peripheral writeValue:data forCharacteristic:chr type:CBCharacteristicWriteWithResponse];
  187. }
  188. #pragma mark - WiFi Settings
  189. //WiFi Setting관련 메뉴
  190. -(void)scanWiFiList
  191. {
  192. [self sendData:[self getChrInfo:kBLEChrStWiFiScan] str:@"1"];
  193. }
  194. -(NSString *)getWLanList:(BLEWlanListType)type
  195. {
  196. // TODO : WLanList 가져오기
  197. NSString *result = @"";
  198. switch (type) {
  199. case BLEWlanListType1:
  200. [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList1 isNotify:NO];
  201. //result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList1]];
  202. break;
  203. case BLEWlanListType2:
  204. [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList2 isNotify:NO];
  205. //result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList2]];
  206. break;
  207. case BLEWlanListType3:
  208. [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList3 isNotify:NO];
  209. //result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList3]];
  210. break;
  211. default:
  212. break;
  213. }
  214. return result;
  215. }
  216. -(NSString *)getValueFromCharacteristic:(NSString*)type
  217. {
  218. return [self asciiStringValue:[self getChrInfo:type]];;
  219. }
  220. -(void)setWiFiSSID:(NSString *)ssid
  221. {
  222. [self sendData:[self getChrInfo:kBLEChrStSSIDArg] str:ssid];
  223. }
  224. -(void)setWiFiPwd:(NSString *)pwd
  225. {
  226. [self sendData:[self getChrInfo:kBLEChrStPWDArg] str:pwd];
  227. }
  228. -(void)enableDHCP
  229. {
  230. [self sendData:[self getChrInfo:kBLEChrStDHCPArg] str:@"1"];
  231. }
  232. -(void)applyWiFiSettingInfo
  233. {
  234. [self sendData:[self getChrInfo:kBLEChrStSetApply] str:@"1"];
  235. // todo : Connection정보가 notify로 들어오면, delegate를 통해서 알리기
  236. }
  237. -(void)readAndNotifyCharacteristicUUID:(NSString *)uuid
  238. isNotify:(BOOL)isNotify
  239. {
  240. for (CBService *service in _conDevice.peripheralRef.services) {
  241. for (CBCharacteristic *characteristic in service.characteristics) {
  242. if ([characteristic.UUID.UUIDString isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:uuid]]) {
  243. if (isNotify)
  244. [_conDevice.peripheralRef setNotifyValue:YES forCharacteristic:characteristic];
  245. else
  246. [_conDevice.peripheralRef readValueForCharacteristic:characteristic];
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. - (void)readConnectionWiFiInfo {
  253. [self readAndNotifyCharacteristicUUID:kBLEChrRdSSID isNotify:NO];
  254. [self readAndNotifyCharacteristicUUID:kBLEChrRdBSSID isNotify:NO];
  255. [self readAndNotifyCharacteristicUUID:kBLEChrRnIpSet isNotify:NO];
  256. [self readAndNotifyCharacteristicUUID:kBLEChrRnIpAddr isNotify:NO];
  257. }
  258. - (NSString*)getStringValueForCharacteristicWithKey:(NSString*)key {
  259. return [self asciiStringValue:[self getChrInfo:key]];
  260. }
  261. -(NSString *)getServiceName:(NSString *)uuid
  262. {
  263. NSString *result = nil;
  264. for (NSString *key in [[self getGatewayCharDict] allKeysForObject:uuid.lowercaseString]) {
  265. result = key;
  266. }
  267. return result;
  268. }
  269. -(NSString *)getServiceUUID:(NSString *)strKey
  270. {
  271. NSString *result = nil;
  272. for (NSString *key in [[self getGatewayCharDict] allKeys]) {
  273. if ([key isEquestToIgnoreCase:strKey]) {
  274. result = [[self getGatewayCharDict] objectForKey:key];
  275. break;
  276. }
  277. }
  278. return result;
  279. }
  280. - (NSString*)getGatewayDicUUIDForKey:(NSString*)key{
  281. return [[self getGatewayCharDict] objectForKey:key];
  282. }
  283. -(NSDictionary *)getGatewayCharDict
  284. {
  285. NSDictionary *db = @{@"WFNPS_UUID":@"6f819d94-dddf-11e6-bf26-cec0c932ce01",
  286. @"SCAN_CHR_UUID":@"b364676d-dd76-11e6-bf26-cec0c932ce01",
  287. @"WLAN_LIST1_CHR_UUID":@"f333f87c-0787-11e7-93ae-92361f002671",
  288. @"WLAN_LIST2_CHR_UUID":@"f333fad4-0787-11e7-93ae-92361f002671",
  289. @"WLAN_LIST3_CHR_UUID":@"f333fbec-0787-11e7-93ae-92361f002671",
  290. @"SSID_ARG_CHR_UUID":@"508e6c28-0788-11e7-93ae-92361f002671",
  291. @"PASSWORD_ARG_CHR_UUID":@"b3646c08-dd76-11e6-bf26-cec0c932ce01",
  292. @"CONNECTION_CHR_UUID":@"b3646fbe-dd76-11e6-bf26-cec0c932ce01",
  293. @"SSID_CHR_UUID":@"b3647108-dd76-11e6-bf26-cec0c932ce01",
  294. @"BSSID_CHR_UUID":@"b3647234-dd76-11e6-bf26-cec0c932ce01",
  295. @"WF_IP_SET_ARG_CHR_UUID":@"8e0b49e6-088b-11e7-93ae-92361f002671",
  296. @"WF_IP_ADDR_ARG_CHR_UUID":@"8e0b4c2a-088b-11e7-93ae-92361f002671",
  297. @"WF_NET_PRE_LEN_ARG_CHR_UUID":@"8e0b4e32-088b-11e7-93ae-92361f002671",
  298. @"WF_GATEWAY_ARG_CHR_UUID":@"8e0b4ffe-088b-11e7-93ae-92361f002671",
  299. @"WF_DNS_ARG_CHR_UUID":@"8e0b50d0-088b-11e7-93ae-92361f002671",
  300. @"APPLY_CHR_UUID":@"8e0b51a2-088b-11e7-93ae-92361f002671",
  301. @"WF_IP_SET_CHR_UUID":@"8e0b5314-088b-11e7-93ae-92361f002671",
  302. @"WF_IP_ADDR_CHR_UUID":@"8e0b54a4-088b-11e7-93ae-92361f002671",
  303. @"WF_NET_PRE_LEN_CHR_UUID":@"8e0b5576-088b-11e7-93ae-92361f002671",
  304. @"WF_GATEWAY_CHR_UUID":@"8e0b568e-088b-11e7-93ae-92361f002671",
  305. @"WF_DNS_CHR_UUID":@"8e0b5878-088b-11e7-93ae-92361f002671",
  306. @"IPNPS_UUID":@"09d38ae8-dbb9-11e6-bf26-cec0c932ce01",
  307. @"IP_SET_ARG_CHR_UUID":@"09d38d68-dbb9-11e6-bf26-cec0c932ce01",
  308. @"IP_ADDR_ARG_CHR_UUID":@"09d38e62-dbb9-11e6-bf26-cec0c932ce01",
  309. @"NET_PRE_LEN_ARG_CHR_UUID":@"09d38f48-dbb9-11e6-bf26-cec0c932ce01",
  310. @"GATEWAY_ARG_CHR_UUID":@"09d39024-dbb9-11e6-bf26-cec0c932ce01",
  311. @"DNS_ARG_CHR_UUID":@"09d3960a-dbb9-11e6-bf26-cec0c932ce01",
  312. @"APPLY_IP_CHR_UUID":@"73207ac8-dd72-11e6-bf26-cec0c932ce01",
  313. @"IP_SET_CHR_UUID":@"73208018-dd72-11e6-bf26-cec0c932ce01",
  314. @"IP_ADDR_CHR_UUID":@"73208126-dd72-11e6-bf26-cec0c932ce01",
  315. @"NET_PRE_LEN_CHR_UUID":@"73208202-dd72-11e6-bf26-cec0c932ce01",
  316. @"GATEWAY_CHR_UUID":@"732082de-dd72-11e6-bf26-cec0c932ce01",
  317. @"DNS_CHR_UUID":@"732083a6-dd72-11e6-bf26-cec0c932ce01"};
  318. return db;
  319. }
  320. #pragma mark - CBCentralManagerDelegate
  321. - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  322. NSLog(@"Central manager changed state: %ld", central.state);
  323. if( _delegate && [_delegate respondsToSelector:@selector(BLEStateChange:)] ) {
  324. [_delegate BLEStateChange:central.state == CBCentralManagerStatePoweredOn] ;
  325. }
  326. if (central.state == CBCentralManagerStatePoweredOn) {
  327. [self startScan];
  328. }
  329. }
  330. - (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals {
  331. NSLog(@"%ld periphirals retrieved", [peripherals count]);
  332. }
  333. - (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals {
  334. for (CBPeripheral *peripheral in peripherals)
  335. {
  336. NSLog(@"1Periphiral discovered: %@, %@", peripheral.name, peripheral.identifier);
  337. BOOL isExist = NO;
  338. for (BTLEDeivceModel *device in _devices)
  339. {
  340. if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]])
  341. {
  342. isExist = YES;
  343. }
  344. }
  345. if (!isExist)
  346. [self addPeripheral:peripheral];
  347. }
  348. }
  349. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
  350. //NSLog(@"2Periphiral discovered: %@, %@, signal strength: %d", peripheral.name, peripheral.identifier, RSSI.intValue);
  351. for (BTLEDeivceModel *device in _devices) {
  352. if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]]) {
  353. return;
  354. }
  355. }
  356. if (peripheral.name != nil && peripheral.name != (id)[NSNull null]) {
  357. if ([peripheral.name rangeOfString:@"DKC"].location != NSNotFound || [peripheral.name rangeOfString:@"BlueZ"].location != NSNotFound) {
  358. BTLEDeivceModel *device = [self addPeripheral:peripheral];
  359. if (![_devices containsObject:device]) {
  360. [_devices addObject:device];
  361. }
  362. }
  363. }
  364. }
  365. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
  366. NSLog(@"Periphiral connected name : %@", peripheral.name);
  367. _isConnected = YES;
  368. [peripheral discoverServices:nil] ;
  369. }
  370. - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
  371. NSLog(@"Periphiral disconnected: %@", peripheral.name);
  372. if( _delegate && [_delegate respondsToSelector:@selector(BLEDisConnected:)] ) {
  373. [_delegate BLEDisConnected:_conDevice] ;
  374. }
  375. _isConnected = NO;
  376. _conDevice = nil;
  377. }
  378. - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
  379. NSLog(@"Periphiral failed to connect: %@", peripheral.name);
  380. }
  381. #pragma mark - CBPeripheralDelegate
  382. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
  383. NSLog(@"\nServices dicovered for peripheral %@:", peripheral.name);
  384. lastService = [peripheral.services lastObject];
  385. for (CBService *service in peripheral.services) {
  386. [peripheral discoverCharacteristics:nil forService:service];
  387. }
  388. }
  389. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
  390. NSLog(@"\ncharacteristics dicovered for service : %@", service.UUID);
  391. if ([lastService.UUID isEqual:service.UUID]) {
  392. _conDevice = [self addPeripheral:peripheral];
  393. lastService = nil;
  394. if( _delegate && [_delegate respondsToSelector:@selector(BLEConnected:)] ) {
  395. [_delegate BLEConnected:_conDevice] ;
  396. }
  397. }
  398. for (CBCharacteristic *characteristic in service.characteristics) {
  399. //NSLog(@"characteristic : %@", characteristic.UUID);
  400. // [self readValueSetNotifyValueForCharacteristic:characteristic peripheral:peripheral];
  401. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  402. // [peripheral readValueForCharacteristic:characteristic];
  403. }
  404. }
  405. - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
  406. NSLog(@"Incoming:\n[%@]\n%@\n%@",
  407. [self getChrName:characteristic],
  408. [self hexStringValue:characteristic],
  409. [self asciiStringValue:characteristic]);
  410. [self updateValueForDelegateWithCharacteristic:characteristic];
  411. }
  412. -(void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices
  413. {
  414. // device = peripheral;
  415. NSLog(@"peripheral : %@", peripheral);
  416. NSLog(@"didModifyServices : %@", invalidatedServices);
  417. }
  418. - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
  419. //_conDevice.peripheralRef = peripheral;
  420. NSLog(@"didWriteValueForCharacteristic [%@]", [self getChrName:characteristic]) ;
  421. }
  422. //update characteristic 데이터 처리
  423. - (void)updateValueForDelegateWithCharacteristic:(CBCharacteristic*)characteristic {
  424. //wlan list
  425. if ([self isReadWlanForCharacteristic:characteristic]) {
  426. if( _delegate && [_delegate respondsToSelector:@selector(BLEWLanUpdateWithKey:result:)] ) {
  427. [_delegate BLEWLanUpdateWithKey:[self getChrName:characteristic]
  428. result:[self asciiStringValue:characteristic]];
  429. }
  430. }
  431. //ssid argument 읽음
  432. else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
  433. [self getServiceUUID:kBLEChrStSSIDArg]]) {
  434. if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiSSIDUpdate:)] ) {
  435. [_delegate BLEWiFiSSIDUpdate:[self asciiStringValue:characteristic]];
  436. }
  437. }
  438. //dhcp 읽음
  439. else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
  440. [self getServiceUUID:kBLEChrStDHCPArg]]) {
  441. if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiDHCPUpdate:)] ) {
  442. [_delegate BLEWiFiDHCPUpdate:nil];
  443. }
  444. }
  445. //wifi connection
  446. else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
  447. [self getServiceUUID:kBLEChrRdConInfo]]) {
  448. if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionUpdate:)] ) {
  449. [_delegate BLEWiFiConnectionUpdate:characteristic];
  450. }
  451. }
  452. //ssid
  453. else if ([self isReadConInfoForCharacteristic:characteristic]) {
  454. if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionInfoUpdateWithKey:result:)] ) {
  455. [_delegate BLEWiFiConnectionInfoUpdateWithKey:[self getChrName:characteristic]
  456. result:[self asciiStringValue:characteristic]];
  457. }
  458. }
  459. }
  460. //wlan list read 인지 판단
  461. - (BOOL)isReadWlanForCharacteristic:(CBCharacteristic*)characteristic {
  462. NSString *uuid = characteristic.UUID.UUIDString;
  463. BOOL rdWiFi = [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList1]] ||
  464. [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList2]] ||
  465. [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList3]];
  466. return rdWiFi;
  467. }
  468. //wlan list read 인지 판단
  469. - (BOOL)isReadConInfoForCharacteristic:(CBCharacteristic*)characteristic {
  470. NSString *uuid = characteristic.UUID.UUIDString;
  471. // static NSString *kBLEChrRdSSID = @"SSID_CHR_UUID";
  472. // static NSString *kBLEChrRdBSSID = @"BSSID_CHR_UUID";
  473. // static NSString *kBLEChrRnIpSet = @"WF_IP_SET_CHR_UUID";
  474. // static NSString *kBLEChrRnIpAddr = @"WF_IP_ADDR_CHR_UUID";
  475. BOOL conInfo = [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdSSID]] ||
  476. [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdBSSID]] ||
  477. [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRnIpSet]] ||
  478. [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRnIpAddr]] ;
  479. return conInfo;
  480. }
  481. @end