func doConnect(c *cli.Context) error { if c.String("addr") != "" { curr.addr = ble.NewAddr(c.String("addr")) curr.client = curr.clients[curr.addr.String()] } if curr.client != nil { return nil } return cmdConnect(c) }
func cmdConnect(c *cli.Context) error { curr.client = nil var cln ble.Client var err error ctx := ble.WithSigHandler(context.WithTimeout(context.Background(), c.Duration("tmo"))) if c.String("addr") != "" { curr.addr = ble.NewAddr(c.String("addr")) fmt.Printf("Dialing to specified address: %s\n", curr.addr) cln, err = ble.Dial(ctx, curr.addr) } else if filter(c) != nil { fmt.Printf("Scanning with filter...\n") if cln, err = ble.Connect(ctx, filter(c)); err == nil { curr.addr = cln.Address() fmt.Printf("Connected to %s\n", curr.addr) } } else if curr.addr != nil { fmt.Printf("Dialing to implicit address: %s\n", curr.addr) cln, err = ble.Dial(ctx, curr.addr) } else { return fmt.Errorf("no filter specified, and cached peripheral address") } if err == nil { curr.client = cln curr.clients[cln.Address().String()] = cln } go func() { <-cln.Disconnected() delete(curr.clients, cln.Address().String()) curr.client = nil fmt.Printf("\n%s disconnected\n", cln.Address().String()) }() return err }