File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -134,6 +134,7 @@ type Connection uint16
134134type GAPDevice interface {
135135 DiscoverServices (uuids []UUID ) ([]DeviceService , error )
136136 RequestConnectionParams (params ConnectionParams ) error
137+ Connected () (bool , error )
137138 Disconnect () error
138139}
139140
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
128132type DeviceService struct {}
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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+
2832type DeviceService struct {}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments