2016-12-15 2 views
0

Ich habe ein Problem mit Blöcken in Objective-C. Mein Problem ist, dass der Completion-Block von der Funktion readDataFromCharactUUID niemals mit der Do-while-Schleife aufgerufen wird. Ohne die Do-while-Schleife zu verwenden, wird sie einmal aufgerufen.Blöcke und Do-While-Schleife in Ziel-C

Was ich mit meinem Code tun möchte, ist einen Wert aus einem BLE-Merkmal so oft zu lesen, wie der Wert 0x01 ist.

Meine Frage: Warum wird der Completion Block nie ausgeführt? Was kann ich tun, dass der Completion Block in meinem Fall ausgeführt wird?

Gebrauchte Code:

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); 

     }];} 

Vielen Dank im Voraus!

+0

Glück gehabt mit diesem? – Miknash

Antwort

0

Dieser Ausführungsblock ist asynchron - was bedeutet, dass er auf einem anderen Thread ausgeführt wird. Ich würde Funktion zum Lesen von Peripheriegeräten erstellen und nachdem Sie gelesen haben und die Ergebnisse sind 0x01, dann rufen Sie eine andere Funktion, sonst rufen Sie diese Funktion mit Rekursion.

So etwas wie dieses (nicht 100% sicher über das Kompilieren - nicht auf dem rechten Seite jetzt mac):

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

Vielen Dank es funktioniert !!! –