Class: BLE::Characteristic

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CharRegistry
Defined in:
lib/ble/characteristic.rb,
lib/ble/db_ollie.rb,
lib/ble/db_nordic.rb,
lib/ble/db_eddystone.rb,
lib/ble/db_sig_characteristic.rb

Overview

Build information about Bluetooth Characteristics

Defined Under Namespace

Classes: NotFound

Instance Method Summary collapse

Methods included from CharRegistry

included

Constructor Details

#initialize(desc) ⇒ Characteristic

Returns a new instance of Characteristic.


14
15
16
17
# File 'lib/ble/characteristic.rb', line 14

def initialize(desc)
  @dbus_obj= desc[:obj]
  @desc= CharDesc.new(desc)
end

Instance Method Details

#async_read(raw: false) ⇒ Object


60
61
62
63
64
65
66
67
# File 'lib/ble/characteristic.rb', line 60

def async_read(raw: false)
  return Concurrent::Promise.execute do
    @dbus_obj[I_GATT_CHARACTERISTIC].ReadValue() do |result|
      val= result.first
      val= _deserialize_value(val, raw: raw)
    end
  end
end

#async_write(val, raw: false) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/ble/characteristic.rb', line 46

def async_write(val, raw: false)
  val= _serialize_value(val, raw: raw)
  Concurrent::Promise.execute do
    @dbus_obj[I_GATT_CHARACTERISTIC].WriteValue(val, []) do |result|
      result
    end
  end
end

#notify!Object

Register to this characteristic for notifications when its value changes.


71
72
73
# File 'lib/ble/characteristic.rb', line 71

def notify!
  @dbus_obj[I_GATT_CHARACTERISTIC].StartNotify
end

#on_change(raw: false, &callback) ⇒ Object


75
76
77
78
79
80
81
82
83
# File 'lib/ble/characteristic.rb', line 75

def on_change(raw: false, &callback)
  @dbus_obj[I_PROPERTIES].on_signal('PropertiesChanged') do |intf, props|
    case intf
    when I_GATT_CHARACTERISTIC
      val= _deserialize_value(props['Value'], raw: raw)
      callback.call(val)
    end
  end
end

#read(raw: false) ⇒ Object


55
56
57
58
# File 'lib/ble/characteristic.rb', line 55

def read(raw: false)
  val= @dbus_obj[I_GATT_CHARACTERISTIC].ReadValue().first
  val= _deserialize_value(val, raw: raw)
end

#write(val, raw: false) ⇒ Object

++++++++++++++++++++++++++++


41
42
43
44
# File 'lib/ble/characteristic.rb', line 41

def write(val, raw: false)
  val= _serialize_value(val, raw: raw)
  @dbus_obj[I_GATT_CHARACTERISTIC].WriteValue(val, [])
end