Pārlūkot izejas kodu

- BLE 모듈 개발완료
=> 연동 테스트 필요

KaRam Kim 8 gadi atpakaļ
vecāks
revīzija
ad94a9fbbc

+ 3 - 0
OneCable/Classes/Categories/NSString-Addtions.h

@@ -42,4 +42,7 @@
 
 - (NSString *)stringWithFormatWithArr:(NSArray*)arguments;
 
+
+-(BOOL)isDigit;
+
 @end

+ 11 - 0
OneCable/Classes/Categories/NSString-Addtions.m

@@ -296,5 +296,16 @@
     return result;
 }
 
+-(BOOL)isDigit
+{
+    NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
+    NSRange nond = [self rangeOfCharacterFromSet:nonDigits];
+    if (NSNotFound == nond.location) {
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
 
 @end

+ 5 - 1
OneCable/Classes/Definitions.h

@@ -45,7 +45,11 @@ static NSString *kWebLinkServer = @"http://61.40.220.23:7794";      //운영
 
 #endif
 
-static NSString *kSocketBoradCast = @"SocketReceiveNoti";      //운영
+static NSString *kSocketBoradCast = @"SocketReceiveNoti";
+
+static NSString *kBLEConnect = @"BLEConnect";
+static NSString *kBLEDisConnect = @"BLEDisConnect";
+
 
 static NSInteger kMaxTimeOut = 60;      //운영
 

+ 55 - 9
OneCable/Classes/Handler/BLEServiceHandler.h

@@ -9,32 +9,78 @@
 #import <Foundation/Foundation.h>
 #import "BTLEDeivceModel.h"
 
+typedef enum {
+    BLEWlanListType1,
+    BLEWlanListType2,
+    BLEWlanListType3,
+} BLEWlanListType;
+
+static NSString *kBLEChrStWiFiScan = @"SCAN_CHR_UUID";
+static NSString *kBLEChrRdWiFiList1 = @"WLAN_LIST1_CHR_UUID";
+static NSString *kBLEChrRdWiFiList2 = @"WLAN_LIST2_CHR_UUID";
+static NSString *kBLEChrRdWiFiList3 = @"WLAN_LIST3_CHR_UUID";
+static NSString *kBLEChrStSSIDArg = @"SSID_ARG_CHR_UUID";
+static NSString *kBLEChrStPWDArg = @"PASSWORD_ARG_CHR_UUID";
+static NSString *kBLEChrStDHCPArg = @"WF_IP_SET_ARG_CHR_UUID";
+static NSString *kBLEChrStSetApply = @"APPLY_CHR_UUID";
+static NSString *kBLEChrRdConInfo = @"CONNECTION_CHR_UUID";
+static NSString *kBLEChrRdSSID = @"SSID_CHR_UUID";
+static NSString *kBLEChrRdBSSID = @"BSSID_CHR_UUID";
+static NSString *kBLEChrRnIpAddr = @"WF_IP_ADDR_CHR_UUID";
+
+
+
 @protocol BLEServiceHandlerDelegate <NSObject>
 
 @optional
-- (void) BLEConnectComplete;
-- (void) BLEDidReceiveMessage:(id)message result:(SocketModel *)result;
-- (void) BLEDidFailWithError:(NSError *)error;
-- (void) BLEDidReceivePong:(NSData *)pongPayload;
-- (void) BLEDidCloseWithCode:(NSInteger)code;
+- (void) BLEConnected:(BTLEDeivceModel *)info;
+- (void) BLEDisConnected:(BTLEDeivceModel *)info;
+- (void) BLEUpldateDevice:(BTLEDeivceModel *)info;
 
+- (void) BLEWiFiConnectionInfoUpdate:(CBCharacteristic *)info;
 @end
 
 @interface BLEServiceHandler : NSObject
 @property (readonly, nonatomic) BOOL isConnected;
+@property (strong, nonatomic) id delegate ;
+@property (strong, nonatomic) BTLEDeivceModel *conDevice;
+
 
 + (id)sharedManager;
 
 
 - (void)connect:(BTLEDeivceModel *)info;
 
+- (void)disConnect;
+
 - (void) startScan;
 - (void) stopScan;
 
-- (void) sendDataWihOutDelegate:(SocketRequestModel *)data;
-- (void) sendDataWithDelegate:(SocketRequestModel *)data delegate:(id)delegate;
-- (void) sendDataWithDelegate:(SocketRequestModel *)data modelClass:(Class)modelClass delegate:(id)delegate;
-- (void) sendDataWithDelegate:(SocketRequestModel *)data modelClass:(Class)modelClass delegate:(id)delegate isShowLoading:(BOOL)isShowLoading;
+- (NSArray *)getDeviceList;
+
+-(CBCharacteristic *)getChrInfo:(NSString *)name;
+-(NSString *)getChrName:(CBCharacteristic *)info;
+-(NSString *)getStrUUID:(CBCharacteristic *)info;
+
+- (NSString*)hexStringValue:(CBCharacteristic *)input;
+- (NSString*)asciiStringValue:(CBCharacteristic *)input;
+
+-(void)sendData:(CBCharacteristic *)chr str:(NSString *)str;
+
+
+//WiFi Setting관련 메뉴
+// WiFi Ap 스캔
+-(void)scanWiFiList;
+// WiFi 스캔결과 조회
+-(NSString *)getWLanList:(BLEWlanListType)type;
+// 스캔된 목록중에 존재하는 SSID를 BLE를 통해 GW에 입력
+-(void)setWiFiSSID:(NSString *)ssid;
+// 입력된 SSID에 해당하는 AP의 Password를 GW에 입력
+-(void)setWiFiPwd:(NSString *)pwd;
+// IP 설정타입을 DHCP로 변경
+-(void)enableDHCP;
 
+// 설정한 정보를 이용해서 AP에 접속 / IP받아오기
+-(void)applyWiFiSettingInfo;
 
 @end

+ 345 - 126
OneCable/Classes/Handler/BLEServiceHandler.m

@@ -8,25 +8,19 @@
 
 #import "BLEServiceHandler.h"
 #import <CoreBluetooth/Corebluetooth.h>
+#import "Definitions.h"
+#import "JDJSONModel.h"
+#import "JDObject.h"
 
 @interface BLEServiceHandler()<CBCentralManagerDelegate, CBPeripheralDelegate>
 {
-    CBCentralManager *manager;
-    NSMutableArray *devices;
-    BOOL scanning;
-    NSTimer *scanTimer;
+    CBCentralManager *_manager;
+    NSMutableArray *_devices;
+    BOOL _scanning;
+    NSTimer *_scanTimer;
     
-    SRWebSocket *_socket;
-    NSMutableArray<SocketRequestModel *> *_messages;
-    //    NSMutableArray *_delegates;
-    
-    int _retryCount;
-    BOOL _isShowLoading;
-    int _loadingRequestId;
+    BTLEDeivceModel *_connectedDevice;
 }
-@property NSMutableDictionary* controllers;
-@property NSMutableDictionary* responses;
-@property int   controllerId;
 
 @end
 
@@ -47,63 +41,293 @@
     self = [super init];
     
     if (self) {
-        
-        _isConnected = NO;
-        
-        
-        _messages = [[NSMutableArray alloc] init];
-        //        _delegates = [[NSMutableArray alloc] init];
-        
-        _controllers = [[NSMutableDictionary alloc] init];
-        _responses = [[NSMutableDictionary alloc] init];
-        
-        self.controllerId   = 0;
-        
-        //        [self socketOpen];
+        _manager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
+//        *peripheralManager = [[CBPeripheralManager alloc]initWithDelegate:nil queue:nil options:@{CBPeripheralManagerOptionShowPowerAlertKey:@NO}]
+        _devices = [NSMutableArray array];
+//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startScan) name:kBLEConnect object:nil];
+//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startScan) name:kBLEDisConnect object:nil];
+
     }
     
     return self;
 }
 
+- (void)connect:(BTLEDeivceModel *)info
+{
+    if (info.peripheralRef.state != CBPeripheralStateDisconnected) {
+        [_manager cancelPeripheralConnection:info.peripheralRef];
+    } else {
+        [_manager connectPeripheral:info.peripheralRef options:nil];
+    }
+}
+
+- (void)disConnect
+{
+    _conDevice = nil;
+}
+
+-(void) openBluetoothSettings{
+    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Bluetooth"]];
+}
+
+
 - (void) startScan
 {
+    if (_manager.state == CBCentralManagerStatePoweredOff) {
+        [[JDFacade facade] confirmTitle:@"Notice"
+                                message:@"블루투스가 비활성화 되어 있습니다. 활성화 해주세요."
+                             btnOKLabel:@"OK"
+                         btnCancelLabel:@"Cancel"
+                             completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
+                                 if (buttonIndex == 1) {
+                                     [self openBluetoothSettings];
+                                 }
+        }];
+
 
+        
+        return;
+    }
+    _devices = [NSMutableArray array];
+    if (_scanTimer) [_scanTimer invalidate];
+    
+    _scanning = YES;
+    [_manager stopScan];
+    
+    [_manager scanForPeripheralsWithServices:nil options:nil];
+    
+    _scanTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(stopScan) userInfo:nil repeats:NO];
 }
 
