2016-12-15 6 views
0

Objective-C의 블록으로 작업하는 데 문제가 있습니다. 내 문제는 readDataFromCharactUUID 함수의 완료 블록이 do-while-loop를 사용하여 호출되지 않는다는 것입니다. do-while-loop를 사용하지 않고는 한 번 호출됩니다.objective-c 블록 및 do-while 루프

내 코드로 수행하고자하는 작업은 값이 0x01 인 것처럼 BLE 특성에서 값을 자주 읽는 것입니다.

내 질문 : 완료 블록이 실행되지 않은 이유는 무엇입니까? 완성 블록이 내 경우에 처형 될 수 있다는 것을 어떻게 할 수 있습니까?

사용 된 코드 :

static bool dataReady = false; 

-(IBAction)Buttonstartpressed:(UIButton *)sender{ 

LGLog(@"start pressed"); 

NSData *data = [NSData dataWithBytes:(unsigned char[]){CMD_Control_Learn} length:1]; 
[LGUtils writeData :data 
     charactUUID :CHARACTERISTIC_UUID_REMOTEBUDDYControl 
     serviceUUID :SERVICE_UUID_REMOTEBUDDY 
     peripheral :_mBuddy completion:^(NSError *error) 
     { 
      // Befehl wurde übermittelt 
      NSLog(@"Einlernen gesendet => Error : %@", error); 

      do 
      { 
       // RB_Notification Data Ready auslesen 
       [LGUtils readDataFromCharactUUID:CHARACTERISTIC_UUID_REMOTEBUDDYNotification 
            serviceUUID:SERVICE_UUID_REMOTEBUDDY 
             peripheral:_mBuddy 
             completion:^(NSData *data, NSError *error) 
       { 

        NSLog(@"Data : %@ Error : %@", data, error); 


        const uint8_t *bytes = [data bytes]; // pointer to the bytes in data 
        int data_int = bytes[0]; // first byte 

        switch(data_int) 
        { 
         case 0x01: 
          NSLog(@"Data ready!"); 
          dataReady = true; 
          break; 
         case 0x02: 
          NSLog(@"Data Transimission Error!"); 
          break; 
         case 0x00: 
          NSLog(@"No Notification! => check again"); 
          break; 
         default: 
          break; 
        } 
       } 
       ]; 
      } 
      while(!dataReady); 

     }];} 

사전에 감사합니다!

+0

이 가진 모든 행운? – Miknash

답변

0

실행 블록이 비동기 - 다른 스레드에서 실행됨을 의미합니다. 주변 장치에서 읽는 함수를 만들고 읽은 후 결과가 0x01이면 다른 함수를 호출하고 그렇지 않으면 재귀를 사용하여 해당 함수를 호출합니다. (컴파일에 대해 확실하지 100 % -하지 맥에서 지금)이 같은

뭔가 :

NSData *data = [NSData dataWithBytes:(unsigned char[]){CMD_Control_Learn} length:1]; 
[LGUtils writeData :data 
     charactUUID :CHARACTERISTIC_UUID_REMOTEBUDDYControl 
     serviceUUID :SERVICE_UUID_REMOTEBUDDY 
    peripheral :_mBuddy completion:^(NSError *error) 
    { 
     // Befehl wurde übermittelt 
     NSLog(@"Einlernen gesendet => Error : %@", error); 
     // RB_Notification Data Ready auslesen 
     [self checkForStatus:CHARACTERISTIC_UUID_REMOTEBUDDYNotification andServiceUUID: SERVICE_UUID_REMOTEBUDDY andPeripheral:_mBuddy]; 
}]; 
} 

-(void) checkForStatus:(NSString*)characteristic andServiceUUID:(NSString*) service andPeripheral:(CBPeripheral*) _mBuddy{ 
    [LGUtils readDataFromCharactUUID:CHARACTERISTIC_UUID_REMOTEBUDDYNotification 
          serviceUUID:SERVICE_UUID_REMOTEBUDDY 
           peripheral:_mBuddy 
           completion:^(NSData *data, NSError *error) 
     { 

      NSLog(@"Data : %@ Error : %@", data, error); 


      const uint8_t *bytes = [data bytes]; // pointer to the bytes in data 
      int data_int = bytes[0]; // first byte 

      switch(data_int) 
      { 
       case 0x01: 
        NSLog(@"Data ready!"); 
        [self dataReady]; // some of yours functions 

        break; 
       case 0x02: 
        [self checkForStatus:CHARACTERISTIC_UUID_REMOTEBUDDYNotification andServiceUUID: SERVICE_UUID_REMOTEBUDDY andPeripheral:_mBuddy]; 
        break; 
       case 0x00: 
        [self checkForStatus:CHARACTERISTIC_UUID_REMOTEBUDDYNotification andServiceUUID: SERVICE_UUID_REMOTEBUDDY andPeripheral:_mBuddy]; 
        break; 
       default: 
        break; 
      } 
     } 
    ]; 

} 
+0

고맙습니다. !!! –