Skip to content

Commit dadb65c

Browse files
committed
feat(gap): add ConnectWithContext in gap_darwin.go
Related #339
1 parent 5c61529 commit dadb65c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

gap_darwin.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bluetooth
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"time"
@@ -104,6 +105,11 @@ type deviceInternal struct {
104105

105106
// Connect starts a connection attempt to the given peripheral device address.
106107
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
108+
return a.ConnectWithContext(context.Background(), address, params)
109+
}
110+
111+
// ConnectWithContext starts a connection attempt to the given peripheral device address.
112+
func (a *Adapter) ConnectWithContext(ctx context.Context, address Address, params ConnectionParams) (Device, error) {
107113
uuid, err := cbgo.ParseUUID(address.UUID.String())
108114
if err != nil {
109115
return Device{}, err
@@ -162,6 +168,16 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
162168
// record an error to use when the disconnect comes through later.
163169
connectionError = errors.New("timeout on Connect")
164170

171+
// we are not ready to return yet, we need to wait for the disconnect event to come through
172+
// so continue on from this case and wait for something to show up on prphCh
173+
continue
174+
case <-ctx.Done():
175+
// we need to cancel the connection if the context is done
176+
a.cm.CancelConnect(prphs[0])
177+
178+
// record an error to use when the disconnect comes through later.
179+
connectionError = ctx.Err()
180+
165181
// we are not ready to return yet, we need to wait for the disconnect event to come through
166182
// so continue on from this case and wait for something to show up on prphCh
167183
continue

0 commit comments

Comments
 (0)