|
|
@@ -59,11 +59,12 @@
|
|
|
|
|
|
- (void)connect:(BTLEDeivceModel *)info
|
|
|
{
|
|
|
- if (info.peripheralRef.state != CBPeripheralStateDisconnected) {
|
|
|
- [_manager cancelPeripheralConnection:info.peripheralRef];
|
|
|
- } else {
|
|
|
- [_manager connectPeripheral:info.peripheralRef options:nil];
|
|
|
- }
|
|
|
+ [_manager connectPeripheral:info.peripheralRef options:nil];
|
|
|
+// if (info.peripheralRef.state != CBPeripheralStateDisconnected) {
|
|
|
+// [_manager cancelPeripheralConnection:info.peripheralRef];
|
|
|
+// } else {
|
|
|
+// [_manager connectPeripheral:info.peripheralRef options:nil];
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
- (void)disConnect
|
|
|
@@ -76,9 +77,8 @@
|
|
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Bluetooth"]];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-- (void) startScan
|
|
|
-{
|
|
|
+- (BOOL) checkBLEStatus {
|
|
|
+
|
|
|
if (_manager.state == CBCentralManagerStatePoweredOff) {
|
|
|
|
|
|
[[JDFacade facade] confirmTitle:@"Notice"
|
|
|
@@ -86,15 +86,25 @@
|
|
|
btnOKLabel:@"OK"
|
|
|
btnCancelLabel:@"Cancel"
|
|
|
completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
|
|
|
+
|
|
|
if (buttonIndex == 1) {
|
|
|
|
|
|
[self openBluetoothSettings];
|
|
|
}
|
|
|
- }];
|
|
|
+ }];
|
|
|
|
|
|
- return;
|
|
|
+ return NO;
|
|
|
}
|
|
|
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (void) startScan
|
|
|
+{
|
|
|
+ if (![self checkBLEStatus])
|
|
|
+ return;
|
|
|
+
|
|
|
_devices = [NSMutableArray array];
|
|
|
|
|
|
if (_scanTimer) [_scanTimer invalidate];
|
|
|
@@ -105,7 +115,7 @@
|
|
|
|
|
|
[_manager scanForPeripheralsWithServices:nil options:nil];
|
|
|
|
|
|
- _scanTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stopScan:) userInfo:@"Y" repeats:NO];
|
|
|
+ _scanTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stopScan:) userInfo:@"Y" repeats:NO];
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -191,7 +201,7 @@
|
|
|
|
|
|
- (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:@""];
|
|
|
@@ -228,12 +238,7 @@
|
|
|
//WiFi Setting관련 메뉴
|
|
|
-(void)scanWiFiList
|
|
|
{
|
|
|
- NSLog(@"scanWiFiList : %@", [self getChrInfo:kBLEChrStWiFiScan]) ;
|
|
|
-
|
|
|
[self sendData:[self getChrInfo:kBLEChrStWiFiScan] str:@"1"];
|
|
|
-
|
|
|
- // todo : Timer 돌면서 List 1~3까지 채워지는지 체크할것
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-(NSString *)getWLanList:(BLEWlanListType)type
|
|
|
@@ -242,26 +247,47 @@
|
|
|
NSString *result = @"";
|
|
|
switch (type) {
|
|
|
case BLEWlanListType1:
|
|
|
- result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList1]];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList1 isNotify:NO];
|
|
|
+ //result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList1]];
|
|
|
break;
|
|
|
case BLEWlanListType2:
|
|
|
- result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList2]];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList2 isNotify:NO];
|
|
|
+ //result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList2]];
|
|
|
break;
|
|
|
case BLEWlanListType3:
|
|
|
- result = [self asciiStringValue:[self getChrInfo:kBLEChrRdWiFiList3]];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRdWiFiList3 isNotify:NO];
|
|
|
+ //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를 통해서 알리기
|
|
|
+}
|
|
|
+
|
|
|
-(void)readAndNotifyCharacteristicUUID:(NSString *)uuid
|
|
|
isNotify:(BOOL)isNotify
|
|
|
{
|
|
|
@@ -272,13 +298,10 @@
|
|
|
|
|
|
if ([characteristic.UUID.UUIDString isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:uuid]]) {
|
|
|
|
|
|
- if (isNotify) {
|
|
|
-
|
|
|
+ if (isNotify)
|
|
|
[_conDevice.peripheralRef setNotifyValue:YES forCharacteristic:characteristic];
|
|
|
- }
|
|
|
- else {
|
|
|
+ else
|
|
|
[_conDevice.peripheralRef readValueForCharacteristic:characteristic];
|
|
|
- }
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
@@ -286,23 +309,19 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
--(void)setWiFiPwd:(NSString *)pwd
|
|
|
-{
|
|
|
- [self sendData:[self getChrInfo:kBLEChrStPWDArg] str:pwd];
|
|
|
-}
|
|
|
-
|
|
|
--(void)enableDHCP
|
|
|
-{
|
|
|
- [self sendData:[self getChrInfo:kBLEChrStDHCPArg] str:@"1"];
|
|
|
+- (void)readConnectionWiFiInfo {
|
|
|
+
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRdSSID isNotify:NO];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRdBSSID isNotify:NO];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRnIpSet isNotify:NO];
|
|
|
+ [self readAndNotifyCharacteristicUUID:kBLEChrRnIpAddr isNotify:NO];
|
|
|
}
|
|
|
|
|
|
--(void)applyWiFiSettingInfo
|
|
|
-{
|
|
|
- [self sendData:[self getChrInfo:kBLEChrStSetApply] str:@"1"];
|
|
|
- // todo : Connection정보가 notify로 들어오면, delegate를 통해서 알리기
|
|
|
+- (NSString*)getStringValueForCharacteristicWithKey:(NSString*)key {
|
|
|
+
|
|
|
+ return [self asciiStringValue:[self getChrInfo:key]];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-(NSString *)getServiceName:(NSString *)uuid
|
|
|
{
|
|
|
NSString *result = nil;
|
|
|
@@ -420,7 +439,7 @@
|
|
|
|
|
|
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
|
|
|
|
|
|
- NSLog(@"2Periphiral discovered: %@, %@, signal strength: %d", peripheral.name, peripheral.identifier, RSSI.intValue);
|
|
|
+ //NSLog(@"2Periphiral discovered: %@, %@, signal strength: %d", peripheral.name, peripheral.identifier, RSSI.intValue);
|
|
|
|
|
|
for (BTLEDeivceModel *device in _devices) {
|
|
|
|
|
|
@@ -438,7 +457,6 @@
|
|
|
|
|
|
[_devices addObject:device];
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -447,6 +465,7 @@
|
|
|
|
|
|
NSLog(@"Periphiral connected name : %@", peripheral.name);
|
|
|
|
|
|
+ _isConnected = YES;
|
|
|
[peripheral discoverServices:nil] ;
|
|
|
|
|
|
}
|
|
|
@@ -454,10 +473,12 @@
|
|
|
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
|
|
|
|
|
|
NSLog(@"Periphiral disconnected: %@", peripheral.name);
|
|
|
+
|
|
|
if( _delegate && [_delegate respondsToSelector:@selector(BLEDisConnected:)] ) {
|
|
|
[_delegate BLEDisConnected:_conDevice] ;
|
|
|
}
|
|
|
|
|
|
+ _isConnected = NO;
|
|
|
_conDevice = nil;
|
|
|
}
|
|
|
|
|
|
@@ -470,15 +491,12 @@
|
|
|
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
|
|
|
|
|
|
NSLog(@"\nServices dicovered for peripheral %@:", peripheral.name);
|
|
|
- //NSLog(@"Services dicovered for peripheral : %@", peripheral.services);
|
|
|
- //NSLog(@"error dicovered for peripheral %@:", error);
|
|
|
-
|
|
|
+
|
|
|
|
|
|
lastService = [peripheral.services lastObject];
|
|
|
|
|
|
for (CBService *service in peripheral.services) {
|
|
|
- //NSLog(@"%@", service.UUID);
|
|
|
-
|
|
|
+
|
|
|
[peripheral discoverCharacteristics:nil forService:service];
|
|
|
}
|
|
|
}
|
|
|
@@ -486,7 +504,6 @@
|
|
|
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
|
|
|
|
|
|
NSLog(@"\ncharacteristics dicovered for service : %@", service.UUID);
|
|
|
- //NSLog(@"characteristics dicovered for service : %@", peripheral.services);
|
|
|
|
|
|
if ([lastService.UUID isEqual:service.UUID]) {
|
|
|
|
|
|
@@ -501,7 +518,7 @@
|
|
|
for (CBCharacteristic *characteristic in service.characteristics) {
|
|
|
//NSLog(@"characteristic : %@", characteristic.UUID);
|
|
|
|
|
|
- [self readValueSetNotifyValueForCharacteristic:characteristic peripheral:peripheral];
|
|
|
+// [self readValueSetNotifyValueForCharacteristic:characteristic peripheral:peripheral];
|
|
|
// [peripheral setNotifyValue:YES forCharacteristic:characteristic];
|
|
|
// [peripheral readValueForCharacteristic:characteristic];
|
|
|
}
|
|
|
@@ -511,31 +528,13 @@
|
|
|
|
|
|
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
|
|
|
|
|
|
- NSLog(@"Incoming: [%@] %@ %@", [self getChrName:characteristic],[self asciiStringValue:characteristic], [self hexStringValue:characteristic]);
|
|
|
+ NSLog(@"Incoming: [%@], %@, %@, %@",
|
|
|
+ [self getChrName:characteristic],
|
|
|
+ characteristic.value,
|
|
|
+ [self asciiStringValue:characteristic],
|
|
|
+ [self hexStringValue:characteristic]);
|
|
|
|
|
|
- if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
- [self getServiceUUID:kBLEChrRdConInfo]]) {
|
|
|
-
|
|
|
- if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionInfoUpdate:)] ) {
|
|
|
- [_delegate BLEWiFiConnectionInfoUpdate:characteristic];
|
|
|
- }
|
|
|
- }
|
|
|
- //ssid 읽음
|
|
|
- else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
- [self getServiceUUID:kBLEChrStSSIDArg]]) {
|
|
|
-
|
|
|
- if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiSSIDRead:)] ) {
|
|
|
- [_delegate BLEWiFiSSIDRead:[self asciiStringValue:characteristic]];
|
|
|
- }
|
|
|
- }
|
|
|
- //dhcp 읽음
|
|
|
- else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
- [self getServiceUUID:kBLEChrStDHCPArg]]) {
|
|
|
-
|
|
|
- if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiDHCPRead:)] ) {
|
|
|
- [_delegate BLEWiFiDHCPRead:nil];
|
|
|
- }
|
|
|
- }
|
|
|
+ [self updateValueForDelegateWithCharacteristic:characteristic];
|
|
|
}
|
|
|
|
|
|
-(void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices
|
|
|
@@ -549,25 +548,89 @@
|
|
|
|
|
|
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
|
|
|
|
|
|
+ //_conDevice.peripheralRef = peripheral;
|
|
|
+
|
|
|
NSLog(@"didWriteValueForCharacteristic : %@", characteristic.value) ;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
-//characteristic 에 맞춰서 read or notify
|
|
|
-- (void)readValueSetNotifyValueForCharacteristic:(CBCharacteristic*)characteristic
|
|
|
- peripheral:(CBPeripheral *)peripheral {
|
|
|
+//update characteristic 데이터 처리
|
|
|
+- (void)updateValueForDelegateWithCharacteristic:(CBCharacteristic*)characteristic {
|
|
|
+
|
|
|
+ //wlan list
|
|
|
+ if ([self isReadWlanForCharacteristic:characteristic]) {
|
|
|
+
|
|
|
+ if( _delegate && [_delegate respondsToSelector:@selector(BLEWLanUpdateWithKey:result:)] ) {
|
|
|
+ [_delegate BLEWLanUpdateWithKey:[self getChrName:characteristic]
|
|
|
+ result:[self asciiStringValue:characteristic]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //ssid argument 읽음
|
|
|
+ else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
+ [self getServiceUUID:kBLEChrStSSIDArg]]) {
|
|
|
+
|
|
|
+ if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiSSIDUpdate:)] ) {
|
|
|
+ [_delegate BLEWiFiSSIDUpdate:[self asciiStringValue:characteristic]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //dhcp 읽음
|
|
|
+ else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
+ [self getServiceUUID:kBLEChrStDHCPArg]]) {
|
|
|
+
|
|
|
+ if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiDHCPUpdate:)] ) {
|
|
|
+ [_delegate BLEWiFiDHCPUpdate:nil];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //wifi connection
|
|
|
+ else if ([[self getStrUUID:characteristic] isEquestToIgnoreCase:
|
|
|
+ [self getServiceUUID:kBLEChrRdConInfo]]) {
|
|
|
+
|
|
|
+ if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionUpdate:)] ) {
|
|
|
+ [_delegate BLEWiFiConnectionUpdate:characteristic];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //ssid
|
|
|
+ else if ([self isReadConInfoForCharacteristic:characteristic]) {
|
|
|
+
|
|
|
+ if( _delegate && [_delegate respondsToSelector:@selector(BLEWiFiConnectionInfoUpdateWithKey:result:)] ) {
|
|
|
+ [_delegate BLEWiFiConnectionInfoUpdateWithKey:[self getChrName:characteristic]
|
|
|
+ result:[self asciiStringValue:characteristic]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//wlan list read 인지 판단
|
|
|
+- (BOOL)isReadWlanForCharacteristic:(CBCharacteristic*)characteristic {
|
|
|
|
|
|
NSString *uuid = characteristic.UUID.UUIDString;
|
|
|
|
|
|
BOOL rdWiFi = [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList1]] ||
|
|
|
[uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList2]] ||
|
|
|
[uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdWiFiList3]];
|
|
|
-
|
|
|
- if (rdWiFi) {
|
|
|
-
|
|
|
- [peripheral readValueForCharacteristic:characteristic];
|
|
|
- }
|
|
|
+
|
|
|
+ return rdWiFi;
|
|
|
}
|
|
|
|
|
|
+//wlan list read 인지 판단
|
|
|
+- (BOOL)isReadConInfoForCharacteristic:(CBCharacteristic*)characteristic {
|
|
|
+
|
|
|
+ NSString *uuid = characteristic.UUID.UUIDString;
|
|
|
+
|
|
|
+ // static NSString *kBLEChrRdSSID = @"SSID_CHR_UUID";
|
|
|
+ // static NSString *kBLEChrRdBSSID = @"BSSID_CHR_UUID";
|
|
|
+ // static NSString *kBLEChrRnIpSet = @"WF_IP_SET_CHR_UUID";
|
|
|
+ // static NSString *kBLEChrRnIpAddr = @"WF_IP_ADDR_CHR_UUID";
|
|
|
+
|
|
|
+
|
|
|
+ BOOL conInfo = [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdSSID]] ||
|
|
|
+ [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRdBSSID]] ||
|
|
|
+ [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRnIpSet]] ||
|
|
|
+ [uuid isEquestToIgnoreCase:[self getGatewayDicUUIDForKey:kBLEChrRnIpAddr]] ;
|
|
|
+
|
|
|
+ return conInfo;
|
|
|
+}
|
|
|
@end
|