Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reconnect connect/disconnect handlers for HCI, Linux, and Windows Centrals #319

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
}
a.addConnection(d)

if a.connectHandler != nil {
a.connectHandler(d, true)
}

return d, nil

} else {
Expand Down Expand Up @@ -427,6 +431,46 @@ func (a *Advertisement) Start() error {
}
}

switch {
case a.adapter.hci.connectData.connected:
random := a.adapter.hci.connectData.peerBdaddrType == 0x01
ysoldak marked this conversation as resolved.
Show resolved Hide resolved

d := Device{
Address: Address{
MACAddress{
MAC: makeAddress(a.adapter.hci.connectData.peerBdaddr),
isRandom: random},
},
deviceInternal: &deviceInternal{
adapter: a.adapter,
handle: a.adapter.hci.connectData.handle,
mtu: defaultMTU,
notificationRegistrations: make([]notificationRegistration, 0),
},
}
a.adapter.addConnection(d)

if a.adapter.connectHandler != nil {
a.adapter.connectHandler(d, true)
}

a.adapter.hci.clearConnectData()
case a.adapter.hci.connectData.disconnected:
d := Device{
deviceInternal: &deviceInternal{
adapter: a.adapter,
handle: a.adapter.hci.connectData.handle,
},
}
a.adapter.removeConnection(d)

if a.adapter.connectHandler != nil {
a.adapter.connectHandler(d, false)
}

a.adapter.hci.clearConnectData()
}

time.Sleep(5 * time.Millisecond)
}
}()
Expand Down
8 changes: 8 additions & 0 deletions gap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,20 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
<-connectChan
}

if a.connectHandler != nil {
a.connectHandler(device, true)
}

return device, nil
}

// Disconnect from the BLE device. This method is non-blocking and does not
// wait until the connection is fully gone.
func (d Device) Disconnect() error {
if d.adapter.connectHandler != nil {
d.adapter.connectHandler(d, false)
}

// we don't call our cancel function here, instead we wait for the
// property change in `watchForConnect` and cancel things then
return d.device.Call("org.bluez.Device1.Disconnect", 0).Err
Expand Down
11 changes: 10 additions & 1 deletion gap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,12 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
return Device{}, err
}

return Device{address, bleDevice, newSession}, nil
device := Device{address, bleDevice, newSession}
if a.connectHandler != nil {
a.connectHandler(device, true)
}

return device, nil
}

// Disconnect from the BLE device. This method is non-blocking and does not
Expand All @@ -358,6 +363,10 @@ func (d Device) Disconnect() error {
return err
}

if DefaultAdapter.connectHandler != nil {
DefaultAdapter.connectHandler(d, false)
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type leAdvertisingReport struct {

type leConnectData struct {
connected bool
disconnected bool
ysoldak marked this conversation as resolved.
Show resolved Hide resolved
status uint8
handle uint16
role uint8
Expand Down Expand Up @@ -613,6 +614,9 @@ func (h *hci) handleEventData(buf []byte) error {
h.att.removeConnection(handle)
h.l2cap.removeConnection(handle)

h.connectData.disconnected = true
h.connectData.handle = handle

return h.leSetAdvertiseEnable(true)

case evtEncryptionChange:
Expand Down Expand Up @@ -818,6 +822,7 @@ func (h *hci) clearAdvData() error {

func (h *hci) clearConnectData() error {
h.connectData.connected = false
h.connectData.disconnected = false
h.connectData.status = 0
h.connectData.handle = 0
h.connectData.role = 0
Expand Down
Loading