+
 - (void) stopScan
 {
+    NSLog(@"STOP SCAN");
+    
+    [_manager stopScan] ;
+    _scanning = NO ;
+}
 
+-(NSArray *)getDeviceList
+{
+    return _devices;
 }
 
-- (void) sendDataWihOutDelegate:(SocketRequestModel *)data
+-(CBCharacteristic *)getChrInfo:(NSString *)name
 {
+    CBCharacteristic *result = nil;
+    
+    for (CBService *serviceInfo in [_conDevice getServiceList]) {
+        for (CBCharacteristic *chrInfo in serviceInfo.characteristics) {
+            
+            if ([[self getStrUUID:chrInfo] isEquestToIgnoreCase:[self getServiceUUID:name]]) {
+                result = chrInfo;
+                break;
+            }
+        }
+    }
+    
+    return result;
+}
 
+-(NSString *)getChrName:(CBCharacteristic *)info
+{
+    NSString *result = nil;
+    for (NSString *key in [[self getGatewayCharDict] allKeysForObject:[[self getStrUUID:info] lowercaseString]]) {
+        result = key;
+    }
+    
+    return result;
 }
 
-- (void) sendDataWithDelegate:(SocketRequestModel *)data delegate:(id)delegate
+-(NSString *)getStrUUID:(CBCharacteristic *)info
 {
+    NSString *uuid =[NSString stringWithFormat:@"%@", info.UUID];
+    
+    return uuid;
+}
+
 
