Add whitelist to pair() and make case insensitive

This commit is contained in:
Elara 2021-10-26 21:42:41 -07:00
parent 651270051e
commit a19fedf763

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"strings"
"time"
bt "github.com/muka/go-bluetooth/api"
@ -70,7 +71,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(opts)
}
if err != nil {
return nil, err
@ -174,7 +175,7 @@ func connectByName(opts *Options) (*Device, error) {
func contains(ss []string, s string) bool {
for _, str := range ss {
if str == s {
if strings.EqualFold(str, s) {
return true
}
}
@ -182,7 +183,7 @@ func contains(ss []string, s string) bool {
}
// Pair attempts to discover and pair an InfiniTime device
func pair() (*Device, error) {
func pair(opts *Options) (*Device, error) {
// Create new device
out := &Device{}
// Start bluetooth discovery
@ -204,6 +205,9 @@ func pair() (*Device, error) {
}
// If device name is InfiniTime
if dev.Properties.Name == BTName {
if opts.WhitelistEnabled && !contains(opts.Whitelist, dev.Properties.Address) {
continue
}
// Set output device
out.device = dev
break