objective-c 中的块和 do-while 循环

Blocks and do-while loop in objective-c

我在使用 Objective-C 中的块时遇到问题。我的问题是,从未使用 do-while 循环调用函数 readDataFromCharactUUID 的完成块。在不使用 do-while-loop 的情况下,它被调用一次。

我想用我的代码做的是经常从 BLE 特性中读取一个值,因为该值是 0x01。

我的问题:为什么完成块从未执行过?我能做什么,完成块在我的案例中被执行?

使用代码:

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

        }];}

提前致谢!

那个执行块是异步的——这意味着它将在不同的线程上执行。我会创建从外设读取的函数,在你读取结果为 0x01 后调用另一个函数,否则用递归调用该函数。

类似这样的事情(不是 100% 确定编译 - 现在 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;
             }
         }
     ];

}