Skip to content

Instantly share code, notes, and snippets.

@anisbhsl
Created May 30, 2020 16:00
Show Gist options
  • Save anisbhsl/f610cf9f58ba4da46c281ecb1e921c8b to your computer and use it in GitHub Desktop.
Save anisbhsl/f610cf9f58ba4da46c281ecb1e921c8b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/bettercap/gatt"
"github.com/bettercap/gatt/examples/option"
"github.com/bettercap/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) {
switch s {
case gatt.StatePoweredOn:
d.AddService(service.NewGapService("Techkai"))
d.AddService(service.NewGattService())
s1 := service.NewBatteryService()
d.AddService(s1)
// Advertise device name and service's UUIDs.
d.AdvertiseNameAndServices("Techkai", []gatt.UUID{s1.UUID()})
// Advertise as an OpenBeacon iBeacon
d.AdvertiseIBeacon(gatt.MustParseUUID("CE061800-43E5-11E4-916C-0800200C9A66"), 1, 2, -59)
default:
}
}
d.Init(onStateChanged)
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment