1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
// +build
package main
import (
"fmt"
"log"
"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
"github.com/paypal/gatt/examples/service"
)
func main() {
d, err := gatt.NewDevice(option.DefaultServerOptions...)
if err != nil {
log.Fatalf("Failed to open device, err: %s", err)
}
// Register optional handlers.
d.Handle(
gatt.CentralConnected(func(c gatt.Central) { fmt.Println("Connect: ", c.ID()) }),
gatt.CentralDisconnected(func(c gatt.Central) { fmt.Println("Disconnect: ", c.ID()) }),
)
// A mandatory handler for monitoring device state.
onStateChanged := func(d gatt.Device, s gatt.State) {
fmt.Printf("State: %s\n", s)
switch s {
case gatt.StatePoweredOn:
// Setup GAP and GATT services for Linux implementation.
// OS X doesn't export the access of these services.
d.AddService(service.NewGapService("Gopher")) // no effect on OS X
d.AddService(service.NewGattService()) // no effect on OS X
// A simple count service for demo.
s1 := service.NewCountService()
d.AddService(s1)
// A fake battery service for demo.
s2 := service.NewBatteryService()
d.AddService(s2)
// Advertise device name and service's UUIDs.
d.AdvertiseNameAndServices("Gopher", []gatt.UUID{s1.UUID(), s2.UUID()})
// Advertise as an OpenBeacon iBeacon
d.AdvertiseIBeacon(gatt.MustParseUUID("AA6062F098CA42118EC4193EB73CCEB6"), 1, 2, -59)
default:
}
}
d.Init(onStateChanged)
select {}
}
|