https://github.com/yasuoza/switchbot-ble-go
Unofficial SwitchBot client for Go.
https://github.com/yasuoza/switchbot-ble-go
ble go switchbot
Last synced: 11 months ago
JSON representation
Unofficial SwitchBot client for Go.
- Host: GitHub
- URL: https://github.com/yasuoza/switchbot-ble-go
- Owner: yasuoza
- License: mit
- Created: 2020-07-23T15:02:42.000Z (almost 6 years ago)
- Default Branch: v2
- Last Pushed: 2025-05-19T11:51:25.000Z (about 1 year ago)
- Last Synced: 2025-06-10T00:07:29.351Z (12 months ago)
- Topics: ble, go, switchbot
- Language: Go
- Homepage:
- Size: 193 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwitchBot Client for Go
  [](https://goreportcard.com/report/github.com/yasuoza/switchbot) [](https://coveralls.io/github/yasuoza/switchbot?branch=master) [](https://pkg.go.dev/github.com/yasuoza/switchbot-ble-go/v2)
Unofficial [SwitchBot](https://www.switch-bot.com/) client for Go.
## Commandline
```
$ go install github.com/yasuoza/switchbot-ble-go/v2/cmd/switchbot@latest
```
```
Usage: switchbot [--version] [--help] []
Available commands are:
info Show current SwitchBot information
press Trigger press command
scan Search for SwitchBots
```
Scan SwitchBots.
```
$ switchbot scan
11:11:11:11:11:11
```
Press.
```
switchbot press -max-retry '11:11:11:11:11:11'
```
## API Example
```go
import (
"github.com/yasuoza/switchbot-ble-go/v2/pkg/switchbot"
)
func main() {
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 := addrs[0]
log.Printf("Connecting to SwitchBot %s\n", addr)
bot, err := switchbot.Connect(ctx, addr, timeout)
if err != nil {
log.Fatal(err)
}
// Trigger Press.
log.Printf("Connected to SwitchBot %s. Trigger Press\n", addr)
bot.Press(false)
}
```