findGateway.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package initialize
  2. import (
  3. "confrontation-training/global"
  4. "confrontation-training/models/gateway"
  5. "strings"
  6. "tinygo.org/x/bluetooth"
  7. )
  8. var adapter = bluetooth.DefaultAdapter
  9. func FindGateway() {
  10. // Enable BLE interface.
  11. must("enable BLE stack", adapter.Enable())
  12. global.GatewayList = make(map[string]gateway.GatewayInfo)
  13. // Start scanning.
  14. println("scanning...")
  15. err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
  16. println("found device:", device.Address.String(), device.RSSI, device.LocalName())
  17. mac := strings.ReplaceAll(device.Address.String(), ":", "")
  18. name := device.LocalName()
  19. if strings.HasSuffix(name, mac) {
  20. global.Log4J.Info("发现可能是网关的设备:mac="+mac, " name="+name)
  21. var gatewayInfo gateway.GatewayInfo
  22. gatewayInfo.Mac = mac
  23. gatewayInfo.Name = name
  24. global.GatewayList[mac] = gatewayInfo
  25. }
  26. })
  27. must("start scan", err)
  28. }
  29. func must(action string, err error) {
  30. if err != nil {
  31. global.Log4J.Info("failed to " + action + ": " + err.Error())
  32. }
  33. }