+
+- (BTLEDeivceModel *)addPeripheral:(CBPeripheral*)peripheral {
+    BTLEDeivceModel *device = [[BTLEDeivceModel alloc] init];
+    device.peripheralRef = peripheral;
+//    device.manager = _manager;
+    
+    peripheral.delegate = self;
+    
+    [_devices addObject:device];
+    
+    if( _delegate && [_delegate respondsToSelector:@selector(BLEDisConnected:)] ) {
+        [_delegate BLEUpldateDevice:device] ;
+    }
+    
+    return device;
 }
 
-- (void) sendDataWithDelegate:(SocketRequestModel *)data modelClass:(Class)modelClass delegate:(id)delegate
+
+
+
+
+- (NSString*)hexStringValue:(CBCharacteristic *)input {
+    if (!input.value) return @"-";
+    //    NSLog(@"hexString : %@", self.value);
+    NSString *raw = [NSString stringWithFormat:@"0x%@", input.value];
+    raw = [raw stringByReplacingOccurrencesOfString:@"<" withString:@""];
+    raw = [raw stringByReplacingOccurrencesOfString:@">" withString:@""];
+    return raw;
+
+}
+
+- (NSString*)asciiStringValue:(CBCharacteristic *)input {
+    if (!input.value) return @"-";
+    //    NSLog(@"asciiString : %@", self.value);
+    NSString *ascii = [[NSString alloc] initWithData:input.value encoding:NSASCIIStringEncoding];
+    return ascii;
+}
+
+-(void)sendData:(CBCharacteristic *)chr str:(NSString *)str
 {
+    NSData *data = [NSData data];
+    
+    if ([str isDigit]) {
+        int dataToWrite = [str intValue];
+        data = [NSData dataWithBytes:&dataToWrite length:dataToWrite];
+    } else {
+        data = [str dataUsingEncoding:NSASCIIStringEncoding];
+    }
+    
+    if (chr.properties & CBCharacteristicPropertyWriteWithoutResponse)
+        [chr.service.peripheral writeValue:data forCharacteristic:chr type:CBCharacteristicWriteWithoutResponse];
+    else if (chr.properties & CBCharacteristicPropertyWrite)
+        [chr.service.peripheral writeValue:data forCharacteristic:chr type:CBCharacteristicWriteWithResponse];
+}
 
