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];
发送时调用委托方法。
- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError {
NSLog(@"didWriteValue characteristic.value: %@ ", iCharacteristic.value);
NSLog(@"Error = %@", iError);
}
但是,在这里我得到空值。
我也可以在外围应用程序中接收,
- (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);
}
接收结果值相同为空。
- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError
将数据发送到外围设备时无法识别问题。任何答案都会对我有很大帮助