linux: do not randomize order of returned discovered services/chars
Previously the list of services and characteristics wasn't sorted, and because of the way Go maps work they were in fact randomized. This commit fixes the sorting for services and characteristics, which both suffered from the same lack of sorting. This fixes https://github.com/tinygo-org/bluetooth/issues/135.
This commit is contained in:
parent
5717af56e0
commit
8260f2fb93
1 changed files with 18 additions and 6 deletions
|
@ -5,9 +5,11 @@ package bluetooth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/muka/go-bluetooth/bluez"
|
"github.com/muka/go-bluetooth/bluez"
|
||||||
"github.com/muka/go-bluetooth/bluez/profile/gatt"
|
"github.com/muka/go-bluetooth/bluez/profile/gatt"
|
||||||
)
|
)
|
||||||
|
@ -70,15 +72,20 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
objects := make([]string, 0, len(list))
|
||||||
for objectPath := range list {
|
for objectPath := range list {
|
||||||
if !strings.HasPrefix(string(objectPath), string(d.device.Path())+"/service") {
|
objects = append(objects, string(objectPath))
|
||||||
|
}
|
||||||
|
sort.Strings(objects)
|
||||||
|
for _, objectPath := range objects {
|
||||||
|
if !strings.HasPrefix(objectPath, string(d.device.Path())+"/service") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
suffix := string(objectPath)[len(d.device.Path()+"/"):]
|
suffix := objectPath[len(d.device.Path()+"/"):]
|
||||||
if len(strings.Split(suffix, "/")) != 1 {
|
if len(strings.Split(suffix, "/")) != 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
service, err := gatt.NewGattService1(objectPath)
|
service, err := gatt.NewGattService1(dbus.ObjectPath(objectPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -160,15 +167,20 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
objects := make([]string, 0, len(list))
|
||||||
for objectPath := range list {
|
for objectPath := range list {
|
||||||
if !strings.HasPrefix(string(objectPath), string(s.service.Path())+"/char") {
|
objects = append(objects, string(objectPath))
|
||||||
|
}
|
||||||
|
sort.Strings(objects)
|
||||||
|
for _, objectPath := range objects {
|
||||||
|
if !strings.HasPrefix(objectPath, string(s.service.Path())+"/char") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
suffix := string(objectPath)[len(s.service.Path()+"/"):]
|
suffix := objectPath[len(s.service.Path()+"/"):]
|
||||||
if len(strings.Split(suffix, "/")) != 1 {
|
if len(strings.Split(suffix, "/")) != 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
characteristic, err := gatt.NewGattCharacteristic1(objectPath)
|
characteristic, err := gatt.NewGattCharacteristic1(dbus.ObjectPath(objectPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue