问题
I set notify to a characteristic, and then write some data(It's the address to be read and read data length) to another characteristic. Delegate didUpdateValueForCharacteristic will be called, but the characteristic.value is zero (should have some value) with the correct length. I also check the delegate didWriteValueForCharacteristic. This will be called after I write the data, but in the delegate, characteristic.value is null while the characteristic.UUID is the same as I wrote.
So when didWriteValueForCharacteristic has been called without error, does it means the write operation is successful? If so, why is characteristic value null?(I think the value should equal to what i wrote in writeValue: forCharacteristic:type:)
回答1:
I've been running into similar issues that you have been experiencing here so I will try to share what I've seen.
didWriteValueForCharacteristic isn't what you want when you are trying to get data from a peripheral. Although you say you are trying to write data somewhere, I'm unsure what that has to do with getting an update notification.
If you are using the notify setting, then you should focus on didUpdateValueForCharacteristic.
When didUpdateValueForCharacteristic is called it doesn't necessarily mean that a value has been completely conveyed. You have to keep in mind that the data being sent can only be sent in small amounts (20 bytes, if I remember correctly), so there could be a case where the data that you are looking at is incomplete.
Another thing that I have noticed is that on the peripheral side when you call
[peripheralManager updateValue:dataToSent forCharacteristic:aCustomCharacteristic onSubscribedCentrals:nil];
it returns a BOOL which tells you whether or not the data was successfully sent. If it is not successfully sent then it stops trying to send subsequent ones, which can be a pain.
When I was trying to send an int value as data from a peripheral to a client, I was using an iPad and an iPhone. I had issues when the iPhone was the client and the iPad was the peripheral. I got the null values much like what you were dealing with, but when i switched the two, I got the proper values I was supposed to get. It could also be an issue with your devices that you are using, as I am not completely sure you can use an iPad a peripheral.
来源:https://stackoverflow.com/questions/14495120/corebluetooth-delegate-didwritevalueforcharacteristic-called-with-a-value-null