Module: BLE::Notifications

Included in:
Device
Defined in:
lib/ble/notifications.rb

Instance Method Summary collapse

Instance Method Details

#on_notification(service, characteristic, raw: false, &callback) ⇒ Object

Registers the callback to be invoked when a notification from the given characteristic is received.

NOTE: Requires the device to be subscribed to characteristic notifications.

Parameters:

  • service (String, Symbol)
  • characteristic (String, Symbol)
  • raw (Boolean) (defaults to: false)

    When raw is true the value (set/get) is a binary string, instead of an object corresponding to the decoded characteristic (float, integer, array, …)

  • callback (Proc)

    This callback will have the notified value as argument.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ble/notifications.rb', line 28

def on_notification(service, characteristic, raw: false, &callback)
  _require_connection!
  char= _find_characteristic(service, characteristic)
  if char.flag?('notify')
    char.on_change(raw: raw) { |val|
      callback.call(val)
    }
  elsif char.flag?('encrypt-read') ||
      char.flag?('encrypt-authenticated-read')
    raise NotYetImplemented
  else
    raise AccessUnavailable
  end
end

#start_notify!(service, characteristic) ⇒ Object

Registers current device for notifications of the given characteristic. Synonym for 'subscribe' or 'activate'. This step is required in order to later receive notifications.

Parameters:

  • service (String, Symbol)
  • characteristic (String, Symbol)

10
11
12
13
14
15
16
17
# File 'lib/ble/notifications.rb', line 10

def start_notify!(service, characteristic)
  char= _find_characteristic(service, characteristic)
  if char.flag?('notify')
    char.notify!
  else
    raise OperationNotSupportedError.new("No notifications available for characteristic #{characteristic}")
  end
end