+#pragma mark - WiFi Settings
+//WiFi Setting관련 메뉴
+-(void)scanWiFiList
+{
+    [self sendData:[self getChrInfo:kBLEChrStWiFiScan] str:@"1"];
+    
+    // todo : Timer 돌면서 List 1~3까지 채워지는지 체크할것
 }
 
-- (void) sendDataWithDelegate:(SocketRequestModel *)data modelClass:(Class)modelClass delegate:(id)delegate isShowLoading:(BOOL)isShowLoading
+-(NSString *)getWLanList:(BLEWlanListType)type
 {
+    // TODO : WLanList 가져오기
+    NSString *result = @"";
+    switch (type) {
+        case BLEWlanListType1:
+            result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList1]];
+            break;
+        case BLEWlanListType2:
+            result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList2]];
+            break;
+        case BLEWlanListType3:
+            result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList3]];
+            break;
+            
+        default:
+            break;
+    }
+    return result;
+}
+
+-(void)setWiFiSSID:(NSString *)ssid
+{
+    [self sendData:[self getChrInfo:kBLEChrStSSIDArg] str:ssid];
+}
 
+-(void)setWiFiPwd:(NSString *)pwd
+{
+    [self sendData:[self getChrInfo:kBLEChrStPWDArg] str:pwd];
 }
 
+-(void)enableDHCP
+{
+    [self sendData:[self getChrInfo:kBLEChrStDHCPArg] str:@"1"];
+}
 
