darwin: properly handle 16-bit UUIDs for service and characteristics in the unique format used by macOS
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
parent
a355f254da
commit
b4b125480a
4 changed files with 67 additions and 29 deletions
|
@ -15,13 +15,7 @@ import (
|
||||||
// Passing a nil slice of UUIDs will return a complete list of
|
// Passing a nil slice of UUIDs will return a complete list of
|
||||||
// services.
|
// services.
|
||||||
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
|
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
|
||||||
cbuuids := []cbgo.UUID{}
|
d.prph.DiscoverServices([]cbgo.UUID{})
|
||||||
for _, u := range uuids {
|
|
||||||
uuid, _ := cbgo.ParseUUID(u.String())
|
|
||||||
cbuuids = append(cbuuids, uuid)
|
|
||||||
}
|
|
||||||
|
|
||||||
d.prph.DiscoverServices(cbuuids)
|
|
||||||
|
|
||||||
// clear cache of services
|
// clear cache of services
|
||||||
d.services = make(map[UUID]*DeviceService)
|
d.services = make(map[UUID]*DeviceService)
|
||||||
|
@ -31,9 +25,24 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
|
||||||
case <-d.servicesChan:
|
case <-d.servicesChan:
|
||||||
svcs := []DeviceService{}
|
svcs := []DeviceService{}
|
||||||
for _, dsvc := range d.prph.Services() {
|
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{
|
svc := DeviceService{
|
||||||
uuidWrapper: uuid,
|
uuidWrapper: dsvcuuid,
|
||||||
device: d,
|
device: d,
|
||||||
service: dsvc,
|
service: dsvc,
|
||||||
}
|
}
|
||||||
|
@ -75,10 +84,6 @@ func (s *DeviceService) UUID() UUID {
|
||||||
// characteristics.
|
// characteristics.
|
||||||
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
|
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
|
||||||
cbuuids := []cbgo.UUID{}
|
cbuuids := []cbgo.UUID{}
|
||||||
for _, u := range uuids {
|
|
||||||
uuid, _ := cbgo.ParseUUID(u.String())
|
|
||||||
cbuuids = append(cbuuids, uuid)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
|
s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
|
||||||
|
|
||||||
|
@ -90,10 +95,25 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
|
||||||
case <-s.device.charsChan:
|
case <-s.device.charsChan:
|
||||||
chars := []DeviceCharacteristic{}
|
chars := []DeviceCharacteristic{}
|
||||||
for _, dchar := range s.service.Characteristics() {
|
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{
|
char := DeviceCharacteristic{
|
||||||
deviceCharacteristic: &deviceCharacteristic{
|
deviceCharacteristic: &deviceCharacteristic{
|
||||||
uuidWrapper: uuid,
|
uuidWrapper: dcuuid,
|
||||||
service: s,
|
service: s,
|
||||||
characteristic: dchar,
|
characteristic: dchar,
|
||||||
},
|
},
|
||||||
|
|
14
uuid.go
14
uuid.go
|
@ -11,20 +11,6 @@ type UUID [4]uint32
|
||||||
|
|
||||||
var errInvalidUUID = errors.New("bluetooth: failed to parse UUID")
|
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.
|
// NewUUID returns a new UUID based on the 128-bit (or 16-byte) input.
|
||||||
func NewUUID(uuid [16]byte) UUID {
|
func NewUUID(uuid [16]byte) UUID {
|
||||||
u := UUID{}
|
u := UUID{}
|
||||||
|
|
17
uuid16.go
Normal file
17
uuid16.go
Normal file
|
@ -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
|
||||||
|
}
|
15
uuid16_darwin.go
Normal file
15
uuid16_darwin.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Reference in a new issue