Remove pair timeout entirely

This commit is contained in:
Elara 2021-08-22 13:12:16 -07:00
parent 47293f04bc
commit 9553844896

View File

@ -41,12 +41,10 @@ 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
@ -64,7 +62,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(opts.PairTimeout) dev, err = pair()
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -164,7 +162,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(timeout time.Duration) (*Device, error) { func pair() (*Device, error) {
// Create new device // Create new device
out := &Device{} out := &Device{}
// Start bluetooth discovery // Start bluetooth discovery
@ -173,28 +171,22 @@ func pair(timeout time.Duration) (*Device, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
discoveryLoop: // For every discovery event
for { for event := range discovery {
select { // If device removed, skip event
case event := <-discovery: if event.Type == adapter.DeviceRemoved {
// If device removed, skip event continue
if event.Type == adapter.DeviceRemoved { }
continue // Create new device with discovered path
} dev, err := device.NewDevice1(event.Path)
// Create new device with discovered path if err != nil {
dev, err := device.NewDevice1(event.Path) return nil, err
if err != nil { }
return nil, err // If device name is InfiniTime
} if dev.Properties.Name == BTName {
// If device name is InfiniTime // Set output device
if dev.Properties.Name == BTName { out.device = dev
// Set output device break
out.device = dev
// Break out of discoveryLoop
break discoveryLoop
}
case <-time.After(timeout):
break discoveryLoop
} }
} }