我在使用 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);
}];}
先感谢您!