| 1 |
|
| 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 |
|
| 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 |
|
| 27 |
onStateChanged := func(d gatt.Device, s gatt.State) { |
| 28 |
fmt.Printf("State: %s\n", s) |
| 29 |
switch s { |
| 30 |
case gatt.StatePoweredOn: |
| 31 |
|
| 32 |
|
| 33 |
d.AddService(service.NewGapService("Gopher")) |
| 34 |
d.AddService(service.NewGattService()) |
| 35 |
|
| 36 |
|
| 37 |
s1 := service.NewCountService() |
| 38 |
d.AddService(s1) |
| 39 |
|
| 40 |
|
| 41 |
s2 := service.NewBatteryService() |
| 42 |
d.AddService(s2) |
| 43 |
|
| 44 |
|
| 45 |
d.AdvertiseNameAndServices("Gopher", []gatt.UUID{s1.UUID(), s2.UUID()}) |
| 46 |
|
| 47 |
|
| 48 |
d.AdvertiseIBeacon(gatt.MustParseUUID("AA6062F098CA42118EC4193EB73CCEB6"), 1, 2, -59) |
| 49 |
|
| 50 |
default: |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
d.Init(onStateChanged) |
| 55 |
select {} |
| 56 |
} |