Skip to content

Commit

Permalink
adaptor/macos: handle case when adapter enable sends notification bef…
Browse files Browse the repository at this point in the history
…ore event delegate is set

Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Oct 28, 2020
1 parent 266de98 commit 0bc0ef8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions adapter_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ func (a *Adapter) Enable() error {
}

// wait until powered
a.poweredChan = make(chan error)
a.poweredChan = make(chan error, 1)

a.cmd = &centralManagerDelegate{a: a}
a.cm.SetDelegate(a.cmd)
select {
case <-a.poweredChan:
case <-time.NewTimer(10 * time.Second).C:
return errors.New("timeout enabling CentralManager")

if a.cm.State() != cbgo.ManagerStatePoweredOn {
select {
case <-a.poweredChan:
case <-time.NewTimer(10 * time.Second).C:
return errors.New("timeout enabling CentralManager")
}
}

// drain any extra powered-on events from channel
for len(a.poweredChan) > 0 {
<-a.poweredChan
}
a.poweredChan = nil

// wait until powered?
a.pmd = &peripheralManagerDelegate{a: a}
Expand All @@ -73,7 +80,7 @@ type centralManagerDelegate struct {
func (cmd *centralManagerDelegate) CentralManagerDidUpdateState(cmgr cbgo.CentralManager) {
// powered on?
if cmgr.State() == cbgo.ManagerStatePoweredOn {
close(cmd.a.poweredChan)
cmd.a.poweredChan <- nil
}

// TODO: handle other state changes.
Expand Down

0 comments on commit 0bc0ef8

Please sign in to comment.