muka / go-bluetooth

Golang bluetooth client based on bluez DBus interfaces
Apache License 2.0
652 stars 125 forks source link

How to use timeouts in case device is inactive #103

Closed andig closed 4 years ago

andig commented 4 years ago

In https://github.com/andig/evcc/pull/164 user reports that application gets blocked during reading BT device if the device is not connected. The code looks somewhat like this:

dev, err := nrgble.FindDevice(nrg.adapter, nrg.macaddress)
if err != nil {
    return nil, fmt.Errorf("findDevice: %s", err)
}

err = nrgble.Connect(dev, nrg.agent, nrg.device)
if err != nil {
    return nil, err
}

char, err := nrg.dev.GetCharByUUID(service)
if err != nil {
    nrg.close()
    return err
}

b, err := char.ReadValue(map[string]interface{}{})
if err != nil {
    nrg.close()
    return err
}

I couldn't find any timeout or context in the codebase that could be applicable. How should this situation be handled?

andig commented 4 years ago

I have meanwhile found the cancel result of api.Discover- please excuse the confusion.