Skip to content
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func main() {
must("enable BLE stack", adapter.Enable())

ctx, cancel := context.WithCancel(context.Background())
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool, err error) {
if connected {
println("device connected:", device.Address.String())
return
}

println("device disconnected:", device.Address.String())
println("device disconnected:", device.Address.String(), "error:", err)
cancel()
})

Expand Down
4 changes: 2 additions & 2 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ type BLEAdapter interface {
Connect(address Address, params ConnectionParams) (Device, error)
Enable() error
Scan(callback func(*Adapter, ScanResult)) (err error)
SetConnectHandler(c func(device Device, connected bool))
SetConnectHandler(c func(device Device, connected bool, err error))
StopScan() error
}

// SetConnectHandler sets a handler function to be called whenever the adapter connects
// or disconnects. You must call this before you call adapter.Connect() for centrals
// or advertisement.Start() for peripherals in order for it to work.
func (a *Adapter) SetConnectHandler(c func(device Device, connected bool)) {
func (a *Adapter) SetConnectHandler(c func(device Device, connected bool, err error)) {
a.connectHandler = c
}
2 changes: 1 addition & 1 deletion adapter_cyw43439.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Adapter struct {
var DefaultAdapter = &Adapter{
hciAdapter: hciAdapter{
isDefault: true,
connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
},
connectedDevices: make([]Device, 0, maxConnections),
Expand Down
14 changes: 11 additions & 3 deletions adapter_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Adapter struct {
// used to allow multiple callers to call Connect concurrently.
connectMap sync.Map

connectHandler func(device Device, connected bool)
connectHandler func(device Device, connected bool, err error)
}

// DefaultAdapter is the default adapter on the system.
Expand All @@ -37,7 +37,7 @@ var DefaultAdapter = &Adapter{
pm: cbgo.NewPeripheralManager(nil),
connectMap: sync.Map{},

connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
},
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (cmd *centralManagerDelegate) DidDisconnectPeripheral(cmgr cbgo.CentralMana
addr := Address{}
uuid, _ := ParseUUID(id)
addr.UUID = uuid
cmd.a.connectHandler(Device{Address: addr}, false)
cmd.a.connectHandler(Device{Address: addr}, false, err)

// like with DidConnectPeripheral, check if we have a chan allocated for this and send through the peripheral
// this will only be true if the receiving side is still waiting for a connection to complete
Expand All @@ -120,6 +120,10 @@ func (cmd *centralManagerDelegate) DidDisconnectPeripheral(cmgr cbgo.CentralMana
// DidConnectPeripheral when peripheral is connected.
func (cmd *centralManagerDelegate) DidConnectPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral) {
id := prph.Identifier().String()
addr := Address{}
uuid, _ := ParseUUID(id)
addr.UUID = uuid
cmd.a.connectHandler(Device{Address: addr}, true, nil)

// Check if we have a chan allocated for this peripheral, and remove it
// from the map if so (it's single-use, will be garbage collected after
Expand All @@ -136,6 +140,10 @@ func (cmd *centralManagerDelegate) DidConnectPeripheral(cmgr cbgo.CentralManager
// DidFailToConnectPeripheral when peripheral connection fails.
func (cmd *centralManagerDelegate) DidFailToConnectPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral, err error) {
id := prph.Identifier().String()
addr := Address{}
uuid, _ := ParseUUID(id)
addr.UUID = uuid
cmd.a.connectHandler(Device{Address: addr}, false, err)

// Send the peripheral through so Connect can check its state
// and return the appropriate error.
Expand Down
2 changes: 1 addition & 1 deletion adapter_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type hciAdapter struct {
isDefault bool
scanning bool

connectHandler func(device Device, connected bool)
connectHandler func(device Device, connected bool, err error)

connectedDevices []Device
notificationsStarted bool
Expand Down
2 changes: 1 addition & 1 deletion adapter_hci_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Adapter struct {
var DefaultAdapter = &Adapter{
hciAdapter: hciAdapter{
isDefault: true,
connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
},
connectedDevices: make([]Device, 0, maxConnections),
Expand Down
4 changes: 2 additions & 2 deletions adapter_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Adapter struct {
address string
defaultAdvertisement *Advertisement

connectHandler func(device Device, connected bool)
connectHandler func(device Device, connected bool, err error)
}

// NewAdapter creates a new Adapter with the given ID.
Expand All @@ -34,7 +34,7 @@ type Adapter struct {
func NewAdapter(id string) *Adapter {
return &Adapter{
id: id,
connectHandler: func(device Device, connected bool) {},
connectHandler: func(device Device, connected bool, err error) {},
}
}

Expand Down
2 changes: 1 addition & 1 deletion adapter_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Adapter struct {
var DefaultAdapter = &Adapter{
hciAdapter: hciAdapter{
isDefault: true,
connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
},
connectedDevices: make([]Device, 0, maxConnections),
Expand Down
4 changes: 2 additions & 2 deletions adapter_nrf51.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func handleEvent() {
Address: Address{makeMACAddress(connectEvent.peer_addr)},
connectionHandle: gapEvent.conn_handle,
}
DefaultAdapter.connectHandler(device, true)
DefaultAdapter.connectHandler(device, true, nil)
case C.BLE_GAP_EVT_DISCONNECTED:
if defaultAdvertisement.isAdvertising.Get() != 0 {
// The advertisement was running but was automatically stopped
Expand All @@ -63,7 +63,7 @@ func handleEvent() {
device := Device{
connectionHandle: gapEvent.conn_handle,
}
DefaultAdapter.connectHandler(device, false)
DefaultAdapter.connectHandler(device, false, nil)
case C.BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
// Respond with the default PPCP connection parameters by passing
// nil:
Expand Down
6 changes: 3 additions & 3 deletions adapter_nrf528xx-full.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func handleEvent() {
println("evt: connected in peripheral role")
}
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
DefaultAdapter.connectHandler(device, true)
DefaultAdapter.connectHandler(device, true, nil)
case C.BLE_GAP_ROLE_CENTRAL:
if debug {
println("evt: connected in central role")
}
connectionAttempt.connectionHandle = gapEvent.conn_handle
connectionAttempt.state.Set(2) // connection was successful
DefaultAdapter.connectHandler(device, true)
DefaultAdapter.connectHandler(device, true, nil)
}
case C.BLE_GAP_EVT_DISCONNECTED:
if debug {
Expand All @@ -68,7 +68,7 @@ func handleEvent() {
device := Device{
connectionHandle: gapEvent.conn_handle,
}
DefaultAdapter.connectHandler(device, false)
DefaultAdapter.connectHandler(device, false, nil)
case C.BLE_GAP_EVT_CONN_PARAM_UPDATE:
if debug {
// Print connection parameters for easy debugging.
Expand Down
4 changes: 2 additions & 2 deletions adapter_nrf528xx-peripheral.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func handleEvent() {
Address: Address{makeMACAddress(connectEvent.peer_addr)},
connectionHandle: gapEvent.conn_handle,
}
DefaultAdapter.connectHandler(device, true)
DefaultAdapter.connectHandler(device, true, nil)
case C.BLE_GAP_EVT_DISCONNECTED:
if debug {
println("evt: disconnected")
Expand All @@ -52,7 +52,7 @@ func handleEvent() {
device := Device{
connectionHandle: gapEvent.conn_handle,
}
DefaultAdapter.connectHandler(device, false)
DefaultAdapter.connectHandler(device, false, nil)
case C.BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
// We need to respond with sd_ble_gap_data_length_update. Setting
// both parameters to nil will make sure we send the default values.
Expand Down
4 changes: 2 additions & 2 deletions adapter_sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ type Adapter struct {
scanning bool
charWriteHandlers []charWriteHandler

connectHandler func(device Device, connected bool)
connectHandler func(device Device, connected bool, err error)
}

// DefaultAdapter is the default adapter on the current system. On Nordic chips,
// it will return the SoftDevice interface.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{isDefault: true,
connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
}}

Expand Down
4 changes: 2 additions & 2 deletions adapter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ BLEAdapter = (*Adapter)(nil)
type Adapter struct {
watcher *advertisement.BluetoothLEAdvertisementWatcher

connectHandler func(device Device, connected bool)
connectHandler func(device Device, connected bool, err error)

defaultAdvertisement *Advertisement
}
Expand All @@ -26,7 +26,7 @@ type Adapter struct {
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{
connectHandler: func(device Device, connected bool) {
connectHandler: func(device Device, connected bool, err error) {
return
},
}
Expand Down
4 changes: 2 additions & 2 deletions examples/advertisement/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func main() {
must("enable BLE stack", adapter.Enable())

ctx, cancel := context.WithCancel(context.Background())
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool, err error) {
if connected {
println("device connected:", device.Address.String())
return
}

println("device disconnected:", device.Address.String())
println("device disconnected:", device.Address.String(), "error:", err)
cancel()
})

Expand Down
2 changes: 1 addition & 1 deletion examples/circuitplay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
ws = ws2812.New(neo)

adapter.SetConnectHandler(func(d bluetooth.Device, c bool) {
adapter.SetConnectHandler(func(d bluetooth.Device, c bool, _ error) {
connected = c

if !connected && !disconnected {
Expand Down
2 changes: 1 addition & 1 deletion examples/connparams/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
must("enable BLE stack", adapter.Enable())

newDevice = make(chan bluetooth.Device, 1)
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool, _ error) {
// If this is a new device, signal it to the separate goroutine.
if connected {
select {
Expand Down
2 changes: 1 addition & 1 deletion examples/stop-advertisement/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go Bluetooth",
}))
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool, _ error) {
if connected {
println("connected, not advertising...")
advState = false
Expand Down
2 changes: 1 addition & 1 deletion gap_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
d.delegate = &peripheralDelegate{d: d}
p.SetDelegate(d.delegate)

a.connectHandler(d, true)
a.connectHandler(d, true, nil)

return d, nil

Expand Down
6 changes: 3 additions & 3 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
a.addConnection(d)

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

return d, nil
Expand Down Expand Up @@ -499,7 +499,7 @@ func (a *Advertisement) Start() error {
a.adapter.addConnection(d)

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

a.adapter.hci.clearConnectData()
Expand All @@ -513,7 +513,7 @@ func (a *Advertisement) Start() error {
a.adapter.removeConnection(d)

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

a.adapter.hci.clearConnectData()
Expand Down
8 changes: 4 additions & 4 deletions gap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
}

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

return device, nil
Expand All @@ -532,7 +532,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
// wait until the connection is fully gone.
func (d Device) Disconnect() error {
if d.adapter.connectHandler != nil {
d.adapter.connectHandler(d, false)
d.adapter.connectHandler(d, false, nil)
}

// we don't call our cancel function here, instead we wait for the
Expand Down Expand Up @@ -610,7 +610,7 @@ func (a *Advertisement) handleDBusSignals() {
}

if connected, ok := props[bluezDevice1Connected].Value().(bool); ok {
a.adapter.connectHandler(device, connected)
a.adapter.connectHandler(device, connected, nil)
}
case dbusSignalPropertiesChanged:
// Skip any signals that are not the Device1 interface.
Expand Down Expand Up @@ -640,7 +640,7 @@ func (a *Advertisement) handleDBusSignals() {
continue
}

a.adapter.connectHandler(device, connected)
a.adapter.connectHandler(device, connected, nil)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
}

if a.connectHandler != nil {
a.connectHandler(device, status == bluetooth.BluetoothConnectionStatusConnected)
a.connectHandler(device, status == bluetooth.BluetoothConnectionStatusConnected, err)
}
})

Expand Down
Loading