Exemplo n.º 1
0
// Send raw message
func writeToBluetooth(b []byte, n *nxt.NXT) error {
	log.Printf("Writing to <%s> message: %v", n, b)

	//n.CommandChannel <- nxt.NewDirectCommand(0x09, b, nil)
	header := []byte{MessageRequiresResponse, TextMessage}

	reader := n.Connection()
	_, err := reader.Write(append(header, b...))
	if err != nil {
		return err
	}

	reply := nxt.ReplyStatus(<-AckChannel)
	log.Println("Status:", reply)

	if reply == nxt.Success {
		return nil
	}
	return fmt.Errorf(reply.String())

	// replyChannel := make(chan *nxt.ReplyTelegram)
	// n.CommandChannel <- nxt.NewDirectCommand(0x09, b, replyChannel)
	// reply := <-replyChannel
	// log.Println("Immediate reply:", reply)

	// if reply.IsSuccess() {
	// 	return nil
	// }
	// return fmt.Errorf(reply.String())

	//return nil
}
Exemplo n.º 2
0
// Read raw message
func readFromBluetooth(n *nxt.NXT) ([]byte, int, error) {
	res := make([]byte, 64)

	reader := n.Connection()
	numRead, err := reader.Read(res)
	if err != nil {
		return nil, 0, err
	}
	return res, numRead, nil
}