@@ -68,18 +68,25 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
68
68
// Wait for when advertisement has stopped by a call to StopScan().
69
69
// Advertisement doesn't seem to stop right away, there is an
70
70
// intermediate Stopping state.
71
- stoppingChan := make (chan struct {} )
71
+ stoppingChan := make (chan error )
72
72
// TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementWatcherStoppedEventArgs>
73
73
eventStoppedGuid := winrt .ParameterizedInstanceGUID (
74
74
foundation .GUIDTypedEventHandler ,
75
75
advertisement .SignatureBluetoothLEAdvertisementWatcher ,
76
76
advertisement .SignatureBluetoothLEAdvertisementWatcherStoppedEventArgs ,
77
77
)
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
+ }
83
90
close (stoppingChan )
84
91
})
85
92
defer stoppedHandler .Release ()
@@ -96,8 +103,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
96
103
}
97
104
98
105
// Wait until advertisement has stopped, and finish.
99
- <- stoppingChan
100
- return nil
106
+ return <- stoppingChan
101
107
}
102
108
103
109
func getScanResultFromArgs (args * advertisement.BluetoothLEAdvertisementReceivedEventArgs ) ScanResult {
0 commit comments