diff --git a/gap_windows.go b/gap_windows.go index 146f2aa..f177977 100644 --- a/gap_windows.go +++ b/gap_windows.go @@ -68,18 +68,25 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) { // Wait for when advertisement has stopped by a call to StopScan(). // Advertisement doesn't seem to stop right away, there is an // intermediate Stopping state. - stoppingChan := make(chan struct{}) + stoppingChan := make(chan error) // TypedEventHandler eventStoppedGuid := winrt.ParameterizedInstanceGUID( foundation.GUIDTypedEventHandler, advertisement.SignatureBluetoothLEAdvertisementWatcher, advertisement.SignatureBluetoothLEAdvertisementWatcherStoppedEventArgs, ) - stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, _ unsafe.Pointer) { - // Note: the args parameter has an Error property that should - // probably be checked, but I'm not sure when stopping the - // advertisement watcher could ever result in an error (except - // for bugs). + stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, arg unsafe.Pointer) { + args := (*advertisement.BluetoothLEAdvertisementWatcherStoppedEventArgs)(arg) + errCode, err := args.GetError() + if err != nil { + // Got an error while getting the error value, that shouldn't + // happen. + stoppingChan <- fmt.Errorf("failed to get stopping error value: %w", err) + } else if errCode != bluetooth.BluetoothErrorSuccess { + // Could not stop the scan? I'm not sure when this would actually + // happen. + stoppingChan <- fmt.Errorf("failed to stop scanning (error code %d)", errCode) + } close(stoppingChan) }) defer stoppedHandler.Release() @@ -96,8 +103,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) { } // Wait until advertisement has stopped, and finish. - <-stoppingChan - return nil + return <-stoppingChan } func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedEventArgs) ScanResult { diff --git a/go.mod b/go.mod index 8a0dd98..8f69552 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/go-ole/go-ole v1.2.6 github.com/godbus/dbus/v5 v5.1.0 - github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38 + github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4 github.com/tinygo-org/cbgo v0.0.4 golang.org/x/crypto v0.12.0 tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6 diff --git a/go.sum b/go.sum index d872848..d7a89e5 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38 h1:YcsdT0vhLMBWScwoO9FHZdjcFqjIWfQENMzq0PNxODs= -github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA= +github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4 h1:3R7V8/xskRIAxAfHKDRWHu5pey0uiyf4wio2RwLXGyM= +github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA= github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=