diff --git a/gap_darwin.go b/gap_darwin.go index f0d5d11..b9f5dee 100644 --- a/gap_darwin.go +++ b/gap_darwin.go @@ -211,3 +211,20 @@ func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripher } } + +// DidWriteValueForCharacteristic is called after the characteristic for a Service +// for a Peripheral trigger a write with response. It contains the returned error or nil. +func (pd *peripheralDelegate) DidWriteValueForCharacteristic(_ cbgo.Peripheral, chr cbgo.Characteristic, err error) { + uuid, _ := ParseUUID(chr.UUID().String()) + svcuuid, _ := ParseUUID(chr.Service().UUID().String()) + + if svc, ok := pd.d.services[svcuuid]; ok { + for _, char := range svc.characteristics { + if char.characteristic == chr && uuid == char.UUID() { // compare pointers + if char.writeChan != nil { + char.writeChan <- err + } + } + } + } +} diff --git a/gattc_darwin.go b/gattc_darwin.go index 5bfc6f1..c4abf72 100644 --- a/gattc_darwin.go +++ b/gattc_darwin.go @@ -168,6 +168,7 @@ type deviceCharacteristic struct { characteristic cbgo.Characteristic callback func(buf []byte) readChan chan error + writeChan chan error } // UUID returns the UUID for this DeviceCharacteristic. @@ -175,6 +176,27 @@ func (c DeviceCharacteristic) UUID() UUID { return c.uuidWrapper } +// Write replaces the characteristic value with a new value. The +// call will return after all data has been written. +func (c DeviceCharacteristic) Write(p []byte) (n int, err error) { + c.writeChan = make(chan error) + c.service.device.prph.WriteCharacteristic(p, c.characteristic, true) + + // wait for result + select { + case <-time.NewTimer(10 * time.Second).C: + err = errors.New("timeout on Write()") + case err = <-c.writeChan: + } + + c.writeChan = nil + if err != nil { + return 0, err + } + + return len(p), nil +} + // 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. This call is also known as a