(摘) Win下蓝牙扫描

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

其实是想在Win下做一个普通蓝牙设备的扫描,似乎并没达到。
网上搜索到的一般都是BLE,而我要搜索类似手机的蓝牙。

以下代码是BLE扫描

package main

import (
	"fmt"
	"os/exec"
	"time"

	"tinygo.org/x/bluetooth"
)

var (
	deviceMAC       = "4C:57:CA:7D:5D:26"      // Bluetooth Device MAC ID 7C:2A:DB:00:7A:94
	minRSSI   int16 = -60                      // Minimum RSSI tolerance value of the bluetooth device
	timeout         = 30 * time.Second         // Timeout Time
	adapter         = bluetooth.DefaultAdapter // Bluetooth Receiver
)

var timer = time.NewTicker(timeout)

func lockWindows() {
	err := exec.Command("cmd", "/C", "rundll32.exe user32.dll,LockWorkStation").Run()
	if err != nil {
		return
	}
}

func startTimer() {
	go func() {
		for range timer.C {
			lockWindows()
		}
	}()
}

func main() {
	startTimer()

	err := adapter.Enable()
	if err != nil {
		panic("Failed to initialize adapter!")
	}

	err = adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
		if device.Address.String() == deviceMAC {
			if device.RSSI > minRSSI {
				timer.Reset(timeout)
			}
		}
	})
	if err != nil {
		panic("Scan Error!")
	}
}

这个是普通蓝牙扫描,但设备没有刷新的样子,是Win的缓存?

package main

import (
	"fmt"

	"github.com/infiniteloopcloud/bluetooth-go"
)

func main() {
	devices, err := bluetooth.NewScanner().Scan()
	if err != nil {
		// handle err
	}
	for i, n := range devices {
		fmt.Println(i, n)
	}
}

相关文章