[Question] Is Connect() method blocking?
ic2hrmk opened this issue · 1 comments
Hi,
I am trying to handle a flow when it is needed to be connected to the device only for a particular time.
I use the context
object to control the point in time when to cancel the connection, so I want to gracefully disconnect from the target device. To do that, I call the Pair()
method on the device, and once it's completed, I call the Connect()
method.
The question here is, is the Connect()
blocking call?
From what I can see it is, but in the example you provide (
go-bluetooth/examples/agent/agent.go
Line 63 in 9857391
Thanks!
PS:
To be on the same page, I share a code that can temporary connect to the target:
func (rcv *bluetoothAcquirer) Lock(ctx context.Context, target *Target, signals chan struct{}) error {
resolvedDevice, err := device.NewDevice1(target.Path)
if err != nil {
return err
}
//
// Pair the target
//
pairErrSignal := make(chan error)
go func() {
err = resolvedDevice.Pair()
if err != nil {
pairErrSignal <- errors.Wrapf(err, "failed to pair the device [target=%s]", resolvedDevice.Properties.Address)
return
}
pairErrSignal <- nil
}()
select {
case err = <-pairErrSignal:
if err != nil {
return err
}
signals <- struct{}{}
case _ = <-ctx.Done():
return nil
}
//
// Connect the target
//
connectionErrSignal := make(chan error)
go func() {
err = resolvedDevice.Connect()
if err != nil {
connectionErrSignal <- errors.Wrapf(err, "failed to connect the device [target=%s]", resolvedDevice.Properties.Address)
}
}()
select {
case err = <-connectionErrSignal:
if err != nil {
return err
}
case _ = <-ctx.Done():
//
// TODO: refactor error collection
//
disconnectErrMsg := ""
unpairErrMsg := ""
err = resolvedDevice.Disconnect()
if err != nil {
disconnectErrMsg = errors.Wrap(err, "failed to properly disconnect").Error()
}
err = resolvedDevice.CancelPairing()
if err != nil {
unpairErrMsg = errors.Wrap(err, "failed to properly un-pair").Error()
}
if disconnectErrMsg != "" && unpairErrMsg != "" {
err = errors.New(strings.Join([]string{disconnectErrMsg, unpairErrMsg}, "; "))
return err
}
return nil
}
return nil
}
Thanks!
Hi, yes it should be blocking but I am unable to verify in reasonable time. Could you provide a PR to fix this eventually ?
Thank you