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