|
1 | 1 | package bluetooth |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "time" |
@@ -104,6 +105,11 @@ type deviceInternal struct { |
104 | 105 |
|
105 | 106 | // Connect starts a connection attempt to the given peripheral device address. |
106 | 107 | 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) { |
107 | 113 | uuid, err := cbgo.ParseUUID(address.UUID.String()) |
108 | 114 | if err != nil { |
109 | 115 | return Device{}, err |
@@ -162,6 +168,16 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err |
162 | 168 | // record an error to use when the disconnect comes through later. |
163 | 169 | connectionError = errors.New("timeout on Connect") |
164 | 170 |
|
| 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 | + |
165 | 181 | // we are not ready to return yet, we need to wait for the disconnect event to come through |
166 | 182 | // so continue on from this case and wait for something to show up on prphCh |
167 | 183 | continue |
|
0 commit comments