Skip to content

Commit 852fa4a

Browse files
aykevldeadprogram
authored andcommitted
windows: check for error when scanning
This was my attempt to figure out why scanning doesn't work on my system. Sadly it still doesn't work, but at least I know there's no error in this place. (Note: I'm doing this on Windows ARM in a VM, so it's a rather special setup).
1 parent e0d5fd4 commit 852fa4a

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

gap_windows.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,25 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
6868
// Wait for when advertisement has stopped by a call to StopScan().
6969
// Advertisement doesn't seem to stop right away, there is an
7070
// intermediate Stopping state.
71-
stoppingChan := make(chan struct{})
71+
stoppingChan := make(chan error)
7272
// TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementWatcherStoppedEventArgs>
7373
eventStoppedGuid := winrt.ParameterizedInstanceGUID(
7474
foundation.GUIDTypedEventHandler,
7575
advertisement.SignatureBluetoothLEAdvertisementWatcher,
7676
advertisement.SignatureBluetoothLEAdvertisementWatcherStoppedEventArgs,
7777
)
78-
stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, _ unsafe.Pointer) {
79-
// Note: the args parameter has an Error property that should
80-
// probably be checked, but I'm not sure when stopping the
81-
// advertisement watcher could ever result in an error (except
82-
// for bugs).
78+
stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, arg unsafe.Pointer) {
79+
args := (*advertisement.BluetoothLEAdvertisementWatcherStoppedEventArgs)(arg)
80+
errCode, err := args.GetError()
81+
if err != nil {
82+
// Got an error while getting the error value, that shouldn't
83+
// happen.
84+
stoppingChan <- fmt.Errorf("failed to get stopping error value: %w", err)
85+
} else if errCode != bluetooth.BluetoothErrorSuccess {
86+
// Could not stop the scan? I'm not sure when this would actually
87+
// happen.
88+
stoppingChan <- fmt.Errorf("failed to stop scanning (error code %d)", errCode)
89+
}
8390
close(stoppingChan)
8491
})
8592
defer stoppedHandler.Release()
@@ -96,8 +103,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
96103
}
97104

98105
// Wait until advertisement has stopped, and finish.
99-
<-stoppingChan
100-
return nil
106+
return <-stoppingChan
101107
}
102108

103109
func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedEventArgs) ScanResult {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/go-ole/go-ole v1.2.6
77
github.com/godbus/dbus/v5 v5.1.0
8-
github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38
8+
github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4
99
github.com/tinygo-org/cbgo v0.0.4
1010
golang.org/x/crypto v0.12.0
1111
tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3
1010
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1111
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1212
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13-
github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38 h1:YcsdT0vhLMBWScwoO9FHZdjcFqjIWfQENMzq0PNxODs=
14-
github.com/saltosystems/winrt-go v0.0.0-20240110120258-ad49e9790c38/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA=
13+
github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4 h1:3R7V8/xskRIAxAfHKDRWHu5pey0uiyf4wio2RwLXGyM=
14+
github.com/saltosystems/winrt-go v0.0.0-20240312144256-43a71786fba4/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA=
1515
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
1616
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
1717
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=

0 commit comments

Comments
 (0)