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) { fmt.Printf("State: %s\n", s) switch s { case gatt.StatePoweredOn: s1 := service.NewNanoPiBLEDemoService() d.AddService(s1) // Advertise device name and service's UUIDs. d.AdvertiseNameAndServices("NanoPi", []gatt.UUID{s1.UUID()}) default: } } d.Init(onStateChanged) select {} }
func main() { d, err := gatt.NewDevice(option.DefaultClientOptions...) if err != nil { log.Fatalf("Failed to open device, err: %s\n", err) return } // Register handlers. d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered)) d.Init(onStateChanged) select {} }
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) { fmt.Printf("State: %s\n", s) switch s { case gatt.StatePoweredOn: // Setup GAP and GATT services for Linux implementation. // OS X doesn't export the access of these services. d.AddService(service.NewGapService("Gopher")) // no effect on OS X d.AddService(service.NewGattService()) // no effect on OS X // A simple count service for demo. s1 := service.NewCountService() d.AddService(s1) // A sensor service for demo. sSensor := service.NewSensorService() d.AddService(sSensor) // A fake battery service for demo. s2 := service.NewBatteryService() d.AddService(s2) // Advertise device name and service's UUIDs. d.AdvertiseNameAndServices("Gopher", []gatt.UUID{s1.UUID(), sSensor.UUID(), s2.UUID()}) // Advertise as an OpenBeacon iBeacon d.AdvertiseIBeacon(gatt.MustParseUUID("AA6062F098CA42118EC4193EB73CCEB6"), 1, 2, -59) default: } } d.Init(onStateChanged) select {} }
func main() { flag.Parse() loadConfiguration() d, err := gatt.NewDevice(option.DefaultClientOptions...) if err != nil { log.Fatalf("Failed to open device, err: %s\n", err) return } // Register handlers. d.Handle( gatt.PeripheralDiscovered(onPeriphDiscovered), gatt.PeripheralConnected(onPeriphConnected), gatt.PeripheralDisconnected(onPeriphDisconnected), ) d.Init(onStateChanged) <-done fmt.Println("Done") }
func main() { f, err := eddystone.MakeURLFrame("http://google.com", -20) if err != nil { panic(err) } fmt.Println(f) // Advertise as an Eddystone beacon a := &gatt.AdvPacket{} a.AppendFlags(advFlagGeneralDiscoverable | advFlagLEOnly) a.AppendField(advTypeAllUUID16, eddystone.SvcUUIDBytes) a.AppendField(advTypeServiceData16, append(eddystone.SvcUUIDBytes, f...)) fmt.Println(a.Len(), a) 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) { fmt.Printf("State: %s\n", s) switch s { case gatt.StatePoweredOn: d.Advertise(a) default: log.Println(s) } } d.Init(onStateChanged) select {} }
func main() { flag.Parse() if len(flag.Args()) != 1 { log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0]) } d, err := gatt.NewDevice(option.DefaultClientOptions...) if err != nil { log.Fatalf("Failed to open device, err: %s\n", err) return } // Register handlers. d.Handle( gatt.PeripheralDiscovered(onPeriphDiscovered), gatt.PeripheralConnected(onPeriphConnected), gatt.PeripheralDisconnected(onPeriphDisconnected), ) d.Init(onStateChanged) <-done fmt.Println("Done") }
func main() { /*initialize GPIO*/ defer S3C2451.FreeS3C2451() PF2, _ = S3C2451.CreateGPIO(S3C2451.PF, 2) defer S3C2451.FreeGPIO(PF2) PF2.GPIOFSetDir(S3C2451.OUTPUT) /*Create BLE Device*/ 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()) }), ) fmt.Println("NAnopi Start") // A mandatory handler for monitoring device state. onStateChanged := func(d gatt.Device, s gatt.State) { fmt.Printf("State: %s\n", s) switch s { case gatt.StatePoweredOn: s1 := NewNanoPiBLEDemoService() d.AddService(s1) // Advertise device name and service's UUIDs. d.AdvertiseNameAndServices("NanoPi", []gatt.UUID{s1.UUID()}) default: } } d.Init(onStateChanged) select {} }
func main() { flag.Parse() d, err := gatt.NewDevice( gatt.LnxMaxConnections(*mc), gatt.LnxDeviceID(*dev, *chk), gatt.LnxSetAdvertisingParameters(&cmd.LESetAdvertisingParameters{ AdvertisingIntervalMin: 0x00f4, AdvertisingIntervalMax: 0x00f4, AdvertisingChannelMap: 0x07, }), ) if err != nil { log.Printf("Failed to open device, err: %s", err) return } // Register optional handlers. d.Handle( gatt.CentralConnected(func(c gatt.Central) { log.Println("Connect: ", c.ID()) }), gatt.CentralDisconnected(func(c gatt.Central) { log.Println("Disconnect: ", c.ID()) }), ) // A mandatory handler for monitoring device state. onStateChanged := func(d gatt.Device, s gatt.State) { fmt.Printf("State: %s\n", s) switch s { case gatt.StatePoweredOn: // Get bdaddr with LnxSendHCIRawCommand() bdaddr(d) // Setup GAP and GATT services. d.AddService(service.NewGapService(*name)) d.AddService(service.NewGattService()) // Add a simple counter service. s1 := service.NewCountService() d.AddService(s1) // Add a simple counter service. s2 := service.NewBatteryService() d.AddService(s2) uuids := []gatt.UUID{s1.UUID(), s2.UUID()} // If id is zero, advertise name and services statically. if *id == time.Duration(0) { d.AdvertiseNameAndServices(*name, uuids) break } // If id is non-zero, advertise name and services and iBeacon alternately. go func() { for { // Advertise as a RedBear Labs iBeacon. d.AdvertiseIBeacon(gatt.MustParseUUID("5AFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), 1, 2, -59) time.Sleep(*id) // Advertise name and services. d.AdvertiseNameAndServices(*name, uuids) time.Sleep(*ii) } }() default: } } d.Init(onStateChanged) select {} }
func main() { err := readSettingsFile() if err != nil { log.Printf("WARNING: %s", err) } defer writeSettingsFile() // Sensor d, err := gatt.NewDevice(DefaultClientOptions...) if err != nil { log.Fatalf("Failed to open device, err: %s\n", err) return } d.Handle( gatt.PeripheralDiscovered(onPeriphDiscovered), gatt.PeripheralConnected(onPeriphConnected), gatt.PeripheralDisconnected(onPeriphDisconnected), ) d.Init(onStateChanged) // API http.HandleFunc("/api", apiHandler) http.Handle("/", http.FileServer(http.Dir("./ui"))) go http.ListenAndServe(":80", nil) // GPIO err = rpio.Open() if err != nil { log.Printf("Failed to open controller, err: %s\n", err) return } defer func() { for _, p := range []rpio.Pin{fan, cool, heat} { Stop(p) } rpio.Close() }() for _, p := range []rpio.Pin{fan, cool, heat} { p.Output() Stop(p) } go func() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) <-c done <- true }() <-done exiting = true Stop(fan) Stop(cool) Stop(heat) if peripheral != nil { d.CancelConnection(peripheral) <-disconnected return } os.Exit(1) }