scan me bitch

This commit is contained in:
freqyXin 2021-04-14 09:30:41 -07:00
parent c51ae3d07b
commit 56c79486b5
3 changed files with 21 additions and 0 deletions

@ -1,7 +1,15 @@
package dust
import (
projVars "protomolecule/src/vars"
)
func Must(action string, err error) {
if err != nil {
panic("failed to" + action + ": " + err.Error())
}
}
func FirstList() {
projVars.ScanList = make(map[string]string)
}

@ -4,6 +4,7 @@ import (
"github.com/fatih/color"
"protomolecule/src/dust"
projVars "protomolecule/src/vars"
"tinygo.org/x/bluetooth"
)
@ -11,14 +12,23 @@ import (
var scanAdapter = bluetooth.DefaultAdapter
func Scanners() {
dust.FirstList()
// Enable BLE interface.
dust.Must("enable BLE stack", scanAdapter.Enable())
//TODO: only scan a device once, skip if its in the map
// Start scanning.
println("scanning...")
err := scanAdapter.Scan(func(scanAdapter *bluetooth.Adapter, device bluetooth.ScanResult) {
color.Cyan("found device:")
println(device.Address.String(), device.RSSI, device.LocalName())
projVars.ScanList[device.LocalName()] = device.Address.String()
//fucking sanity check
i := projVars.ScanList[device.LocalName()]
color.Red(i)
})
dust.Must("start scan", err)
}

3
src/vars/projVars.go Normal file

@ -0,0 +1,3 @@
package projVars
var ScanList map[string]string