5
votes

The Bluetooth Core Spec V4.0 Vol. 3 Part G Section 4.9.3 states, that for writes of characteristic values with response, the Attribute Protocol Write Request procedure is used.

Bluetooth Core Spec V4.0 Vol 3 Part F Section 3.3.2 describes, that

Once a client sends a request to a server, that client shall send no other request to the same server until a response PDU has been received.

I want to write multiple values with response in an iOS app using CoreBluetooth. Do I have to manage this specification myself? Or can I simply use - writeValue:forCharacteristic:type to write all of the values at once, and iOS manages that each request is only sent after the preceding one has been processed?

I somehow guess that iOS will manage it, because according to Bluetooth Core Spec V4.0 Vol 3 Part F Section 3.4.5.2 the Write Response does not contain a link to the written characteristic. However, the - peripheral:didWriteValueForCharacteristic:error method suggests that iOS somehow keeps track of what characteristic the response is linked to.

Can someone confirm or deny this?

2

2 Answers

2
votes

I don't think you need to worry about the ATT/GATT so much when dealing with CB. The reason is many people who are using CB does not have access to the Bluetooth Core 4.0 spec, neither are they expected to read them.

The reason that CB knows how to associate the characteristic is that the protocol dictates that each command and response are paired. When you send a command, you will receive a response.

So you can use writeValue multiple times, and CB will properly queue the calls for you, i.e. it will wait for the response at the ATT layer before the next write. And the delegate callbacks are guaranteed to be in the same order the writes are executed.

1
votes

I was able to write "multiple requests with response" i.e

[self peripheral] writeValue:valueToWrite forCharacteristic:dataPointCharacteristic type:CBCharacteristicWriteWithResponse];

together in one bunch - actually I sent 14 of them - and all of them came back with responses after a delay. But the - Write Responses did not contain data that was written to the characteristic - i.e - Only the value inside the characteristic was invalid in the response.

Seems to be close to the note "according to Bluetooth Core Spec V4.0 Vol 3 Part F Section 3.4.5.2 the Write Response does not contain a link to the written characteristic" with one difference that it is only the [characteristc value] that is incorrect - but ios took care of the sequencing internally. So it does not seem feasible to use the write responses (i.e the BLE acknowledgements) to to hook up sequencing logic to handle a sequence of steps to perform next.

So - the takeaway is - if you ask BLE to "writeWithReponse" the message "Do Task #1" to the peripheral, the BLE response from the peripheral is "OK!". The response does not tell you that the peripheral received the message "Do Task #1" but instead is something like - Yes, I got what you are saying. I'm lazy to repeat the exact command you sent me :)