[iOS]すれちがい通信(その4)
それでは、見つける側となるCentralのサンプルコードを見ていこう。
CBCentralManagerのインスタンスを生成すると、すぐにスキャンできるようになるわけではない。stateがCBCentralManagerStatePoweredOnになったらスキャンを開始する事になる。
...self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];...- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ if (central.state != CBCentralManagerStatePoweredOn) { return; } /* スキャン */ [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:WIBREE_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];}UUIDがWIBREE_SERVICE_UUIDのサービスをスキャンしている。
スキャンしているPeripheralが見つかるとメソッドが呼ばれるので、見つけたPeripheralと接続する。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ if (self.discoveredPeripheral != peripheral) { self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:peripheral options:nil]; }}接続できたらサービスUUIDとキャラクタリスティックUUIDを指定して、識別子を受け取る。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ [self.centralManager stopScan]; [self.data setLength:0]; peripheral.delegate = self; [peripheral discoverServices:@[[CBUUID UUIDWithString:WIBREE_SERVICE_UUID]]];} - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:WIBREE_CHARACTERISTIC_UUID]] forService:service]; }} - (void)peripheral:(CBPeripheral *)peripheraldidDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:WIBREE_CHARACTERISTIC_UUID]]) { [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } }} - (void)peripheral:(CBPeripheral *)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]; if ([stringFromData isEqualToString:@"EOM"]) { NSString *uniqueIdentifier = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]; DBGMSG(@"%s UUID(%@)", __func__, uniqueIdentifier); [peripheral setNotifyValue:NO forCharacteristic:characteristic]; [self.centralManager cancelPeripheralConnection:peripheral]; } [self.data appendData:characteristic.value];} - (void)peripheral:(CBPeripheral *)peripheraldidUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:WIBREE_CHARACTERISTIC_UUID]]) { return; } if (characteristic.isNotifying) { } else { [self.centralManager cancelPeripheralConnection:peripheral]; }}識別子を受け取ったら切断しているので、切断したら呼ばれるメソッドで、スキャンを再開する。
- (void)centralManager:(CBCentralManager *)centraldidDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ self.discoveredPeripheral = nil; [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:WIBREE_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];}ソースコード
GitHubからどうぞ。
関連情報
【Cocoa練習帳】
