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