package initialize import ( "confrontation-training/global" "confrontation-training/models/gateway" "strings" "tinygo.org/x/bluetooth" ) var adapter = bluetooth.DefaultAdapter func FindGateway() { // Enable BLE interface. must("enable BLE stack", adapter.Enable()) global.GatewayList = make(map[string]gateway.GatewayInfo) // Start scanning. println("scanning...") err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) { println("found device:", device.Address.String(), device.RSSI, device.LocalName()) mac := strings.ReplaceAll(device.Address.String(), ":", "") name := device.LocalName() if strings.HasSuffix(name, mac) { global.Log4J.Info("发现可能是网关的设备:mac="+mac, " name="+name) var gatewayInfo gateway.GatewayInfo gatewayInfo.Mac = mac gatewayInfo.Name = name global.GatewayList[mac] = gatewayInfo } }) must("start scan", err) } func must(action string, err error) { if err != nil { global.Log4J.Info("failed to " + action + ": " + err.Error()) } }