+-(void)applyWiFiSettingInfo
+{
+    [self sendData:[self getChrInfo:kBLEChrStSetApply] str:@"1"];
+    // todo : Connection정보가 notify로 들어오면, delegate를 통해서 알리기
+}
+
+
+-(NSString *)getServiceName:(NSString *)uuid
+{
+    NSString *result = nil;
+   
+    
+    for (NSString *key in [[self getGatewayCharDict] allKeysForObject:uuid.lowercaseString]) {
+        result = key;
+    }
+    
+    return result;
+}
+
+-(NSString *)getServiceUUID:(NSString *)strKey
+{
+    NSString *result = nil;
+    
+    
+    for (NSString *key in [[self getGatewayCharDict] allKeys]) {
+        if ([key isEquestToIgnoreCase:strKey]) {
+            result = [[self getGatewayCharDict] objectForKey:key];
+            break;
+        }
+    }
+    
+    return result;
+}
+
+
+-(NSDictionary *)getGatewayCharDict
+{
+    NSDictionary *db = @{@"WFNPS_UUID":@"6f819d94-dddf-11e6-bf26-cec0c932ce01",
+                         @"SCAN_CHR_UUID":@"b364676d-dd76-11e6-bf26-cec0c932ce01",
+                         @"WLAN_LIST1_CHR_UUID":@"f333f87c-0787-11e7-93ae-92361f002671",
+                         @"WLAN_LIST2_CHR_UUID":@"f333fad4-0787-11e7-93ae-92361f002671",
+                         @"WLAN_LIST3_CHR_UUID":@"f333fbec-0787-11e7-93ae-92361f002671",
+                         @"SSID_ARG_CHR_UUID":@"508e6c28-0788-11e7-93ae-92361f002671",
+                         @"PASSWORD_ARG_CHR_UUID":@"b3646c08-dd76-11e6-bf26-cec0c932ce01",
+                         @"CONNECTION_CHR_UUID":@"b3646fbe-dd76-11e6-bf26-cec0c932ce01",
+                         @"SSID_CHR_UUID":@"b3647108-dd76-11e6-bf26-cec0c932ce01",
+                         @"BSSID_CHR_UUID":@"b3647234-dd76-11e6-bf26-cec0c932ce01",
+                         @"WF_IP_SET_ARG_CHR_UUID":@"8e0b49e6-088b-11e7-93ae-92361f002671",
+                         @"WF_IP_ADDR_ARG_CHR_UUID":@"8e0b4c2a-088b-11e7-93ae-92361f002671",
+                         @"WF_NET_PRE_LEN_ARG_CHR_UUID":@"8e0b4e32-088b-11e7-93ae-92361f002671",
+                         @"WF_GATEWAY_ARG_CHR_UUID":@"8e0b4ffe-088b-11e7-93ae-92361f002671",
+                         @"WF_DNS_ARG_CHR_UUID":@"8e0b50d0-088b-11e7-93ae-92361f002671",
+                         @"APPLY_CHR_UUID":@"8e0b51a2-088b-11e7-93ae-92361f002671",
+                         @"WF_IP_SET_CHR_UUID":@"8e0b5314-088b-11e7-93ae-92361f002671",
+                         @"WF_IP_ADDR_CHR_UUID":@"8e0b54a4-088b-11e7-93ae-92361f002671",
+                         @"WF_NET_PRE_LEN_CHR_UUID":@"8e0b5576-088b-11e7-93ae-92361f002671",
+                         @"WF_GATEWAY_CHR_UUID":@"8e0b568e-088b-11e7-93ae-92361f002671",
+                         @"WF_DNS_CHR_UUID":@"8e0b5878-088b-11e7-93ae-92361f002671",
+                         @"IPNPS_UUID":@"09d38ae8-dbb9-11e6-bf26-cec0c932ce01",
+                         @"IP_SET_ARG_CHR_UUID":@"09d38d68-dbb9-11e6-bf26-cec0c932ce01",
+                         @"IP_ADDR_ARG_CHR_UUID":@"09d38e62-dbb9-11e6-bf26-cec0c932ce01",
+                         @"NET_PRE_LEN_ARG_CHR_UUID":@"09d38f48-dbb9-11e6-bf26-cec0c932ce01",
+                         @"GATEWAY_ARG_CHR_UUID":@"09d39024-dbb9-11e6-bf26-cec0c932ce01",
+                         @"DNS_ARG_CHR_UUID":@"09d3960a-dbb9-11e6-bf26-cec0c932ce01",
+                         @"APPLY_IP_CHR_UUID":@"73207ac8-dd72-11e6-bf26-cec0c932ce01",
+                         @"IP_SET_CHR_UUID":@"73208018-dd72-11e6-bf26-cec0c932ce01",
+                         @"IP_ADDR_CHR_UUID":@"73208126-dd72-11e6-bf26-cec0c932ce01",
+                         @"NET_PRE_LEN_CHR_UUID":@"73208202-dd72-11e6-bf26-cec0c932ce01",
+                         @"GATEWAY_CHR_UUID":@"732082de-dd72-11e6-bf26-cec0c932ce01",
+                         @"DNS_CHR_UUID":@"732083a6-dd72-11e6-bf26-cec0c932ce01"};
+    
+    return db;
+}
 
 #pragma mark - CBCentralManagerDelegate
 - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
     NSLog(@"Central manager changed state: %ld", central.state);
