@@ -3,13 +3,15 @@ package bluetooth
33import (
44 "errors"
55 "fmt"
6+ "strings"
67 "syscall"
78 "unsafe"
89
910 "github.com/go-ole/go-ole"
1011 "github.com/saltosystems/winrt-go"
1112 "github.com/saltosystems/winrt-go/windows/devices/bluetooth/advertisement"
1213 "github.com/saltosystems/winrt-go/windows/foundation"
14+ "golang.org/x/sys/windows"
1315)
1416
1517var _ BLEAdapter = (* Adapter )(nil )
@@ -112,6 +114,23 @@ func hresultToError(hr uint32) error {
112114 return fmt .Errorf ("Bluetooth ATT error (code 0x%04X)" , code )
113115 }
114116
117+ // For FACILITY_WIN32 the lower 16 bits are a plain Win32 error code that
118+ // FormatMessage understands; for other facilities try the full HRESULT.
119+ errCode := hr
120+ if facility == 0x7 { // FACILITY_WIN32
121+ errCode = code
122+ }
123+
124+ buf := make ([]uint16 , 512 )
125+ n , err := windows .FormatMessage (
126+ windows .FORMAT_MESSAGE_FROM_SYSTEM | windows .FORMAT_MESSAGE_IGNORE_INSERTS ,
127+ 0 , errCode , 0 , buf , nil ,
128+ )
129+ if err == nil && n > 0 {
130+ msg := strings .TrimRight (windows .UTF16ToString (buf [:n ]), " \r \n " )
131+ return fmt .Errorf ("HRESULT 0x%08X: %s" , hr , msg )
132+ }
133+
115134 return fmt .Errorf ("HRESULT 0x%08X" , hr )
116135}
117136
0 commit comments