在IOS上通过BLE接收数据,数据总是很旧

时间:2016-10-19 13:10:33

标签: ios objective-c bluetooth-lowenergy xcode8

我是iOS编程的新手,我正在尝试从BLE设备接收数据。我能够连接到设备并将数据发送到设备。问题是从BLE设备接收数据。

我用它作为BLE Base: LGBluetooth

问题:

 @property (nonatomic,strong) NSString* recievedData; //data recieved from Peripheral

 - (void)RecieveDataBLE:(LGPeripheral *)peripheral :(NSString*)Service_UUID :(NSString*)Characteristic_UUID {
     //Function to read Data from BLE Device
    [LGUtils readDataFromCharactUUID:Characteristic_UUID serviceUUID:Service_UUID
            peripheral:peripheral
             completion:^(NSData *data, NSError *error) {
                 NSLog(@"Data : %s Error : %@", (char *)[data bytes], error);
                 recievedData = [NSString stringWithUTF8String:[data bytes]];
    }

- (IBAction)sendDOWN:(id)sender {
    MessageLabel.hidden = NO;
    [self RecieveDataBLE:mBuddy:SERVICE_UUID_DEVICE_INFORMATION:CHARACTERISTIC_UUID_MANUFACTURER];
    MessageLabel.text = [NSString stringWithFormat:@"%@",recievedData]; //Output is always old!!
 }

调用sendDown操作后,应通过ReceiveDataBle函数读取数据。然后这个将处理数据。问题是,ReceiveDataBle总是太晚了,并且没有立即收到信息,但是在一定时间之后。回调函数将数据返回给ReceiveDataBle。因此,当调用动作SendDown时,最后一次调用的数据将显示在MessageLabel !!

问题是,我如何确保我拥有最新的数据,而不是过时的数据?

1 个答案:

答案 0 :(得分:0)

**显示更新后的recievedData **

时可以使用此类型
-(IBAction)sendDOWN:(id)sender
{
MessageLabel.hidden = NO;
   [LGUtils readDataFromCharactUUID:Characteristic_UUID
                                             serviceUUID:Service_UUID
                                              peripheral:peripheral
                                              completion:^(NSData *data, NSError *error) {
                                                  NSLog(@"Data : %s Error : %@", (char *)[data bytes], error);
 recievedData = [NSString stringWithUTF8String:[data bytes]];                                                                                                  
MessageLabel.text = [NSString stringWithFormat:@"%@",recievedData];  
}


}