44 "context"
55 "errors"
66 "fmt"
7+ "syscall"
78 "unsafe"
89
910 "github.com/go-ole/go-ole"
@@ -231,21 +232,33 @@ func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedE
231232 defer winAdv .Release ()
232233
233234 var manufacturerData []ManufacturerDataElement
234- mVector , _ := winAdv .GetManufacturerData ()
235- if mVector != nil {
236- defer mVector .Release ()
237- mSize , _ := mVector .GetSize ()
238- for i := uint32 (0 ); i < mSize ; i ++ {
239- element , _ := mVector .GetAt (i )
235+ var serviceUUIDs []UUID
236+ if winAdv , err := args .GetAdvertisement (); err == nil && winAdv != nil {
237+ // Extract manufacturer data
238+ vector , _ := winAdv .GetManufacturerData ()
239+ size , _ := vector .GetSize ()
240+ for i := uint32 (0 ); i < size ; i ++ {
241+ element , _ := vector .GetAt (i )
240242 manData := (* advertisement .BluetoothLEManufacturerData )(element )
243+
241244 companyID , _ := manData .GetCompanyId ()
242245 buffer , _ := manData .GetData ()
243246 manufacturerData = append (manufacturerData , ManufacturerDataElement {
244247 CompanyID : companyID ,
245248 Data : bufferToSlice (buffer ),
246249 })
247- buffer .Release ()
248- manData .Release ()
250+ }
251+
252+ // Extract service UUIDs
253+ vector , _ = winAdv .GetServiceUuids ()
254+ size , _ = vector .GetSize ()
255+ for i := uint32 (0 ); i < size ; i ++ {
256+ element , _ := vector .GetAt (i )
257+ // element is not a pointer, but a GUID struct. But we cannot convert
258+ // unsafe.Pointer to a non-pointer type, so instead we are doing this:
259+ serviceGUID := (* syscall .GUID )(unsafe .Pointer (& element ))
260+ uuid := GUIDToUUID (* serviceGUID )
261+ serviceUUIDs = append (serviceUUIDs , uuid )
249262 }
250263 }
251264
@@ -254,13 +267,31 @@ func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedE
254267 result .AdvertisementPayload = & advertisementFields {
255268 AdvertisementFields {
256269 LocalName : localName ,
270+ ServiceUUIDs : serviceUUIDs ,
257271 ManufacturerData : manufacturerData ,
258272 },
259273 }
260274
261275 return result
262276}
263277
278+ func GUIDToUUID (guid syscall.GUID ) UUID {
279+ return NewUUID ([16 ]byte {
280+ byte (guid .Data1 >> 24 ),
281+ byte (guid .Data1 >> 16 ),
282+ byte (guid .Data1 >> 8 ),
283+ byte (guid .Data1 ),
284+ byte (guid .Data2 >> 8 ),
285+ byte (guid .Data2 ),
286+ byte (guid .Data3 >> 8 ),
287+ byte (guid .Data3 ),
288+ guid .Data4 [0 ], guid .Data4 [1 ],
289+ guid .Data4 [2 ], guid .Data4 [3 ],
290+ guid .Data4 [4 ], guid .Data4 [5 ],
291+ guid .Data4 [6 ], guid .Data4 [7 ],
292+ })
293+ }
294+
264295func bufferToSlice (buffer * streams.IBuffer ) []byte {
265296 dataReader , _ := streams .DataReaderFromBuffer (buffer )
266297 defer dataReader .Release ()
0 commit comments