-//    
-//    if (central.state == CBCentralManagerStatePoweredOn) {
-//        [self actionScan];
-//    }
+    if (central.state == CBCentralManagerStatePoweredOn) {
+        [self startScan];
+    }
 }
 
 - (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals {
@@ -112,108 +336,103 @@
 
 - (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals {
     
-//    for (CBPeripheral *peripheral in peripherals) {
-//        NSLog(@"Periphiral discovered: %@", peripheral.name);
-//        
-//        BOOL found = NO;
-//        for (BTLEDevice *device in devices) {
-//            if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]]) {
-//                found = YES;
-//            }
-//        }
-//        if (!found)
-//            [self addPeripheral:peripheral];
-//        
-//    }
-//    [self.tableView reloadData];
+    for (CBPeripheral *peripheral in peripherals)
+    {
+        NSLog(@"Periphiral discovered: %@", peripheral.name);
+
+        BOOL isExist = NO;
+        for (BTLEDeivceModel *device in _devices)
+        {
+            if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]])
+            {
+                isExist = YES;
+            }
+        }
+        if (!isExist)
+            [self addPeripheral:peripheral];
+
+    }
 }
 
 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
-    //    NSLog(@"Periphiral discovered: %@, signal strength: %d", peripheral.name, RSSI.intValue);
-    //    NSLog(@"Periphiral discovered: %@, signal strength: %d", , RSSI.intValue);
-//    for (BTLEDevice *device in devices) {
-//        if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]]) {
-//            return;
-//        }
-//    }
-//    if (peripheral.name != nil && peripheral.name != (id)[NSNull null]) {
-//        if ([peripheral.name rangeOfString:@"DKC"].location != NSNotFound || [peripheral.name rangeOfString:@"BlueZ"].location != NSNotFound) {
-//            BTLEDevice *device = [self addPeripheral:peripheral];
-//            device.advertisementData = advertisementData;
-//            device.RSSI = RSSI;
-//        }
-//    }
-//    
-//    
-//    
-//    [self.tableView reloadData];
+        NSLog(@"Periphiral discovered: %@, signal strength: %d", peripheral.name, RSSI.intValue);
+        for (BTLEDeivceModel *device in _devices) {
+            if ([[device.peripheralRef.identifier UUIDString] isEqualToString:[peripheral.identifier UUIDString]]) {
+                return;
+            }
+        }
+        if (peripheral.name != nil && peripheral.name != (id)[NSNull null]) {
+            if ([peripheral.name rangeOfString:@"DKC"].location != NSNotFound || [peripheral.name rangeOfString:@"BlueZ"].location != NSNotFound) {
+                BTLEDeivceModel *device = [self addPeripheral:peripheral];
+                device.advertisementData = advertisementData;
+                device.RSSI = RSSI;
+            }
+        }
 }
 
 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
-//    NSLog(@"Periphiral connected: %@", peripheral.name);
-//    
-//    [[Logger shared] appendWithDevice:peripheral event:EventConnected service:nil characteristic:nil data:nil];
-//    
-//    [[NSNotificationCenter defaultCenter] postNotificationName:@"connected" object:nil];
-//    
-//    [self.tableView reloadData];
+    NSLog(@"Periphiral connected: %@", peripheral.name);
+    _conDevice = [self addPeripheral:peripheral];
+    if( _delegate && [_delegate respondsToSelector:@selector(BLEConnected:)] ) {
+        [_delegate BLEConnected:_conDevice] ;
+    }
 }
 
 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
