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