Debian Codegraph @

examples/server.go

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