iOSでBLE接続をするアプリを作っております。
iOSアプリがBLE通信の受信側(セントラル)です。
アプリが停止したあとも自動でBLE機器の近くに来た時に通信させたいと思い、状態の保存と復元を実装しようとしました。
ところが、CBCentralManager
クラスの初期化のオプションCBCentralManagerOptionRestoreIdentifierKey
を追加したところ、アプリ側BLEの受信ができなくなってしまいました。
何か、接続方法で間違ってしまったのでしょうか。接続できるようにしたいと思っています。なにか情報があれば(確認すべき点)お伝えいただけると幸いです。
また不足している情報があればお伝え下さい。
Objective
1-(void)initPropertyWithUUIDs:(NSArray *)UUIDS 2{ 3 //BluetoothがOFFの場合にアラートを出す 4 NSDictionary *options = @{ 5 6 CBCentralManagerOptionRestoreIdentifierKey:@"myidentifierKey",//->追加して接続ができなくなってしまいました。 7 CBCentralManagerOptionShowPowerAlertKey:@YES 8 9 }; 10 11 self.centralManager = [[CBCentralManager alloc] initWithDelegate:self 12 queue:nil 13 options:options]; 14}
上記の初期化をしたところ、BLEの接続ができなくなってしまいました。
ちなみに下記のようにコメントアウトして、CBCentralManagerOptionShowPowerAlertKeyのみ指定したところ接続を確認しています。
CBCentralManagerOptionRestoreIdentifierKey
の追加だけでなぜ接続すらできなくなってしまったのかがわからい状態です。
Objective
1 //BluetoothがOFFの場合にアラートを出す 2 NSDictionary *options = @{ 3 4 // CBCentralManagerOptionRestoreIdentifierKey:@"myidentifierKey",//->追加して接続ができなくなってしまいました。 5 CBCentralManagerOptionShowPowerAlertKey:@YES 6 7 };
ちなみに、実際に接続させるメソッドはこちらです。
Objective
1-(void)connectStart 2{ 3 // 探索対象のデバイスが持つサービスを指定 4 NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kUUIDforService],nil]; 5 // 単一デバイスの発見イベントを重複して発行させない 6 NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] 7 forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; 8 //接続開始 9 [self.centralManager scanForPeripheralsWithServices:services 10 options:options]; 11}
また、状態の保存と復元を行うデリゲートメソッドも下記のように実装しています。
Objective
1-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *,id> *)dict 2{ 3 4 NSLog(@"状態の保存と復元"); 5 6 NSString *msg = [NSString stringWithFormat:@"セントラル復元:%@", dict]; 7 8 //復元されたかどうかをみるためにローカル通知を発行させる 9 if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { 10 11 UILocalNotification *localNotification = [UILocalNotification new]; 12 localNotification.alertBody = msg; 13 localNotification.fireDate = [NSDate date]; 14 localNotification.soundName = UILocalNotificationDefaultSoundName; 15 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 16 17 } 18}
iPhone6 plus iOS9.2で開発しています。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/21 09:56
2016/01/21 10:45
2016/01/21 11:03
2016/01/21 11:05
2016/01/21 11:12
2016/01/21 11:22
2016/01/21 11:33
2016/01/21 16:41
2016/01/22 06:21
2016/01/22 06:55
2016/01/22 08:27