This commit is contained in:
Jan Graichen 2018-11-26 21:03:12 +01:00
commit c1c348db75
3 changed files with 115 additions and 0 deletions

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module miflorad
require (
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae // indirect
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1 // indirect
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1
github.com/pkg/errors v0.8.0 // indirect
github.com/sirupsen/logrus v1.2.0
)

25
go.sum Normal file
View File

@ -0,0 +1,25 @@
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 h1:3jFq2xL4ZajGK4aZY8jz+DAF0FHjI51BXjjSwCzS1Dk=
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae h1:NTs1uIj/Ru/QlpKwd9C9dnv/5zblvCXH7Dbn2oi3p98=
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1 h1:DrMDOYCxMG7TUMjhBWnRsgPfpPlz0sFL11ukl+2+CZU=
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1/go.mod h1:1lEPQDLh8Z86+REeQMqID8UpXCSdHF0fvU0qZj3b1eA=
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1 h1:VKhCDuxZ3+Bg+Plf7+CxgGRlNEdwHDKUqHoZLzer8Ms=
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1/go.mod h1:brKFFAJeW2mWp1W5D/GJhwbn3IpNR69jIm1qGWZcl50=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

79
main.go Normal file
View File

@ -0,0 +1,79 @@
//shows how to watch for new devices and list them
package main
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/emitter"
"github.com/muka/go-bluetooth/linux"
)
const logLevel = log.DebugLevel
const adapterID = "hci0"
func main() {
log.SetLevel(logLevel)
//clean up connection on exit
defer api.Exit()
log.Debugf("Reset bluetooth device")
a := linux.NewBtMgmt(adapterID)
err := a.Reset()
if err != nil {
log.Error(err)
os.Exit(1)
}
devices, err := api.GetDevices()
if err != nil {
log.Error(err)
os.Exit(1)
}
log.Infof("Cached devices:")
for _, dev := range devices {
showDeviceInfo(&dev)
}
log.Infof("Discovered devices:")
err = discoverDevices(adapterID)
if err != nil {
log.Error(err)
os.Exit(1)
}
select {}
}
func discoverDevices(adapterID string) error {
err := api.StartDiscovery()
if err != nil {
return err
}
log.Debugf("Started discovery")
err = api.On("discovery", emitter.NewCallback(func(ev emitter.Event) {
discoveryEvent := ev.GetData().(api.DiscoveredDeviceEvent)
dev := discoveryEvent.Device
showDeviceInfo(dev)
}))
return err
}
func showDeviceInfo(dev *api.Device) {
if dev == nil {
return
}
props, err := dev.GetProperties()
if err != nil {
log.Errorf("%s: Failed to get properties: %s", dev.Path, err.Error())
return
}
log.Infof("name=%s addr=%s rssi=%d", props.Name, props.Address, props.RSSI)
}