Skip to content

Commit 5d805a9

Browse files
aykevldeadprogram
authored andcommitted
all: use Device instead of Address in SetConnectHandler
This makes it possible to discover services on a connected central while in peripheral mode, for example.
1 parent c9eafaf commit 5d805a9

15 files changed

+65
-32
lines changed

adapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ const debug = false
66
// SetConnectHandler sets a handler function to be called whenever the adaptor connects
77
// or disconnects. You must call this before you call adaptor.Connect() for centrals
88
// or adaptor.Start() for peripherals in order for it to work.
9-
func (a *Adapter) SetConnectHandler(c func(device Address, connected bool)) {
9+
func (a *Adapter) SetConnectHandler(c func(device Device, connected bool)) {
1010
a.connectHandler = c
1111
}

adapter_darwin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Adapter struct {
2424
// used to allow multiple callers to call Connect concurrently.
2525
connectMap sync.Map
2626

27-
connectHandler func(device Address, connected bool)
27+
connectHandler func(device Device, connected bool)
2828
}
2929

3030
// DefaultAdapter is the default adapter on the system.
@@ -35,7 +35,7 @@ var DefaultAdapter = &Adapter{
3535
pm: cbgo.NewPeripheralManager(nil),
3636
connectMap: sync.Map{},
3737

38-
connectHandler: func(device Address, connected bool) {
38+
connectHandler: func(device Device, connected bool) {
3939
return
4040
},
4141
}
@@ -106,7 +106,7 @@ func (cmd *centralManagerDelegate) DidDisconnectPeripheral(cmgr cbgo.CentralMana
106106
addr := Address{}
107107
uuid, _ := ParseUUID(id)
108108
addr.UUID = uuid
109-
cmd.a.connectHandler(addr, false)
109+
cmd.a.connectHandler(Device{Address: addr}, false)
110110

111111
// like with DidConnectPeripheral, check if we have a chan allocated for this and send through the peripheral
112112
// this will only be true if the receiving side is still waiting for a connection to complete

adapter_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Adapter struct {
2323
address string
2424
defaultAdvertisement *Advertisement
2525

26-
connectHandler func(device Address, connected bool)
26+
connectHandler func(device Device, connected bool)
2727
}
2828

2929
// DefaultAdapter is the default adapter on the system. On Linux, it is the
@@ -32,7 +32,7 @@ type Adapter struct {
3232
// Make sure to call Enable() before using it to initialize the adapter.
3333
var DefaultAdapter = &Adapter{
3434
id: defaultAdapter,
35-
connectHandler: func(device Address, connected bool) {
35+
connectHandler: func(device Device, connected bool) {
3636
},
3737
}
3838

adapter_ninafw.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Adapter struct {
1919
isDefault bool
2020
scanning bool
2121

22-
connectHandler func(device Address, connected bool)
22+
connectHandler func(device Device, connected bool)
2323

2424
connectedDevices []Device
2525
notificationsStarted bool
@@ -30,7 +30,7 @@ type Adapter struct {
3030
// Make sure to call Enable() before using it to initialize the adapter.
3131
var DefaultAdapter = &Adapter{
3232
isDefault: true,
33-
connectHandler: func(device Address, connected bool) {
33+
connectHandler: func(device Device, connected bool) {
3434
return
3535
},
3636
connectedDevices: make([]Device, 0, maxConnections),

adapter_nrf51.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ func handleEvent() {
4444
case C.BLE_GAP_EVT_CONNECTED:
4545
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
4646
connectEvent := gapEvent.params.unionfield_connected()
47-
address := Address{makeMACAddress(connectEvent.peer_addr)}
48-
DefaultAdapter.connectHandler(address, true)
47+
device := Device{
48+
Address: Address{makeMACAddress(connectEvent.peer_addr)},
49+
connectionHandle: gapEvent.conn_handle,
50+
}
51+
DefaultAdapter.connectHandler(device, true)
4952
case C.BLE_GAP_EVT_DISCONNECTED:
5053
if defaultAdvertisement.isAdvertising.Get() != 0 {
5154
// The advertisement was running but was automatically stopped
@@ -57,7 +60,10 @@ func handleEvent() {
5760
defaultAdvertisement.start()
5861
}
5962
currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID
60-
DefaultAdapter.connectHandler(Address{}, false)
63+
device := Device{
64+
connectionHandle: gapEvent.conn_handle,
65+
}
66+
DefaultAdapter.connectHandler(device, false)
6167
case C.BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
6268
// Respond with the default PPCP connection parameters by passing
6369
// nil:

adapter_nrf528xx-full.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@ func handleEvent() {
2525
switch id {
2626
case C.BLE_GAP_EVT_CONNECTED:
2727
connectEvent := gapEvent.params.unionfield_connected()
28-
address := Address{makeMACAddress(connectEvent.peer_addr)}
28+
device := Device{
29+
Address: Address{makeMACAddress(connectEvent.peer_addr)},
30+
connectionHandle: gapEvent.conn_handle,
31+
}
2932
switch connectEvent.role {
3033
case C.BLE_GAP_ROLE_PERIPH:
3134
if debug {
3235
println("evt: connected in peripheral role")
3336
}
3437
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
35-
DefaultAdapter.connectHandler(address, true)
38+
DefaultAdapter.connectHandler(device, true)
3639
case C.BLE_GAP_ROLE_CENTRAL:
3740
if debug {
3841
println("evt: connected in central role")
3942
}
4043
connectionAttempt.connectionHandle = gapEvent.conn_handle
4144
connectionAttempt.state.Set(2) // connection was successful
42-
DefaultAdapter.connectHandler(address, true)
45+
DefaultAdapter.connectHandler(device, true)
4346
}
4447
case C.BLE_GAP_EVT_DISCONNECTED:
4548
if debug {
@@ -62,7 +65,10 @@ func handleEvent() {
6265
// necessary.
6366
C.sd_ble_gap_adv_start(defaultAdvertisement.handle, C.BLE_CONN_CFG_TAG_DEFAULT)
6467
}
65-
DefaultAdapter.connectHandler(Address{}, false)
68+
device := Device{
69+
connectionHandle: gapEvent.conn_handle,
70+
}
71+
DefaultAdapter.connectHandler(device, false)
6672
case C.BLE_GAP_EVT_CONN_PARAM_UPDATE:
6773
if debug {
6874
// Print connection parameters for easy debugging.

adapter_nrf528xx-peripheral.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ func handleEvent() {
2929
}
3030
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
3131
connectEvent := gapEvent.params.unionfield_connected()
32-
DefaultAdapter.connectHandler(Address{makeMACAddress(connectEvent.peer_addr)}, true)
32+
device := Device{
33+
Address: Address{makeMACAddress(connectEvent.peer_addr)},
34+
connectionHandle: gapEvent.conn_handle,
35+
}
36+
DefaultAdapter.connectHandler(device, true)
3337
case C.BLE_GAP_EVT_DISCONNECTED:
3438
if debug {
3539
println("evt: disconnected")
@@ -45,7 +49,10 @@ func handleEvent() {
4549
// necessary.
4650
C.sd_ble_gap_adv_start(defaultAdvertisement.handle, C.BLE_CONN_CFG_TAG_DEFAULT)
4751
}
48-
DefaultAdapter.connectHandler(Address{}, false)
52+
device := Device{
53+
connectionHandle: gapEvent.conn_handle,
54+
}
55+
DefaultAdapter.connectHandler(device, false)
4956
case C.BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
5057
// We need to respond with sd_ble_gap_data_length_update. Setting
5158
// both parameters to nil will make sure we send the default values.

adapter_sd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ type Adapter struct {
4848
scanning bool
4949
charWriteHandlers []charWriteHandler
5050

51-
connectHandler func(device Address, connected bool)
51+
connectHandler func(device Device, connected bool)
5252
}
5353

5454
// DefaultAdapter is the default adapter on the current system. On Nordic chips,
5555
// it will return the SoftDevice interface.
5656
//
5757
// Make sure to call Enable() before using it to initialize the adapter.
5858
var DefaultAdapter = &Adapter{isDefault: true,
59-
connectHandler: func(device Address, connected bool) {
59+
connectHandler: func(device Device, connected bool) {
6060
return
6161
}}
6262

adapter_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
type Adapter struct {
1313
watcher *advertisement.BluetoothLEAdvertisementWatcher
1414

15-
connectHandler func(device Address, connected bool)
15+
connectHandler func(device Device, connected bool)
1616
}
1717

1818
// DefaultAdapter is the default adapter on the system.
1919
//
2020
// Make sure to call Enable() before using it to initialize the adapter.
2121
var DefaultAdapter = &Adapter{
22-
connectHandler: func(device Address, connected bool) {
22+
connectHandler: func(device Device, connected bool) {
2323
return
2424
},
2525
}

examples/circuitplay/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
3939
ws = ws2812.New(neo)
4040

41-
adapter.SetConnectHandler(func(d bluetooth.Address, c bool) {
41+
adapter.SetConnectHandler(func(d bluetooth.Device, c bool) {
4242
connected = c
4343

4444
if !connected && !disconnected {

0 commit comments

Comments
 (0)