Definition
A write request and a write command are the two basic messages with which a client of ATT §7.9 (Attribute Protocol), the BLE §7.1 (Bluetooth Low Energy) protocol for reading and writing small data items called attributes, asks a server to replace an attribute’s value. Both carry the attribute’s handle, a 2-byte number that identifies it, and the new value. A write request gets a Write Response on success or an Error Response on failure. A write command gets no answer: a server that cannot carry it out ignores it.
The idea
A phone app sets the target temperature of a thermostat to 21 degrees. The app needs to know whether the thermostat accepted the value: the attribute may refuse writes over an unencrypted link, or the value may have the wrong length. A write request returns that answer. Later the same app sends a 2,000-byte heating schedule in pieces of 20 bytes. Waiting for an answer after each of the 100 pieces would make the transfer slow, so the app sends write commands one after another and checks the result once at the end.
The answer to a write request is narrow. “Success” means that the server’s software accepted the value and reported it as written. It does not mean that a power failure can no longer erase the value, that the thermostat has started heating, or that the write happened exactly once. And if the connection breaks before the answer arrives, the client cannot tell whether the write happened at all.
How it works
Write request. The client sends a message of three parts: the 1-byte opcode
0x12, a code that names the operation; the 2-byte handle; and the value. Let be the ATT MTU §7.14 (maximum transmission unit), the largest ATT message the two devices agreed on; the value is at most bytes long. Requests are sequential: the client may not send another request on the same ATT bearer, the channel that carries ATT messages, until the answer to this one has arrived.Checks at the server. The server checks that the handle exists, that the attribute may be written, that the link meets the attribute’s security requirements (for example, that the link is encrypted), and that the value has an allowed length. The server’s application code then receives the value and decides whether to accept it.
Answer. On success the server sends a Write Response, a 1-byte message that holds only the opcode
0x13. The specification says that this response “acknowledges that the attribute has been successfully written” and is sent after the value is written; what “written” means is left to the server’s software. On failure the server sends a 5-byte Error Response: the opcode0x01, the opcode of the failed request, the handle, and a 1-byte error code, for example Write Not Permitted (0x03), Insufficient Encryption (0x0F), or a code from0x80to0x9Fdefined by the application’s profile, the specification of its attributes.Timeout §9.34. If no answer arrives within 30 seconds, the request has failed, and the client may send no further ATT message on that bearer; for the usual ATT channel, only a new connection provides a new bearer.
Write command. The client sends the opcode
0x52, which is0x12with the command bit0x40added, then the handle and the value. The server performs the write if it can. If it cannot, for any reason, it ignores the command and sends neither a response nor an error. ATT does not pace commands: the client may send one at any time, even while a request waits for its answer, and a server whose buffers are full discards the command.
GATT §7.10 (Generic Attribute Profile), the layer above ATT, marks each characteristic, a named value that the server exposes, with the writes it allows: the property Write allows write requests, and Write Without Response allows write commands. A value longer than bytes needs queued writes: the client sends the pieces in Prepare Write Requests, which the server holds in a queue, and then an Execute Write Request, which tells the server to write all of them.
Both kinds of write travel over the Link Layer §7.6, the radio protocol layer below ATT, which acknowledges every radio packet and resends lost ones while the connection lasts. A write command is therefore no more likely than a write request to be lost to a radio error. The two differ in what the client learns and in how fast the client may send.
Worked example
Pacing. A client sends a 2,000-byte schedule over a connection with a connection interval §7.7 of 30 ms and an ATT MTU of 23 bytes. Each write carries at most bytes, so the schedule needs writes. Suppose a write request and its response take two connection intervals, ms; then 100 requests take ms, which is 6 seconds. Suppose instead that the client’s protocol software sends 4 write commands in each connection event; then 100 commands take connection events, which is ms. The commands are times faster, but the client learns nothing about any single write, so it checks the outcome at the end, for example by reading back a checksum §4.32 of the whole schedule.
A lost response and a retry. A control attribute on a server adds one to a counter for each write. The counter holds 5. The client sends a write request; the server increments the counter to 6 and sends the Write Response; the connection breaks before the response arrives. If the client reconnects and sends the same write again, the counter becomes 7: one intended increment has been applied twice. The repair is an application protocol, not an ATT feature. The client puts an operation number, say 41, in the value, and the server stores the number of the last operation it applied in the same record as the counter. When operation 41 arrives a second time, the server recognizes the number, does not increment again, and sends a Write Response. The counter stays at 6.
Success before storage. A server’s application code copies a new setting into RAM, schedules a write to flash memory §6.6 20 ms later, and reports success. The Write Response leaves at 0 ms, and power fails at 10 ms. The client holds a success answer for a setting that the server no longer has.
Stated precisely
For one write request, exactly one of three outcomes reaches the client:
a Write Response: the server’s software reported the attribute as written;
an Error Response: the server refused the write, and the error code gives the reason;
nothing, because the connection ended or 30 seconds passed: the client does not know whether the write happened.
A write command produces none of these; the client learns nothing about it from ATT.
A Write Response implies a durable commit only if the server’s software sends success after the value, and every effect of it that must survive, has been written to non-volatile memory in a way that survives a power failure at any moment. That is a property of the server’s software, not a requirement of ATT. Sending a write again after outcome 3 is safe only if the write is idempotent §8.31, meaning that applying it twice has the same effect as applying it once (“set the target to 21 degrees”), or if the server recognizes repeats.
Limits and common mistakes
A Write Response is not durable storage, not activation of the new value, and not proof that the write happened exactly once. Each needs its own design; the Durability §8.2 and Exactly-once effect §8.32 entries treat two of them.
Write commands fail silently. A write command to an attribute that requires encryption, sent over an unencrypted link, is ignored, and the client cannot tell this from success.
Write commands can flood a server. ATT does not pace them, so the server must bound the memory and work that incoming commands can take, and must check every command as strictly as a request.
Remembered operation numbers must last as long as a retry is possible. If the server forgets them at a restart while the counter survives, a retry after the restart is applied twice.
Neither kind of write shows who sent it. That comes from encrypting the link with keys set up by pairing §7.17, or from a message authentication code §5.7 computed over the value.
Questions answered
Does a successful write response imply durable application commit?
No. In ATT (Attribute Protocol), the BLE (Bluetooth Low Energy) protocol for reading and writing attributes, a Write Response states that the server’s software accepted the value and reported the attribute as written. The specification does not require the value to be in non-volatile memory at that moment, and it says nothing about effects the value triggers. A server may copy the value into RAM and store or act on it later; a power failure in between loses the value although the client saw success.
A Write Response implies a durable commit only if the server’s software is built to send success after the value is committed to non-volatile memory in a way that survives power loss, and says so. Even then, a response lost on the way leaves the client unsure whether the write happened, so sending it again is safe only for an idempotent write or when the server recognizes repeated operations.
Related entries: Retransmission and duplicate delivery (§7.36), Idempotency (§8.31), Exactly-once effect (§8.32), Finite-state machine (§8.33), ATT (§7.9), Notification versus indication (§7.12), Durability (§8.2). Further reading: [91].