0

I'm developing an app using BTLE iOS. I want to send data from central to peripheral without delay. As of now i can received the data coming from the peripheral, and also i send data from central to peripheral. I have four data to be send: 1. @"I:A:O\n"; 2. @"I:A:1\n"; 3. @"I:B:O\n"; 4. @"I:B:1\n"; sending "A" value has a delay in first send by 2-3 seconds. sending "B" value has a delay in second send 2-3 seconds. and then sometimes it is not working.

There is the code that receive and send data

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error)
    {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        return;
    }

    //used to identify what is inside the charateristics
    characteristicValue = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    NSLog(@" String Characteristic Value %@", characteristicValue);
   //characteristicData = [characteristicValue dataUsingEncoding:NSUTF8StringEncoding];

    if([characteristicValue length] != 1)
    {
        NSLog(@"Length Characteristic Value %lu", [characteristicValue length]);

        int indexf =    1;
        int indexs =    2;
        int indext =    3;
        int indexft =   4;
        //int indexfft =  5;

        firstCharacter = [NSString stringWithFormat:@"%c", [characteristicValue characterAtIndex:indexf-1]];
        secondCharacter = [NSString stringWithFormat:@"%c", [characteristicValue characterAtIndex:indexs-1]];
        thirdCharacter = [NSString stringWithFormat:@"%c", [characteristicValue characterAtIndex:indext-1]];
        fourthCharacter = [NSString stringWithFormat:@"%c", [characteristicValue characterAtIndex:indexft-1]];
        //fifthCharacter = [NSString stringWithFormat:@"%c", [characteristicValue characterAtIndex:indexfft-1]];
        NSLog(@"the ahcoha;fa %@", firstCharacter);
        NSLog(@"the ahcoha;fa %@", secondCharacter);
        NSLog(@"the ahcoha;fa %@", thirdCharacter);
        NSLog(@"the ahcoha;fa %@", fourthCharacter);
        NSLog(@"the ahcoha;fa %@", fifthCharacter);

        //for lock/unlock
        if ([thirdCharacter isEqualToString:@"0"])
        {
            UIImage *lockDoorImage =[UIImage imageNamed: @"btn_lock_mode.png"];
            [self.btnDoorImage setImage: lockDoorImage forState:UIControlStateNormal];
            NSLog(@"Lock daw");
        }
        else if ([thirdCharacter isEqualToString:@"1"])
        {
            UIImage *lockDoorImage =[UIImage imageNamed: @"btn_unlock_mode.png"];
            [self.btnDoorImage setImage: lockDoorImage forState:UIControlStateNormal];
            NSLog(@"unLock daw");
        }

        //for on/off alarm
        if ([fourthCharacter isEqualToString:@"0"])
        {
            UIImage *lockDoorImage =[UIImage imageNamed: @"btn_toggle_off.png"];
            [self.btnAlarmImage setImage: lockDoorImage forState:UIControlStateNormal];
            NSLog(@"alarm off daw");
        }
        else if ([fourthCharacter isEqualToString:@"1"])
        {
            UIImage *lockDoorImage =[UIImage imageNamed: @"btn_toggle_on.png"];
            [self.btnAlarmImage setImage: lockDoorImage forState:UIControlStateNormal];
            NSLog(@"alarm on daw");
        }

        //for active/inactive sensor
        if([secondCharacter isEqualToString:@"0"])
        {
            _LabelsensorStatus.text = @"Active";

        }
        else if ([secondCharacter isEqualToString:@"1"])
        {
            _LabelsensorStatus.text = @"Inactive";
        }

    }
    else
    {
        NSLog(@"alknbfjasd");
    }



    // Have we got everything we need?
     //We have, so show the data,

    if (characteristic.value != nil )
    {
        //value here.
        NSLog(@"chahahaha %@", characteristic.value);

        if([buttonClicked isEqualToString:@"open"])
        {
            doorValue = @"I:A:1\n";
            NSLog(@"You cliked Open Door button: %@", doorValue);

        }
        else if ([buttonClicked isEqualToString:@"close"])
        {
            doorValue = @"I:A:0\n";
            NSLog(@"You cliked Close Door button: %@", doorValue);
        }


        if([buttonClicked isEqualToString:@"on"])
        {
            doorValue = @"I:B:1\n";
            NSLog(@"You cliked alarm on button: %@", doorValue);
        }

        else if ([buttonClicked isEqualToString:@"off"])
        {
            doorValue = @"I:B:0\n";
            NSLog(@"You cliked alarm Off button: %@", doorValue);
        }


        if([buttonClicked isEqualToString:@"Active"])
        {
            doorValue = @"I:S:0\n";
        }
        else if ([buttonClicked isEqualToString:@"Inactive"])
        {
            doorValue = @"I:S:1\n";
        }
    }

