Station(基站)类型表示“该接口是具有控制接入点controlling access point的客户端设备管理的基本服务集basic service set(BSS)的一部分”。这是大众熟悉的无线设备标准功能:作为一个客户端来连接到网络接入点。这是测试 WiFi 质量的重要接口。
利用接口获取基站信息
利用该信息,我可以修改遍历接口的代码来获取所需信息:
for _, x := range interfaces { if x.Type == wifi.InterfaceTypeStation { // c.StationInfo(x) returns a slice of all // the staton information about the interface info, err := c.StationInfo(x) if err != nil { fmt.Printf("Station err: %s\n", err) } for _, x := range info { fmt.Printf("%+v\n", x) } }}
首先,这段程序检查了 x.Type(接口类型)是否为wifi.InterfaceTypeStation,它是一个基站接口(也是本练习中唯一涉及到的类型)。不幸的是名字出现了冲突,这个接口“类型”并不是 Golang 中的“类型”。事实上,我在这里使用了一个叫做interfaceType的 Go 类型来代表接口类型。呼,我花了一分钟才弄明白!
所以,看着上面输出显示的我的信号:-79。哇哦,感觉不大好呢。不过单看这个结果并没有太大帮助,它只能提供某个时间点的参考,只对 WiFi 网络适配器在特定物理空间的某一瞬间有效。一个连续的读数会更有用,借助于它,我们观察到信号随着树莓派的移动而变化。我可以再次修改 main函数来实现这一点。
var i *wifi.Interfacefor _, x := range interfaces { if x.Type == wifi.InterfaceTypeStation { // Loop through the interfaces, and assign the station // to var x // We could hardcode the station by name, or index, // or hardwareaddr, but this is more portable, if less efficient i = x break }}for { // c.StationInfo(x) returns a slice of all // the staton information about the interface info, err := c.StationInfo(i) if err != nil { fmt.Printf("Station err: %s\n", err) } for _, x := range info { fmt.Printf("Signal: %d\n", x.Signal) } time.Sleep(time.Second)}
首先,我命名了一个 wifi.Interface类型的变量i。因为它在循环的范围外,所以我可以用它来存储接口信息。循环内创建的任何变量在该循环的范围外都是不可访问的。