Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions gattc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
errCannotSendWriteWithoutResponse = errors.New("bluetooth: cannot send write without response (buffer full)")
errWriteWithoutResponseTimeout = errors.New("bluetooth: write without response timed out waiting for buffer space")
)

// DiscoverServices starts a service discovery procedure. Pass a list of service
Expand Down Expand Up @@ -236,13 +236,21 @@ func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {
// WriteWithoutResponse replaces the characteristic value with a new value. The
// call will return before all data has been written. A limited number of such
// writes can be in flight at any given time.
// If the client is not ready to send write without response requests at this time (e.g. because the internal buffer is full), an error is returned.
// If the peripheral's buffer is full, this method polls
// CanSendWriteWithoutResponse every 15ms (one BLE connection interval) until
// ready, with a 10-second timeout.
func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (int, error) {
if !c.service.device.prph.CanSendWriteWithoutResponse() {
return 0, errCannotSendWriteWithoutResponse
dev := c.service.device

deadline := time.Now().Add(10 * time.Second)
for !dev.prph.CanSendWriteWithoutResponse() {
if time.Now().After(deadline) {
return 0, errWriteWithoutResponseTimeout
}
time.Sleep(15 * time.Millisecond)
}

c.service.device.prph.WriteCharacteristic(p, c.characteristic, false)
dev.prph.WriteCharacteristic(p, c.characteristic, false)

return len(p), nil
}
Expand Down
Loading