//    //doorValue = @"I:A:1";
    NSLog(@"\nDoor Value used: %@", doorValue);

    payload = [doorValue dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"payload2 %@", payload);

    NSLog(@"Connected to peripheral %@", peripheral);

    //tempVariable


    if(buttonClicked != nil)
    {
        NSLog(@"ButtonClicked is not nil");
        if ((characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
        {
            NSLog(@"Writing is possible");
            [peripheral writeValue:payload forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
            payloadValue = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];
            newCharacteristicValue = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
            NSLog(@"newCharacteristicValueaa = %@", newCharacteristicValue);

            NSLog(@"charot  %@ ",  newCharacteristicValue);
            NSLog(@"payload  %@ ",  payloadValue);

            NSLog(@"newCharacteristicValue = %@", newCharacteristicValue);
        }

        else
        {
            NSLog(@"Writing is not possible");
        }
    }

    // Cancel our subscription to the characteristic+
    [peripheral setNotifyValue:YES forCharacteristic:characteristic]; //Jong (NO to YES)

    // and disconnect from the peripheral
   // [self.centralManager cancelPeripheralConnection:peripheral];

    // Otherwise, just add the data on to what we already have
    [_data appendData:payload];


    doorValue = nil;
}
  • Try dispatching your updates to the button images on the main queue. Updating from a background queue will cause delays. – Paulw11 Jun 22 '16 at 05:59
  • How? can you explain sir? –  Jun 22 '16 at 06:45
  • `dispatch_async(dispatch_get_main_queue(), ^{ [self.btnAlarmImage setImage: lockDoorImage forState:UIControlStateNormal]; });` – Paulw11 Jun 22 '16 at 06:51
  • i'm not familiar in this type of code sir ,anyway thank you. but can you please help me how can i apply that in my codeS? –  Jun 22 '16 at 07:06
  • Put that block of code where you are currently setting the button image. It dispatches the block onto the main queue. All UI updates must happen on the main queue – Paulw11 Jun 22 '16 at 07:07
  • if (button.selected) { [button setImage:[UIImage imageNamed:@"btn_unlock_mode.png"] forState:UIControlStateSelected]; dispatch_async(dispatch_get_main_queue(), ^{[button setImage:[UIImage imageNamed:@"btn_unlock_mode.png"] forState:UIControlStateSelected];}); –  Jun 22 '16 at 07:25
  • is this is correct? –  Jun 22 '16 at 07:25
  • `didUpdateValueForCharacteristic` will be called from a background thread, so when you call `[button setImage:[UIImage imageNamed:@"btn_unlock_mode.png"] forState:UIControlStateSelected];` it will be called from a background thread; this is not permitted and causes delays. You need to wrap all UI updates in ` dispatch_async(dispatch_get_main_queue()` as I showed in my comment, not just the button but other UI updates such as `_LabelsensorStatus.text` – Paulw11 Jun 22 '16 at 07:28
  • okay sir, thank you for this, i will try later. –  Jun 22 '16 at 07:42
  • sir, it is same as before. –  Jun 22 '16 at 07:56
  • sir, i just fixed the delay , button having a problem in sending I:B:0 and I:B:1 in peripheral –  Jun 24 '16 at 07:40
  • in sending I:A:0\n and I:A:1\n it works smoothly but sending I:B:0\n and I:B:1\n, stop on second send of data –  Jun 24 '16 at 07:42

0 Answers0