From c9e6868870cd5aaa343ccdc19a1b6ca48b1fe4c1 Mon Sep 17 00:00:00 2001 From: Yasuharu Ozaki Date: Fri, 12 Nov 2021 01:09:35 +0900 Subject: [PATCH] Update docs --- pkg/switchbot/doc_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/switchbot/doc_test.go b/pkg/switchbot/doc_test.go index 669c168..dc5a5f8 100644 --- a/pkg/switchbot/doc_test.go +++ b/pkg/switchbot/doc_test.go @@ -3,6 +3,7 @@ package switchbot_test import ( "context" "log" + "os" "time" "github.com/yasuoza/switchbot-ble-go/v2/pkg/switchbot" @@ -12,8 +13,23 @@ func Example_press() { ctx := context.Background() timeout := 5 * time.Second + // Scan SwitchBots. + var addrs []string + err := switchbot.Scan(ctx, timeout, func(addr string) { + addrs = append(addrs, addr) + }) + if err != nil { + log.Fatal(err) + } + + // If there is no SwitchBot, err is nil and length of addresses is 0. + if len(addrs) == 0 { + log.Println("SwitchBot not found") + os.Exit(0) + } + // First, connect to SwitchBot. - addr := "A0:00:0A:AA:00:00" + addr := addrs[0] log.Printf("Connecting to SwitchBot %s\n", addr) bot, err := switchbot.Connect(ctx, addr, timeout) if err != nil {