12345678910111213141516171819202122232425262728293031323334353637 |
- 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 {
- panic("failed to " + action + ": " + err.Error())
- }
- }
|