コード例 #1
0
ファイル: main.go プロジェクト: currantlabs/ble
func cmdRead(c *cli.Context) error {
	if err := doGetUUID(c); err != nil {
		return err
	}
	if err := doConnect(c); err != nil {
		return err
	}
	if err := doDiscover(c); err != nil {
		return err
	}
	if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil {
		b, err := curr.client.ReadCharacteristic(u.(*ble.Characteristic))
		if err != nil {
			return errors.Wrap(err, "can't read characteristic")
		}
		fmt.Printf("    Value         %x | %q\n", b, b)
		return nil
	}
	if u := curr.profile.Find(ble.NewDescriptor(curr.uuid)); u != nil {
		b, err := curr.client.ReadDescriptor(u.(*ble.Descriptor))
		if err != nil {
			return errors.Wrap(err, "can't read descriptor")
		}
		fmt.Printf("    Value         %x | %q\n", b, b)
		return nil
	}
	return errNoUUID
}
コード例 #2
0
ファイル: echo.go プロジェクト: currantlabs/ble
// NewEchoChar ...
func NewEchoChar() *ble.Characteristic {
	e := &echoChar{m: make(map[string]chan []byte)}
	c := ble.NewCharacteristic(EchoCharUUID)
	c.HandleWrite(ble.WriteHandlerFunc(e.written))
	c.HandleNotify(ble.NotifyHandlerFunc(e.echo))
	c.HandleIndicate(ble.NotifyHandlerFunc(e.echo))
	return c
}
コード例 #3
0
ファイル: count.go プロジェクト: currantlabs/ble
// NewCountChar ...
func NewCountChar() *ble.Characteristic {
	n := 0
	c := ble.NewCharacteristic(CountCharUUID)
	c.HandleRead(ble.ReadHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) {
		fmt.Fprintf(rsp, "count: Read %d", n)
		log.Printf("count: Read %d", n)
		n++
	}))

	c.HandleWrite(ble.WriteHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) {
		log.Printf("count: Wrote %s", string(req.Data()))
	}))

	c.HandleNotify(ble.NotifyHandlerFunc(func(req ble.Request, n ble.Notifier) {
		cnt := 0
		log.Printf("count: Notification subscribed")
		for {
			select {
			case <-n.Context().Done():
				log.Printf("count: Notification unsubscribed")
				return
			case <-time.After(time.Second):
				log.Printf("count: Notify: %d", cnt)
				if _, err := fmt.Fprintf(n, "Count: %d", cnt); err != nil {
					// Client disconnected prematurely before unsubscription.
					log.Printf("count: Failed to notify : %s", err)
					return
				}
				cnt++
			}
		}
	}))

	c.HandleIndicate(ble.NotifyHandlerFunc(func(req ble.Request, n ble.Notifier) {
		cnt := 0
		log.Printf("count: Indication subscribed")
		for {
			select {
			case <-n.Context().Done():
				log.Printf("count: Indication unsubscribed")
				return
			case <-time.After(time.Second):
				log.Printf("count: Indicate: %d", cnt)
				if _, err := fmt.Fprintf(n, "Count: %d", cnt); err != nil {
					// Client disconnected prematurely before unsubscription.
					log.Printf("count: Failed to indicate : %s", err)
					return
				}
				cnt++
			}
		}
	}))
	return c
}
コード例 #4
0
ファイル: main.go プロジェクト: currantlabs/ble
func cmdUnsub(c *cli.Context) error {
	if err := doGetUUID(c); err != nil {
		return err
	}
	if err := doConnect(c); err != nil {
		return err
	}
	if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil {
		err := curr.client.Unsubscribe(u.(*ble.Characteristic), c.Bool("ind"))
		return errors.Wrap(err, "can't unsubscribe to characteristic")
	}
	return errNoUUID
}
コード例 #5
0
ファイル: main.go プロジェクト: currantlabs/ble
func cmdSub(c *cli.Context) error {
	if err := doGetUUID(c); err != nil {
		return err
	}
	if err := doConnect(c); err != nil {
		return err
	}
	if err := doDiscover(c); err != nil {
		return err
	}
	// NotificationHandler
	h := func(req []byte) { fmt.Printf("notified: %x | %q\n", req, req) }
	if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil {
		err := curr.client.Subscribe(u.(*ble.Characteristic), c.Bool("ind"), h)
		return errors.Wrap(err, "can't subscribe to characteristic")
	}
	return errNoUUID
}
コード例 #6
0
ファイル: main.go プロジェクト: currantlabs/ble
func cmdWrite(c *cli.Context) error {
	if err := doGetUUID(c); err != nil {
		return err
	}
	if err := doConnect(c); err != nil {
		return err
	}
	if err := doDiscover(c); err != nil {
		return err
	}
	if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil {
		err := curr.client.WriteCharacteristic(u.(*ble.Characteristic), []byte("hello"), true)
		return errors.Wrap(err, "can't write characteristic")
	}
	if u := curr.profile.Find(ble.NewDescriptor(curr.uuid)); u != nil {
		err := curr.client.WriteDescriptor(u.(*ble.Descriptor), []byte("fixme"))
		return errors.Wrap(err, "can't write descriptor")
	}
	return errNoUUID
}