I have two applications, one as a peripheral (on iPad 4th Gen running iOS 7) and one as central (iPhone 5c running iOS7).
I wish to have a writable characteristic within the peripheral device that the central writes a handful of bytes to (approx 4 bytes but could vary from execution to execution).
I am configuring the peripheral as follows:
-(void)startPeripheral
{
CBMutableService *service = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:kMyServiceUUID] primary:YES];
CBMutableCharacteristic *ch = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:kMyWriteUUID] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];
service.characteristics = @[ch];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@[[CBUUID UUIDWithString:kMyServiceUUID]], CBAdvertisementDataServiceUUIDsKey, @"My Peripheral", CBAdvertisementDataLocalNameKey, nil];
[self.peripheralManager addService:service];
[self.peripheralManager startAdvertising:dict];
NSLog(@"Service = %@",service);
}
This all appears to work at first, the peripheral advertises the service and my central can scan, find, discovers service and characteristics and find the characteristic it's interested in. The problem comes when I try to write to the characteristic.
On the central I have the following method:
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
NSLog(@"Service/Characteristics = %@",service.characteristics);
for(CBCharacteristic *ch in service.characteristics)
{
NSLog(@"Characteristic properties = %x",ch.properties);
NSLog(@"Characterisctic = %@",ch.UUID);
if([ch.UUID isEqual:[CBUUID UUIDWithString:kMyWriteUUID]])
{
// We've found our write characteristic
NSLog(@"Writing data to characteristic %@",ch.UUID);
NSData *data = [NSData dataWithBytes:(Byte[]){1,2,3,4} length:4];
[peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
}
}
}
I get a callback to didWriteValueForCharacteristic which indicates the error:
Error Domain=CBATTErrorDomain Code=14 "Unlikely error."
Which is not very helpful.
Can anyone spot and issues or suggest any solutions?
Thanks, Rich