|
|
@@ -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
|