0

我在使用 Objective-C 中的块时遇到问题。我的问题是,函数 readDataFromCharactUUID 的完成块永远不会使用 do-while-loop 调用。在不使用 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);

        }];}

先感谢您!

4

1 回答 1

0

该执行块是异步的 - 这意味着它将在不同的线程上执行。我将创建用于从外围设备读取的函数,在您读取结果为 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;
             }
         }
     ];

}
于 2016-12-15T08:48:00.637 回答