0
 I am sending the date from to the peripheral by using the below code.

NSString* alternative = [NSString stringWithFormat:@"hello"];
NSData *dataToSend = [alternative dataUsingEncoding:NSUTF8StringEncoding];
[self.discoveredPeripheral writeValue:dataToSend forCharacteristic:self.discoveredCharacteristics type:CBCharacteristicWriteWithResponse];

While sending called the delegate method.

- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError {

    NSLog(@"didWriteValue characteristic.value: %@ ", iCharacteristic.value);

    NSLog(@"Error = %@", iError);

}

But, here I am getting null value.

I can receive in peripheral app also in,

- (void)peripheralManager:(CBPeripheralManager *)iPeripheral didReceiveWriteRequests:(NSArray *)iRequests {


    [iPeripheral respondToRequest:[iRequests objectAtIndex:0] withResult:CBATTErrorSuccess];
    CBATTRequest *aRequest = iRequests[0];
    NSData *aData = aRequest.value;
    NSDictionary *aResponse = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:nil];

    NSLog(@"Received Data = %@", aResponse);
}

receiving result value is same null.

- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError

The couldn't recognize the issue while sending the data to peripheral. Any answer will help me a lot

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • What's the value of `[iRequest count]`? If more than 1, what gives `[iRequests valueForKey:@"value"]`. You seem (at least in the code you gave) that you do read only the first request. I also don't understand, in one hand, you send string to data, and you try to read it through JSON. I hope that you tried to simplify your example, and forgot a few things. Is `aData` really nil? – Larme Dec 03 '15 at 12:40
  • didWriteValueForCharacteristic it self the data is nill. But, when sending " dataToSend" variable have the data. – Vineesh TP Dec 04 '15 at 02:42
  • `NSData *aData = aRequest.value`: What's the value of `aData`? – Larme Dec 04 '15 at 08:01
  • @Larme: thanks , Now it is working. – Vineesh TP Dec 04 '15 at 08:55
  • And what was your issue? The `NSJSONSerialization`? – Larme Dec 04 '15 at 09:14
  • @Larme: yes, that was the issue. thanks a lot. I didn't noticed the same. – Vineesh TP Dec 04 '15 at 10:22

0 Answers0