Skip to content

Commit f4fcee3

Browse files
committed
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.
1 parent 3b1ff4f commit f4fcee3

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

gap_darwin.go

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

179+
// Connected returns whether the device is currently connected.
180+
func (d Device) Connected() (bool, error) {
181+
return d.prph.State() == cbgo.PeripheralStateConnected, nil
182+
}
183+
179184
// RequestConnectionParams requests a different connection latency and timeout
180185
// of the given device connection. Fields that are unset will be left alone.
181186
// 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
@@ -538,6 +538,15 @@ func (d Device) Disconnect() error {
538538
return d.device.Call("org.bluez.Device1.Disconnect", 0).Err
539539
}
540540

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

gap_windows.go

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

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

0 commit comments

Comments
 (0)