func NewBatteryService() *gatt.Service { lv := byte(100) s := gatt.NewService(gatt.UUID16(0x180F)) c := s.AddCharacteristic(gatt.UUID16(0x2A19)) c.HandleReadFunc( func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) { rsp.Write([]byte{lv}) lv-- }) // FIXME: this cause connection interrupted on Mac. // Characteristic User Description // c.AddDescriptor(gatt.UUID16(0x2901)).SetValue([]byte("Battery level between 0 and 100 percent")) // Characteristic Presentation Format c.AddDescriptor(gatt.UUID16(0x2904)).SetValue([]byte{4, 1, 39, 173, 1, 0, 0}) return s }
func main() { g, err := gonashi.NewGonashi() if err != nil { return } g.Scan() var discovered map[string]*gonashi.Konashi ticker := time.NewTicker(time.Second * 20) select { case discovered = <-g.Discovered(): break case <-ticker.C: log.Println("time out") g.StopScanning() return } g.StopScanning() log.Println(gatt.UUID16(0xFF00).String()) log.Println(discovered) wg := new(sync.WaitGroup) for idStr, konashi := range discovered { log.Println(idStr) konashi.Connect() wg.Add(1) go func() { <-konashi.Connected log.Println("Connected") defer func() { konashi.DisConnect() <-konashi.Disconnected log.Println("Disconnected") wg.Done() }() for _, c := range konashi.DiscoverCharacteristics() { log.Println(c) } }() } wg.Wait() }
package service import "github.com/flemay/gatt" var ( attrGAPUUID = gatt.UUID16(0x1800) attrDeviceNameUUID = gatt.UUID16(0x2A00) attrAppearanceUUID = gatt.UUID16(0x2A01) attrPeripheralPrivacyUUID = gatt.UUID16(0x2A02) attrReconnectionAddrUUID = gatt.UUID16(0x2A03) attrPeferredParamsUUID = gatt.UUID16(0x2A04) ) // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml var gapCharAppearanceGenericComputer = []byte{0x00, 0x80} // NOTE: OS X provides GAP and GATT services, and they can't be customized. // For Linux/Embedded, however, this is something we want to fully control. func NewGapService(name string) *gatt.Service { s := gatt.NewService(attrGAPUUID) s.AddCharacteristic(attrDeviceNameUUID).SetValue([]byte(name)) s.AddCharacteristic(attrAppearanceUUID).SetValue(gapCharAppearanceGenericComputer) s.AddCharacteristic(attrPeripheralPrivacyUUID).SetValue([]byte{0x00}) s.AddCharacteristic(attrReconnectionAddrUUID).SetValue([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) s.AddCharacteristic(attrPeferredParamsUUID).SetValue([]byte{0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0xd0, 0x07}) return s }
package service import ( "log" "github.com/flemay/gatt" ) var ( attrGATTUUID = gatt.UUID16(0x1801) attrServiceChangedUUID = gatt.UUID16(0x2A05) ) // NOTE: OS X provides GAP and GATT services, and they can't be customized. // For Linux/Embedded, however, this is something we want to fully control. func NewGattService() *gatt.Service { s := gatt.NewService(attrGATTUUID) s.AddCharacteristic(attrServiceChangedUUID).HandleNotifyFunc( func(r gatt.Request, n gatt.Notifier) { go func() { log.Printf("TODO: indicate client when the services are changed") }() }) return s }