diff --git a/gattc_windows.go b/gattc_windows.go index f0f69ac..0f8746f 100644 --- a/gattc_windows.go +++ b/gattc_windows.go @@ -79,6 +79,13 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) { } var services []DeviceService + + if len(filterUUIDs) > 0 { + // The caller wants to get a list of services in a specific + // order. + services = make([]DeviceService, len(filterUUIDs)) + } + for i := uint32(0); i < servicesSize; i++ { s, err := servicesVector.GetAt(i) if err != nil { @@ -95,29 +102,26 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) { // only include services that are included in the input filter if len(filterUUIDs) > 0 { - found := false - for _, uuid := range filterUUIDs { + for j, uuid := range filterUUIDs { if serviceUuid.String() == uuid.String() { // One of the services we're looking for. - found = true + services[j] = makeService(serviceUuid, srv, d) break } } - if !found { - continue - } + } else { + // The caller wants to get all services, in any order. + services = append(services, makeService(serviceUuid, srv, d)) } go func() { <-d.ctx.Done() srv.Close() }() + } - services = append(services, DeviceService{ - uuidWrapper: serviceUuid, - service: srv, - device: d, - }) + if slices.Contains(services, (DeviceService{})) { + return nil, errors.New("bluetooth: did not find all requested services") } return services, nil @@ -144,8 +148,24 @@ func winRTUuidToUuid(uuid syscall.GUID) UUID { // struct method of the same name. type uuidWrapper = UUID +// Small helper to create a DeviceService object. +func makeService(serviceUuid uuidWrapper, srv *genericattributeprofile.GattDeviceService, d Device) DeviceService { + svc := DeviceService{ + deviceService: &deviceService{ + uuidWrapper: serviceUuid, + service: srv, + device: d, + }, + } + return svc +} + // DeviceService is a BLE service on a connected peripheral device. type DeviceService struct { + *deviceService +} + +type deviceService struct { uuidWrapper service *genericattributeprofile.GattDeviceService