Skip to content

Commit 62565e6

Browse files
committed
feature: implement connect handler for Advertiser for HCI
Signed-off-by: deadprogram <[email protected]>
1 parent cdf3fe1 commit 62565e6

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

gap_hci.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,6 @@ func (d Device) Disconnect() error {
262262
}
263263

264264
d.adapter.removeConnection(d)
265-
266-
if d.adapter.connectHandler != nil {
267-
d.adapter.connectHandler(d, false)
268-
}
269-
270265
return nil
271266
}
272267

@@ -436,6 +431,46 @@ func (a *Advertisement) Start() error {
436431
}
437432
}
438433

434+
switch {
435+
case a.adapter.hci.connectData.connected:
436+
random := a.adapter.hci.connectData.peerBdaddrType == 0x01
437+
438+
d := Device{
439+
Address: Address{
440+
MACAddress{
441+
MAC: makeAddress(a.adapter.hci.connectData.peerBdaddr),
442+
isRandom: random},
443+
},
444+
deviceInternal: &deviceInternal{
445+
adapter: a.adapter,
446+
handle: a.adapter.hci.connectData.handle,
447+
mtu: defaultMTU,
448+
notificationRegistrations: make([]notificationRegistration, 0),
449+
},
450+
}
451+
a.adapter.addConnection(d)
452+
453+
if a.adapter.connectHandler != nil {
454+
a.adapter.connectHandler(d, true)
455+
}
456+
457+
a.adapter.hci.clearConnectData()
458+
case a.adapter.hci.connectData.disconnected:
459+
d := Device{
460+
deviceInternal: &deviceInternal{
461+
adapter: a.adapter,
462+
handle: a.adapter.hci.connectData.handle,
463+
},
464+
}
465+
a.adapter.removeConnection(d)
466+
467+
if a.adapter.connectHandler != nil {
468+
a.adapter.connectHandler(d, false)
469+
}
470+
471+
a.adapter.hci.clearConnectData()
472+
}
473+
439474
time.Sleep(5 * time.Millisecond)
440475
}
441476
}()

hci.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ type leAdvertisingReport struct {
113113

114114
type leConnectData struct {
115115
connected bool
116+
disconnected bool
116117
status uint8
117118
handle uint16
118119
role uint8
@@ -613,6 +614,9 @@ func (h *hci) handleEventData(buf []byte) error {
613614
h.att.removeConnection(handle)
614615
h.l2cap.removeConnection(handle)
615616

617+
h.connectData.disconnected = true
618+
h.connectData.handle = handle
619+
616620
return h.leSetAdvertiseEnable(true)
617621

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

819823
func (h *hci) clearConnectData() error {
820824
h.connectData.connected = false
825+
h.connectData.disconnected = false
821826
h.connectData.status = 0
822827
h.connectData.handle = 0
823828
h.connectData.role = 0

0 commit comments

Comments
 (0)