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

+1-1
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

+3-3
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

+2-2
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

+2-2
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

+9-3
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

+10-4
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

+9-2
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

+2-2
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

+2-2
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

+1-1
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 {

examples/stop-advertisement/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
2222
LocalName: "Go Bluetooth",
2323
}))
24-
adapter.SetConnectHandler(func(device bluetooth.Address, connected bool) {
24+
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
2525
if connected {
2626
println("connected, not advertising...")
2727
advState = false

gap_darwin.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func (a *Adapter) StopScan() error {
8585

8686
// Device is a connection to a remote peripheral.
8787
type Device struct {
88+
Address Address
89+
8890
*deviceInternal
8991
}
9092

@@ -137,7 +139,8 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
137139
}
138140

139141
d := Device{
140-
&deviceInternal{
142+
Address: address,
143+
deviceInternal: &deviceInternal{
141144
cm: a.cm,
142145
prph: p,
143146
servicesChan: make(chan error),
@@ -148,7 +151,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
148151
d.delegate = &peripheralDelegate{d: d}
149152
p.SetDelegate(d.delegate)
150153

151-
a.connectHandler(address, true)
154+
a.connectHandler(d, true)
152155

153156
return d, nil
154157

gap_linux.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ func makeScanResult(props map[string]dbus.Variant) ScanResult {
292292

293293
// Device is a connection to a remote peripheral.
294294
type Device struct {
295+
Address Address // the MAC address of the device
296+
295297
device dbus.BusObject // bluez device interface
296298
adapter *Adapter // the adapter that was used to form this device connection
297-
address Address // the address of the device
298299
}
299300

300301
// Connect starts a connection attempt to the given peripheral device address.
@@ -303,9 +304,9 @@ type Device struct {
303304
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
304305
devicePath := dbus.ObjectPath(string(a.adapter.Path()) + "/dev_" + strings.Replace(address.MAC.String(), ":", "_", -1))
305306
device := Device{
307+
Address: address,
306308
device: a.bus.Object("org.bluez", devicePath),
307309
adapter: a,
308-
address: address,
309310
}
310311

311312
// Already start watching for property changes. We do this before reading

gap_nrf528xx-central.go

-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ func (a *Adapter) StopScan() error {
9292
return nil
9393
}
9494

95-
// Device is a connection to a remote peripheral.
96-
type Device struct {
97-
connectionHandle C.uint16_t
98-
}
99-
10095
// In-progress connection attempt.
10196
var connectionAttempt struct {
10297
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means ready (connected or timeout)

gap_sd.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build softdevice
2+
3+
package bluetooth
4+
5+
/*
6+
#include "ble_gap.h"
7+
*/
8+
import "C"
9+
10+
// Device is a connection to a remote peripheral or central.
11+
type Device struct {
12+
Address Address
13+
14+
connectionHandle C.uint16_t
15+
}

0 commit comments

Comments
 (0)