diff --git a/gattc_darwin.go b/gattc_darwin.go index c89f657..32876b3 100644 --- a/gattc_darwin.go +++ b/gattc_darwin.go @@ -15,13 +15,7 @@ import ( // Passing a nil slice of UUIDs will return a complete list of // services. func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { - cbuuids := []cbgo.UUID{} - for _, u := range uuids { - uuid, _ := cbgo.ParseUUID(u.String()) - cbuuids = append(cbuuids, uuid) - } - - d.prph.DiscoverServices(cbuuids) + d.prph.DiscoverServices([]cbgo.UUID{}) // clear cache of services d.services = make(map[UUID]*DeviceService) @@ -31,9 +25,24 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { case <-d.servicesChan: svcs := []DeviceService{} for _, dsvc := range d.prph.Services() { - uuid, _ := ParseUUID(dsvc.UUID().String()) + dsvcuuid, _ := ParseUUID(dsvc.UUID().String()) + // add if in our original list + if len(uuids) > 0 { + found := false + for _, uuid := range uuids { + if dsvcuuid.String() == uuid.String() { + // one of the services we're looking for. + found = true + break + } + } + if !found { + continue + } + } + svc := DeviceService{ - uuidWrapper: uuid, + uuidWrapper: dsvcuuid, device: d, service: dsvc, } @@ -75,10 +84,6 @@ func (s *DeviceService) UUID() UUID { // characteristics. func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) { cbuuids := []cbgo.UUID{} - for _, u := range uuids { - uuid, _ := cbgo.ParseUUID(u.String()) - cbuuids = append(cbuuids, uuid) - } s.device.prph.DiscoverCharacteristics(cbuuids, s.service) @@ -90,10 +95,25 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter case <-s.device.charsChan: chars := []DeviceCharacteristic{} for _, dchar := range s.service.Characteristics() { - uuid, _ := ParseUUID(dchar.UUID().String()) + dcuuid, _ := ParseUUID(dchar.UUID().String()) + // add if in our original list + if len(uuids) > 0 { + found := false + for _, uuid := range uuids { + if dcuuid.String() == uuid.String() { + // one of the characteristics we're looking for. + found = true + break + } + } + if !found { + continue + } + } + char := DeviceCharacteristic{ deviceCharacteristic: &deviceCharacteristic{ - uuidWrapper: uuid, + uuidWrapper: dcuuid, service: s, characteristic: dchar, }, diff --git a/uuid.go b/uuid.go index 75f30cd..97fe11b 100644 --- a/uuid.go +++ b/uuid.go @@ -11,20 +11,6 @@ type UUID [4]uint32 var errInvalidUUID = errors.New("bluetooth: failed to parse UUID") -// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID. -// -// Note: only use registered UUIDs. See -// https://www.bluetooth.com/specifications/gatt/services/ for a list. -func New16BitUUID(shortUUID uint16) UUID { - // https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uuid-into-a-128-bit-uuid - var uuid UUID - uuid[0] = 0x5F9B34FB - uuid[1] = 0x80000080 - uuid[2] = 0x00001000 - uuid[3] = uint32(shortUUID) - return uuid -} - // NewUUID returns a new UUID based on the 128-bit (or 16-byte) input. func NewUUID(uuid [16]byte) UUID { u := UUID{} diff --git a/uuid16.go b/uuid16.go new file mode 100644 index 0000000..6915f36 --- /dev/null +++ b/uuid16.go @@ -0,0 +1,17 @@ +// +build !darwin + +package bluetooth + +// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID. +// +// Note: only use registered UUIDs. See +// https://www.bluetooth.com/specifications/gatt/services/ for a list. +func New16BitUUID(shortUUID uint16) UUID { + // https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uuid-into-a-128-bit-uuid + var uuid UUID + uuid[0] = 0x5F9B34FB + uuid[1] = 0x80000080 + uuid[2] = 0x00001000 + uuid[3] = uint32(shortUUID) + return uuid +} diff --git a/uuid16_darwin.go b/uuid16_darwin.go new file mode 100644 index 0000000..4d7f9c7 --- /dev/null +++ b/uuid16_darwin.go @@ -0,0 +1,15 @@ +package bluetooth + +// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID. +// +// Note: only use registered UUIDs. See +// https://www.bluetooth.com/specifications/gatt/services/ for a list. +func New16BitUUID(shortUUID uint16) UUID { + // mac OS uses a unique format for UUID. + var uuid UUID + uuid[0] = 0x00000000 + uuid[1] = 0x00000000 + uuid[2] = 0x00000000 + uuid[3] = uint32(shortUUID) << 16 + return uuid +}