-//    NSLog(@"Periphiral disconnected: %@", peripheral.name);
-//    
-//    [[Logger shared] appendWithDevice:peripheral event:EventDisconnected service:nil characteristic:nil data:nil];
-//    
-//    [[NSNotificationCenter defaultCenter] postNotificationName:@"disconnected" object:nil];
-//    
-//    [self.tableView reloadData];
-    
+    NSLog(@"Periphiral disconnected: %@", peripheral.name);
+    if( _delegate && [_delegate respondsToSelector:@selector(BLEDisConnected:)] ) {
+        [_delegate BLEDisConnected:_conDevice] ;
+    }
+    _conDevice = nil;
 }
 
 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
-//    NSLog(@"Periphiral failed to connect: %@", peripheral.name);
-//    
-//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Failed to connect" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
-//    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
-//    [self presentViewController:alert animated:true completion:nil];
-}
-
-
-/*
- #define BLE_WFNPS_UUID                                  @"6f819d94-dddf-11e6-bf26-cec0c932ce01"
- #define BLE_SCAN_CHR_UUID         	                    @"b364676d-dd76-11e6-bf26-cec0c932ce01"
- #define BLE_WLAN_LIST1_CHR_UUID                         @"f333f87c-0787-11e7-93ae-92361f002671"
- #define BLE_WLAN_LIST2_CHR_UUID                         @"f333fad4-0787-11e7-93ae-92361f002671"
- #define BLE_WLAN_LIST3_CHR_UUID                         @"f333fbec-0787-11e7-93ae-92361f002671"
- #define BLE_SSID_ARG_CHR_UUID                           @"508e6c28-0788-11e7-93ae-92361f002671"
- #define BLE_PASSWORD_ARG_CHR_UUID                 		@"b3646c08-dd76-11e6-bf26-cec0c932ce01"
- #define BLE_CONNECTION_CHR_UUID                        	@"b3646fbe-dd76-11e6-bf26-cec0c932ce01"
- #define BLE_SSID_CHR_UUID                               @"b3647108-dd76-11e6-bf26-cec0c932ce01"
- #define BLE_BSSID_CHR_UUID                              @"b3647234-dd76-11e6-bf26-cec0c932ce01"
- #define BLE_WF_IP_SET_ARG_CHR_UUID                   	@"8e0b49e6-088b-11e7-93ae-92361f002671"
- #define BLE_WF_IP_ADDR_ARG_CHR_UUID                		@"8e0b4c2a-088b-11e7-93ae-92361f002671"
- #define BLE_WF_NET_PRE_LEN_ARG_CHR_UUID     			@"8e0b4e32-088b-11e7-93ae-92361f002671"
- #define BLE_WF_GATEWAY_ARG_CHR_UUID               		@"8e0b4ffe-088b-11e7-93ae-92361f002671"
- #define BLE_WF_DNS_ARG_CHR_UUID                         @"8e0b50d0-088b-11e7-93ae-92361f002671"
- #define BLE_APPLY_CHR_UUID              	            @"8e0b51a2-088b-11e7-93ae-92361f002671"
- #define BLE_WF_IP_SET_CHR_UUID                          @"8e0b5314-088b-11e7-93ae-92361f002671"
- #define BLE_WF_IP_ADDR_CHR_UUID                         @"8e0b54a4-088b-11e7-93ae-92361f002671"
- #define BLE_WF_NET_PRE_LEN_CHR_UUID               		@"8e0b5576-088b-11e7-93ae-92361f002671"
- #define BLE_WF_GATEWAY_CHR_UUID                        	@"8e0b568e-088b-11e7-93ae-92361f002671"
- #define BLE_WF_DNS_CHR_UUID                             @"8e0b5878-088b-11e7-93ae-92361f002671"
- #define BLE_IPNPS_UUID                                  @"09d38ae8-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_IP_SET_ARG_CHR_UUID         	          	@"09d38d68-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_IP_ADDR_ARG_CHR_UUID                        @"09d38e62-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_NET_PRE_LEN_ARG_CHR_UUID             		@"09d38f48-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_GATEWAY_ARG_CHR_UUID                     	@"09d39024-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_DNS_ARG_CHR_UUID                            @"09d3960a-dbb9-11e6-bf26-cec0c932ce01"
- #define BLE_APPLY_IP_CHR_UUID                           @"73207ac8-dd72-11e6-bf26-cec0c932ce01"
- #define BLE_IP_SET_CHR_UUID                             @"73208018-dd72-11e6-bf26-cec0c932ce01"
- #define BLE_IP_ADDR_CHR_UUID                            @"73208126-dd72-11e6-bf26-cec0c932ce01"
- #define BLE_NET_PRE_LEN_CHR_UUID                       	@"73208202-dd72-11e6-bf26-cec0c932ce01"
- #define BLE_GATEWAY_CHR_UUID                            @"732082de-dd72-11e6-bf26-cec0c932ce01"
- #define BLE_DNS_CHR_UUID                                @"732083a6-dd72-11e6-bf26-cec0c932ce01"
- 
- */
+    NSLog(@"Periphiral failed to connect: %@", peripheral.name);
+}
+
+#pragma mark - CBPeripheralDelegate
+- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
+    NSLog(@"Services dicovered for peripheral %@:", peripheral.name);
+    NSLog(@"error dicovered for peripheral %@:", error);
+    
+    for (CBService *service in peripheral.services) {
+        NSLog(@"%@", service.UUID);
+        
+        [peripheral discoverCharacteristics:nil forService:service];
+    }
+    
+}
+
+- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
+    NSLog(@"characteristics dicovered for service %@:", service.UUID);
+    
+    for (CBCharacteristic *characteristic in service.characteristics) {
+        NSLog(@"%@", characteristic.UUID);
+        
+        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
+        [peripheral readValueForCharacteristic:characteristic];
+    }
+   
+}
+
+
+
+- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
+    NSLog(@"Incoming: [%@] %@", [self getChrName:characteristic],[self asciiStringValue:characteristic]);
+    if ([[self getStrUUID:characteristic] isEqualToString:[self getServiceUUID:kBLEChrRdConInfo]]) {
+        if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionInfoUpdate:)] ) {
+            [_delegate BLEWiFiConnectionInfoUpdate:characteristic];
+        }
+    }
+    
+}
+
+-(void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices
+{
+    //    device = peripheral;
+    NSLog(@"peripheral : %@", peripheral);
+    NSLog(@"didModifyServices : %@", invalidatedServices);
+    
+}
 
 @end

+ 4 - 2
OneCable/Classes/Model/BTLEDeivceModel.h

@@ -9,14 +9,15 @@
 #import <Foundation/Foundation.h>
 #import <CoreBluetooth/Corebluetooth.h>
 
+
+@protocol BTLEDeivceModel;
 @interface BTLEDeivceModel : NSObject 
 
 @property (nonatomic, strong) CBPeripheral *peripheralRef;
 @property (nonatomic, strong) NSDictionary *advertisementData;
 @property (nonatomic, strong) CBCentralManager *manager;
 @property (nonatomic, strong) NSNumber *RSSI;
-//@property (nonatomic, strong) NSString *advServices;
-//@property (nonatomic, strong) NSString *brcData;
+
 
 
 - (NSString*)advertisedServices;
@@ -26,5 +27,6 @@
 - (NSString*)name;
 - (NSString*)broadcastData;
 
+-(NSArray*)getServiceList;
 
 @end

+ 10 - 0
OneCable/Classes/Model/BTLEDeivceModel.m

@@ -10,6 +10,10 @@
 #import "CBUUID+String.h"
 
 @interface BTLEDeivceModel () {
+//    CBPeripheral *peripheralRef;
+//    NSDictionary *advertisementData;
+//    CBCentralManager *manager;
+    
     NSString *advServices;
     NSString *brcData;
 }
@@ -86,4 +90,10 @@
     return brcData;
 }
 
+
+-(NSArray*)getServiceList
+{
+    return _peripheralRef.services;
+}
+
 @end