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);
}
接收结果值为null。
- (void)peripheral:(CBPeripheral *)iPeripheral didWriteValueForCharacteristic:(CBCharacteristic *)iCharacteristic error:(NSError *)iError
在向外围设备发送数据时,无法识别问题。 任何答案都会对我有所帮助