这个代码是GATT clint 也就是蓝牙主机 主动去扫描从机的 

我们的代码就是基于这个开始的

直接可以run的 如上

现在开始拆解

第一步   键盘输入的模块

自己del写小代码 直接拖过去 失败

这样修改mod是也不行的  需要修改所有者

chgrp  用户名    文件名  -R

chown 用户名   文件名  -R

-R表示递归目录下所有文件

package main

import (
    "fmt"
	"tinygo.org/x/bluetooth/rawterm"
)

func main() {
	println("Connected. Exit console using Ctrl-X.")
	rawterm.Configure()
	defer rawterm.Restore()
	var line []byte
	for {
		ch := rawterm.Getchar()
		line = append(line, ch)

		// Send the current line to the central.
		if ch == '\x18' {
			// The user pressed Ctrl-X, exit the program.
			break//--------------------------跳出for 真个程序结束!!!!!!!
		} else if ch == '\n' {
			sendbuf := line // copy buffer
			// Reset the slice while keeping the buffer in place.
			line = line[:0]

			// Send the sendbuf after breaking it up in pieces.
			for len(sendbuf) != 0 {
				// Chop off up to 20 bytes from the sendbuf.
				partlen := 20
				if len(sendbuf) < 20 {
					partlen = len(sendbuf)
				}
				part := sendbuf[:partlen]
				sendbuf = sendbuf[partlen:]
				// This performs a "write command" aka "write without response".
				//_, err := rx.WriteWithoutResponse(part)

				fmt.Println(part)

			}
		}
	}
}

代码就是键盘输入什么  他就输出什么

测试效果:

进一步看这个:go测试代码--package rawterm处理控制台_GKoSon的博客-CSDN博客

第二部:上面的需要从机代码 先换一个GATT SERVICE 这样可以用手机APP测试

手机看到一个NUS的设备

 发送11上面就会显示

第三步:研究上面GW用的是GATT CLINET 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