Skip to content

Commit 5372045

Browse files
authored
feat(gap): add Device.Connected() (#426)
* feat(gap): add Device.Connected() It is currently not possible to know wether we are connected or not. This allow the user to know if the device is connected. * add Connected to GAPDevice interface
1 parent 46c61fd commit 5372045

8 files changed

Lines changed: 43 additions & 0 deletions

File tree

gap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ type Connection uint16
134134
type GAPDevice interface {
135135
DiscoverServices(uuids []UUID) ([]DeviceService, error)
136136
RequestConnectionParams(params ConnectionParams) error
137+
Connected() (bool, error)
137138
Disconnect() error
138139
}
139140

gap_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func (d Device) Disconnect() error {
178178
return nil
179179
}
180180

181+
// Connected returns whether the device is currently connected.
182+
func (d Device) Connected() (bool, error) {
183+
return d.prph.State() == cbgo.PeripheralStateConnected, nil
184+
}
185+
181186
// RequestConnectionParams requests a different connection latency and timeout
182187
// of the given device connection. Fields that are unset will be left alone.
183188
// Whether or not the device will actually honor this, depends on the device and

gap_hci.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ func (d Device) Disconnect() error {
296296
return nil
297297
}
298298

299+
func (d Device) Connected() (bool, error) {
300+
return false, errNotYetImplmented
301+
}
302+
299303
// RequestConnectionParams requests a different connection latency and timeout
300304
// of the given device connection. Fields that are unset will be left alone.
301305
// Whether or not the device will actually honor this, depends on the device and

gap_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ func (d Device) Disconnect() error {
540540
return d.device.Call("org.bluez.Device1.Disconnect", 0).Err
541541
}
542542

543+
// Connected returns whether the device is currently connected.
544+
func (d Device) Connected() (bool, error) {
545+
val, err := d.device.GetProperty("org.bluez.Device1.Connected")
546+
if err != nil {
547+
return false, err
548+
}
549+
return val.Value().(bool), nil
550+
}
551+
543552
// RequestConnectionParams requests a different connection latency and timeout
544553
// of the given device connection. Fields that are unset will be left alone.
545554
// Whether or not the device will actually honor this, depends on the device and

gap_nrf51.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,8 @@ func (d Device) RequestConnectionParams(params ConnectionParams) error {
125125
return errNotYetImplmented
126126
}
127127

128+
func (d Device) Connected() (bool, error) {
129+
return false, errNotYetImplmented
130+
}
131+
128132
type DeviceService struct{}

gap_nrf528xx-central.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,7 @@ func (d Device) RequestConnectionParams(params ConnectionParams) error {
237237
errCode := C.sd_ble_gap_conn_param_update(d.connectionHandle, &connParams)
238238
return makeError(errCode)
239239
}
240+
241+
func (d Device) Connected() (bool, error) {
242+
return false, errNotYetImplmented
243+
}

gap_s113v7.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ func (d Device) RequestConnectionParams(params ConnectionParams) error {
2525
return errNotYetImplmented
2626
}
2727

28+
func (d Device) Connected() (bool, error) {
29+
return false, errNotYetImplmented
30+
}
31+
2832
type DeviceService struct{}

gap_windows.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,18 @@ func (d Device) Disconnect() error {
430430
return nil
431431
}
432432

433+
// Connected returns whether the device is currently connected.
434+
func (d Device) Connected() (bool, error) {
435+
if d.device == nil {
436+
return false, nil
437+
}
438+
status, err := d.device.GetConnectionStatus()
439+
if err != nil {
440+
return false, err
441+
}
442+
return status == bluetooth.BluetoothConnectionStatusConnected, nil
443+
}
444+
433445
// RequestConnectionParams requests a different connection latency and timeout
434446
// of the given device connection. Fields that are unset will be left alone.
435447
// Whether or not the device will actually honor this, depends on the device and

0 commit comments

Comments
 (0)