| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/bettercap/gatt" |
| 9 | ) |
| 10 | |
| 11 | func NewCountService() *gatt.Service { |
| 12 | n := 0 |
| 13 | s := gatt.NewService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b")) |
| 14 | s.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc( |
| 15 | func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { |
| 16 | fmt.Fprintf(rsp, "count: %d", n) |
| 17 | n++ |
| 18 | }) |
| 19 | |
| 20 | s.AddCharacteristic(gatt.MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b")).HandleWriteFunc( |
| 21 | func(r gatt.Request, data []byte) (status byte) { |
| 22 | log.Println("Wrote:", string(data)) |
| 23 | return gatt.StatusSuccess |
| 24 | }) |
| 25 | |
| 26 | s.AddCharacteristic(gatt.MustParseUUID("1c927b50-c116-11e3-8a33-0800200c9a66")).HandleNotifyFunc( |
| 27 | func(r gatt.Request, n gatt.Notifier) { |
| 28 | cnt := 0 |
| 29 | for !n.Done() { |
| 30 | fmt.Fprintf(n, "Count: %d", cnt) |
| 31 | cnt++ |
| 32 | time.Sleep(time.Second) |
| 33 | } |
| 34 | }) |
| 35 | |
| 36 | return s |
| 37 | } |