Skip to content

Commit d04f5a8

Browse files
Merge pull request #138 from TakayukiHoshi1984/bugfix_linking_reconnect
Linkingデバイスの再接続処理の修正
2 parents 4ecaed3 + efddd26 commit d04f5a8

7 files changed

Lines changed: 63 additions & 22 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/dConnectDeviceHost/Classes/DPHostService.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ - (instancetype) initWithFileManager: (DConnectFileManager *) fileMgr plugin: (i
5757
[self addProfile:[DPHostCanvasProfile new]];
5858
[self addProfile:[DPHostTouchProfile new]];
5959
[self addProfile:[DPHostGeolocationProfile new]];
60-
[self addProfile:[DPHostLightProfile new]];
60+
61+
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
62+
if ([captureDevice isTorchAvailable]
63+
&& [captureDevice isTorchModeSupported:AVCaptureTorchModeOn]
64+
&& [captureDevice isTorchModeSupported:AVCaptureTorchModeOff]) {
65+
[self addProfile:[DPHostLightProfile new]];
66+
}
6167
}
6268
return self;
6369
}

dConnectDevicePlugin/dConnectDeviceLinking/dConnectDeviceLinking/Classes/DPLinkingDevicePlugin.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "DPLinkingDevicePlugin.h"
1111
#import "DPLinkingServiceDiscoveryProfile.h"
1212
#import "DPLinkingSystemProfile.h"
13+
#import "DPLinkingDeviceManager.h"
1314

1415
@implementation DPLinkingDevicePlugin
1516

@@ -24,9 +25,25 @@ - (id) init
2425

2526
[self addProfile:[[DPLinkingServiceDiscoveryProfile alloc] initWithServiceProvider: self.serviceProvider]];
2627
[self addProfile:[DPLinkingSystemProfile systemProfileWithVersion:@"1.0"]];
28+
29+
__weak typeof(self) weakSelf = self;
30+
31+
dispatch_async(dispatch_get_main_queue(), ^{
32+
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
33+
UIApplication *application = [UIApplication sharedApplication];
34+
[notificationCenter addObserver:weakSelf
35+
selector:@selector(enterForeground)
36+
name:UIApplicationWillEnterForegroundNotification
37+
object:application];
38+
});
2739
}
2840
return self;
2941
}
42+
43+
- (void) enterForeground {
44+
[[DPLinkingDeviceManager sharedInstance] restart];
45+
}
46+
3047
#pragma mark - DevicePlugin's icon image
3148

3249
- (NSString*)iconFilePath:(BOOL)isOnline

dConnectDevicePlugin/dConnectDeviceLinking/dConnectDeviceLinking/Classes/linking/DPLinkingDevice.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ - (id) initWithCoder:(NSCoder *)coder {
3434
self.vibrationOffPatternId = [[coder decodeObjectForKey:kVibrationOffPatternId] intValue];
3535
self.connectFlag = [[coder decodeObjectForKey:kConnectFlag] boolValue];
3636
self.temperatureType = DCMTemperatureProfileEnumCelsius;
37+
self.online = NO;
3738

3839
DCLogInfo(@"LDPDevice");
3940
DCLogInfo(@" name: %@", self.name);
4041
DCLogInfo(@" id: %@", self.identifier);
4142
DCLogInfo(@" led: %d", self.ledOffPatternId);
4243
DCLogInfo(@" vibration: %d", self.vibrationOffPatternId);
44+
DCLogInfo(@" connectFlag: %@", self.connectFlag ? @"YES" : @"NO");
4345
}
4446
return self;
4547
}

dConnectDevicePlugin/dConnectDeviceLinking/dConnectDeviceLinking/Classes/linking/DPLinkingDeviceManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
- (void) stopScan;
7373
- (BOOL) isStartScan;
7474

75+
- (void) restart;
76+
7577
- (NSArray *) getDPLinkingDevices;
7678

7779
- (DPLinkingDevice *) createDPLinkingDevice:(CBPeripheral *)peripheral;

dConnectDevicePlugin/dConnectDeviceLinking/dConnectDeviceLinking/Classes/linking/DPLinkingDeviceManager.m

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ - (BOOL) isStartScan {
102102
return [[BLEConnecter sharedInstance] isScanning];
103103
}
104104

105+
- (void) restart {
106+
[self startScanWithTimeout:20];
107+
}
108+
105109
- (DPLinkingDevice *) findDPLinkingDeviceByPeripheral:(CBPeripheral *)peripheral {
106110
__block DPLinkingDevice *result = nil;
107111
[self.devices enumerateObjectsUsingBlock:^(DPLinkingDevice *obj, NSUInteger idx, BOOL *stop) {
@@ -204,6 +208,7 @@ - (void) disconnectDPLinkingDevice:(DPLinkingDevice *)device {
204208

205209
[[BLEConnecter sharedInstance] disconnectByDeviceUUID:device.peripheral.identifier.UUIDString];
206210
device.connectFlag = NO;
211+
device.peripheral = nil;
207212
[self saveDPLinkingDevice];
208213
}
209214

@@ -634,6 +639,7 @@ - (void) removeConnectDelegate:(id<DPLinkingDeviceConnectDelegate>)delegate {
634639

635640
- (void) startScanWithTimeout:(NSTimeInterval)timeout {
636641
if ([BLEConnecter sharedInstance].canDiscovery) {
642+
DCLogInfo(@"DPLinkingDeviceManager::startScanWithTimeout");
637643
[DPLinkingUtil asyncAfterDelay:timeout block:^{
638644
DCLogWarn(@"Timeout.");
639645
_initFlag = NO;
@@ -800,12 +806,18 @@ - (void) cancelConnectingDevice:(CBPeripheral *)peripheral {
800806
}
801807

802808
- (void) failToConnect:(CBPeripheral *)peripheral {
803-
DCLogInfo(@"AAAAA failToConnect: %@", peripheral);
809+
BLEDeviceSetting *setting = [[BLEConnecter sharedInstance] getDeviceByPeripheral:peripheral];
810+
811+
DCLogInfo(@"@@ failToConnect: [デバイス:%@ との接続に失敗しました。]", setting.name);
812+
DCLogInfo(@" setting.notifyDeviceInitial: %@", setting.notifyDeviceInitial ? @"YES" : @"NO");
813+
DCLogInfo(@" setting.initialDeviceSettingFinished: %@", setting.initialDeviceSettingFinished ? @"YES" : @"NO");
814+
DCLogInfo(@" setting.saved: %@", setting.saved ? @"YES" : @"NO");
815+
DCLogInfo(@" setting.connectionStatus: %@", setting.connectionStatus);
804816

805817
if ([self isEqualDeviceUuid:peripheral]) {
806818
DPLinkingDevice *device = [self findDPLinkingDeviceByPeripheral:peripheral];
807819
if (device) {
808-
device.setting = [[BLEConnecter sharedInstance] getDeviceByPeripheral:peripheral];
820+
device.setting = setting;
809821
device.online = NO;
810822

811823
[self notifyFailToConnectDevice:device];
@@ -821,17 +833,15 @@ - (void) didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSD
821833
if (device) {
822834
device.peripheral = peripheral;
823835

824-
if (_initFlag) {
825-
if (device.connectFlag) {
826-
[self connectDPLinkingDevice:device];
836+
if ([self isEqualDeviceUuid:peripheral]) {
837+
if (_scanTimerCancelBlock) {
838+
_scanTimerCancelBlock();
839+
_scanTimerCancelBlock = nil;
827840
}
828-
} else {
829-
if ([self isEqualDeviceUuid:peripheral]) {
830-
if (_scanTimerCancelBlock) {
831-
_scanTimerCancelBlock();
832-
_scanTimerCancelBlock = nil;
833-
}
834-
[self stopScan];
841+
[self stopScan];
842+
[self connectDPLinkingDevice:device];
843+
} else if (_initFlag) {
844+
if (device.connectFlag && !device.online) {
835845
[self connectDPLinkingDevice:device];
836846
}
837847
}
@@ -850,7 +860,12 @@ - (void) didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)
850860
}
851861

852862
- (void)didConnectDevice:(BLEDeviceSetting *)setting {
853-
DCLogInfo(@"@@ didConnectDevice [デバイス:%@ と接続されました。]: %@", setting.name, setting.initialDeviceSettingFinished ? @"true" : @"false");
863+
DCLogInfo(@"@@ didConnectDevice: [デバイス:%@ と接続されました。]", setting.name);
864+
DCLogInfo(@" setting.notifyDeviceInitial: %@", setting.notifyDeviceInitial ? @"YES" : @"NO");
865+
DCLogInfo(@" setting.initialDeviceSettingFinished: %@", setting.initialDeviceSettingFinished ? @"YES" : @"NO");
866+
DCLogInfo(@" setting.saved: %@", setting.saved ? @"YES" : @"NO");
867+
DCLogInfo(@" setting.connectionStatus: %@", setting.connectionStatus);
868+
854869
setting.isInDistanceThreshold = YES;
855870
}
856871

@@ -859,7 +874,7 @@ - (void)didDeviceInitialFinished:(CBPeripheral *)peripheral {
859874

860875
BLEDeviceSetting *setting = [[BLEConnecter sharedInstance] getDeviceByPeripheral:peripheral];
861876

862-
DCLogInfo(@"デバイスを発見しました。");
877+
DCLogInfo(@"デバイスの初期化が完了しました");
863878
DCLogInfo(@" name : %@", setting.peripheral.name);
864879
DCLogInfo(@" device id : %d", setting.deviceId);
865880
DCLogInfo(@" device uid : %d", setting.deviceUid);
@@ -887,7 +902,7 @@ - (void)didDeviceInitialFinished:(CBPeripheral *)peripheral {
887902
}
888903

889904
- (void)didDisconnectPeripheral:(CBPeripheral *)peripheral {
890-
DCLogInfo(@"@@ didDisconnectPeripheral");
905+
DCLogInfo(@"@@ didDisconnectPeripheral: %@", peripheral);
891906

892907
DPLinkingDevice *device = [self findDPLinkingDeviceByPeripheral:peripheral];
893908
if (device) {
@@ -896,7 +911,7 @@ - (void)didDisconnectPeripheral:(CBPeripheral *)peripheral {
896911
}
897912

898913
- (void) didDisconnectDevice:(BLEDeviceSetting *)setting {
899-
DCLogInfo(@"@@ didDisconnectDevice");
914+
DCLogInfo(@"@@ didDisconnectDevice: %@", setting.peripheral);
900915

901916
DPLinkingDevice *device = [self findDPLinkingDeviceByPeripheral:setting.peripheral];
902917
if (device) {

dConnectDevicePlugin/dConnectDeviceLinking/dConnectDeviceLinking/Classes/ui/DPLinkingSearchViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ - (void) startScan {
7070
}
7171

7272
- (void) paringPeripheral:(CBPeripheral *)peripheral {
73-
DCLogInfo(@"paringPeripheral");
73+
DCLogInfo(@"paringPeripheral: %@", peripheral);
7474

7575
_disconnectCount = 0;
7676

dConnectSDK/dConnectSDKForIOS/DConnectSDK/Classes/DConnectManager.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ - (BOOL) start {
231231

232232
_requestQueue = dispatch_queue_create("org.deviceconnect.manager.queue.request", DISPATCH_QUEUE_SERIAL);
233233

234-
// デバイスプラグインの検索
235-
[self.mDeviceManager searchDevicePlugin];
236-
237234
_webServer = [DConnectServerManager new];
238235
_webServer.settings = self.settings;
239236

@@ -249,7 +246,6 @@ - (void) stop {
249246
return;
250247
}
251248
self.mStartFlag = NO;
252-
253249
[_webServer stopServer];
254250
}
255251

@@ -451,6 +447,9 @@ - (id) init {
451447
self.mEventSessionTable = [DConnectEventSessionTable new];
452448
self.mEventBroker = [[DConnectEventBroker alloc] initWithContext:self table:self.mEventSessionTable localOAuth:self.mLocalOAuth pluginManager:self.mDeviceManager delegate:self.delegate];
453449

450+
// デバイスプラグインの検索
451+
[self.mDeviceManager searchDevicePlugin];
452+
454453
// プロファイルの追加
455454
[self addProfile:[DConnectManagerServiceDiscoveryProfile new]];
456455
[self addProfile:[DConnectManagerSystemProfile new]];

0 commit comments

Comments
 (0)