Allow configuration of pair timeout

This commit is contained in:
Elara 2021-08-21 20:24:56 -07:00
parent ea488067fb
commit 7f19dfb354
1 changed files with 5 additions and 4 deletions

View File

@ -39,13 +39,14 @@ type Device struct {
var ErrNoDevices = errors.New("no InfiniTime devices found") var ErrNoDevices = errors.New("no InfiniTime devices found")
var ErrNotFound = errors.New("could not find any advertising InfiniTime devices") var ErrNotFound = errors.New("could not find any advertising InfiniTime devices")
type Options struct { type Options struct {
AttemptReconnect bool AttemptReconnect bool
PairTimeout time.Duration
} }
var DefaultOptions = &Options{ var DefaultOptions = &Options{
AttemptReconnect: true, AttemptReconnect: true,
PairTimeout: time.Minute,
} }
// Connect will attempt to connect to a // Connect will attempt to connect to a
@ -63,7 +64,7 @@ func Connect(opts *Options) (*Device, error) {
// If such device does not exist // If such device does not exist
if errors.Is(err, ErrNoDevices) { if errors.Is(err, ErrNoDevices) {
// Attempt to pair device // Attempt to pair device
dev, err = pair() dev, err = pair(dev.opts.PairTimeout)
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -160,7 +161,7 @@ func connectByName() (*Device, error) {
} }
// Pair attempts to discover and pair an InfiniTime device // Pair attempts to discover and pair an InfiniTime device
func pair() (*Device, error) { func pair(timeout time.Duration) (*Device, error) {
// Create new device // Create new device
out := &Device{} out := &Device{}
// Start bluetooth discovery // Start bluetooth discovery
@ -190,7 +191,7 @@ discoveryLoop:
// Break out of discoveryLoop // Break out of discoveryLoop
break discoveryLoop break discoveryLoop
} }
case <-time.After(5 * time.Second): case <-time.After(timeout):
break discoveryLoop break discoveryLoop
} }
} }